Reference: www.netacad.
com 1
CHAPTER 1
Introduction to computer programming
(Input, Output, Operators, and Expressions)
Computer Science Department
Jordan University of Science and Technology
Reference: www.netacad.com 2
1.7 CONNECTING WITH THE
REAL WORLD: INPUT AND
OUTPUT
Reference: www.netacad.com 3
1.7 Input and output
• When data moves in the direction of the human (user) to
the computer program, it’s called the input
• cin stream, used with the >> operator to insert data.
• The >> operator itself is sometimes referred to as an extraction
operator.
• The data transferred from the computer to the human, is
called the output.
• cout stream, used with the << operator to output data.
• The << operator itself is sometimes referred to as an insertion
operator.
Reference: www.netacad.com 4
1.7 output
• To print the value of an integer variable we need to send it
to cout stream through << operator
• Both the << operator and cout stream are responsible for
printing
• You can connect more than one << operator in
one cout statement and each of the printed elements
may be of a different type and a different nature
Reference: www.netacad.com 5
1.7 output
• To break the line being sent to the screen using two ways:
• First, we can use one of the control characters called “newline” and
coded as \n (note: we use two characters to write it down but the
compiler sees it as one character).
• The newline character forces the console to complete the current line
and to start a new one.
• Second, using a manipulator called endl (as “end line”). Using endl
We can achieve exactly the same effect as \n.
• Example:
• This snippet causes the console to display the following three lines of
text:
1
2
3
Reference: www.netacad.com 6
1.7 Input
• C++ program allow getting information from the user,
transfer it to the program, and then use it for calculations.
• C++ use cin stream with the operator >>, referred to as an
extraction operator.
• The cin stream, along with the extraction operator, is
responsible for:
• transferring the human-readable form of the data from the input
device e.g. a console.
• converting the data into the internal (machine) representation of the
value being input.
Reference: www.netacad.com 7
1.7 Input
• Using cin stream with the operator >>; C++ language
program get data from a human and store it in variables.
• C++ program can ask the user to enter a value from the
keyboard and the program stores it in a specified variable
• For example: cin>>MaxPrice;
• A difference between cout and cin streams is that the
argument for cout may not be a variable. It can also be an
expression or constants.
• For example: cout << 2 * i;
• But using an input stream, we need to explicitly specify the
variable that can store the data entered by the user.
Reference: www.netacad.com 8
1.7 Input
• Example:
Reference: www.netacad.com 9
1.4 Operators
• An operator is a symbol of the programming language,
which is able to operate on the values
• For example, an assignment operator is (=)
• Used to assign values to variables X= 4
• Other operators available in C++ language associated
with widely recognizable arithmetic operations.
Reference: www.netacad.com 10
1.4 Multiplication ( * )
• An asterisk * is a multiplication operator.
• For example:
• k will be set to the value of 120, while the z variable will be set to
0.625.
Reference: www.netacad.com 11
1.4 Division ( / )
• A slash / is a divisional operator. The value in front of the
slash is a dividend, while the value behind the slash is
a divisor.
• For example:
• k will be set to 2, z to 0.5.
Reference: www.netacad.com 12
1.4 Division by zero
• This will result with a compilation error, runtime error or
some message at runtime. As a general rule, you
shouldn't divide by zero.
• For example:
• And the code:
• when you try to execute that code, the result of the operation is not
a number. It’s a special featured value named inf (as in infinitive).
Generally, this kind of illegal operation is a so-called exception.
Reference: www.netacad.com 13
1.4 Addition ( + )
• The addition operator is the + (plus) sign.
• For example:
• k is equal to 102 and z to 1.02..
Reference: www.netacad.com 14
1.4 Subtraction( - )
• The subtraction operator is the – (minus) sign.
• this operator has another meaning – it can change the sign of a
number.
• For example:
• k will be equal to -100, while z will be equal to 0.0.
Reference: www.netacad.com 15
1.4 Unary minus ( - )
• In “subtracting” applications, the minus operator expects two
arguments: the left (a minuend in arithmetical terms) and right
(a subtrahend).
• For this reason, the subtraction operator is considered to be one of the binary
operators, just like the addition, multiplication and division operators.
• The minus operator used as the unary operator, as it expects only
one argument - the right one.
• For example:
• The variable j will be assigned the value of 100.
Reference: www.netacad.com 16
1.4 Unary plus (+)
• Its role is to preserve the sign. Take a look at the snippet
• Although such a construction is syntactically correct, using it
doesn’t make much sense and it would be hard to find a good
rationale for doing it.
• For example:
• The variable j will be assigned the value of 100.
Reference: www.netacad.com 17
1.4 Remainder ( % )
• Its representation in the C++ language is the % (percent)
character.
• It’s a binary operator (it performs the modulo operation) and
both arguments cannot be floats.
• For example:
• The k variable is 3 (because 2 * 5 + 3 = 13).
Reference: www.netacad.com 18
1.4 Priorities
• The hierarchy of priorities is the phenomenon that causes some
operators to act before others is known as the priority.
• The C++ language precisely defines the priorities of all operators and
assumes that operators of larger (higher) priority perform their
operations before the operators with lower priority.
• For example:
• The result will be 17.
Reference: www.netacad.com 19
1.4 Bindings
• The binding of the operator determines the order of computations
performed by some operators with equal priority, put side by side in
one expression.
• Most operators in the C++ language have the left-sided binding,
which means that the calculation of this sample expression is
conducted from left to right.
• For example:
• 3 will be added to 2, and 5 will be added to the result.
Reference: www.netacad.com 20
1.4 List of priorities
• Priorities from the highest to the lowest priority.
Reference: www.netacad.com 21
1.4 List of priorities
• Quiz
• Result is:
Reference: www.netacad.com 22
1.4 Parentheses
• Parentheses can change the natural order of calculation. Sub
expressions in parentheses are always calculated first.
• You can use as many parentheses as you need and we often
use them to improve the readability
• For example:
• I will take 10.
Reference: www.netacad.com
Expressions
• If all operands are integers
• Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
• If all operands are floating-point
• Expression is called a floating-point expression
• Yields a floating-point result
• Example: 12.8 * 17.5 - 34.50
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
Reference: www.netacad.com
Mixed Expressions
• Mixed expression:
• Has operands of different data types
• Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
• If operator has both types of operands
• Integer is changed to floating-point
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24
Reference: www.netacad.com 25
Type Conversion
Cast operator: provides explicit type conversion
static_cast<dataTypeName>(expression)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 25
Reference: www.netacad.com 26
Implicit conversion
• cout is able to recognize the actual type of its element even
when it is an effect of a conversion.
• A conversion can be done using the syntax: (newtype)expr
• Changes the type of the expr expression into the newtype type.
• We can see the ASCII code of any character stored within
a char variable and vice versa, or see a character whose ASCII
code is placed inside an int variable.
• For example:
• This snippet outputs the following text to the screen: X 88 88 X
Reference: www.netacad.com 27
1.4 Increment Operator ( ++ )
• Used to increment a variable by one.
• This is often done when we’re counting something .
• For example:
int Counter;
Counter = 0;
Counter = Counter + 1;
• We achieve the same effect in a shorter way:
Counter++;
Reference: www.netacad.com 28
1.4 Decrement Operator ( -- )
• Used to decrease the value of a chosen variable by one.
• This is often done when we’re counting something .
• For example:
int Counter;
Counter = 10;
Counter = Counter - 1;
• We achieve the same effect in a shorter way:
Counter--;
Reference: www.netacad.com 29
1.4 Increment and Decrement Operators
• Both operators can be used as postfix and prefix operators
with the same effect to increment or decrement a given
variable by 1 called Pre/post increment/decrement
operators.
• For example:
Reference: www.netacad.com 30
1.4 Increment and Decrement Operators
• There is a fairly significant difference between prefix and
postfix.
• Operation: Variable++ Variable--
• Effect: Use the original (unchanged) variable's value and
then increment/decrement the variable by 1.
• For example:
• The variable i is set to 1. In the second statement : the value of i will
be taken (as we use the post-increment operator), then the
variable i will be increased by 1.
• In effect, j will receive the value of 1, and i will receive the value of 2.
Reference: www.netacad.com 31
1.4 Increment and Decrement Operators
• There is a fairly significant difference between prefix and
postfix.
• Operation: ++Variable --Variable
• Effect: Increment/decrement the variable by 1 and use its
value already increased/reduced.
• For example:
• The variable i is assigned the value of 1; next, the I variable is
incremented and becomes 2; next, the increased value is assigned
to the j variable.
• In effect both i and j will be equal to 2.
Reference: www.netacad.com 32
1.4 Increment and Decrement Operators
• Example:
• The program execution step by step:
1. The i variable is assigned the value of 4;
2. We take the original value of i (4), multiply it by 2, assign the
result (8) to j and eventually (post-)increment the i variable (it’s
now equal to 5).
3. We (pre-)decrement the value of j (it's now equal to 7), this
reduced value is taken and multiplied by 2, and the result (14) is
assigned to the variable i.
Reference: www.netacad.com 33
1.4 Pre- and post- operators (priorities)
Reference: www.netacad.com 34
1.4 Shortcut operators
• In the C++ language there is a short way to write the
operators.
• If op is a two-argument operator and the operator is used
in the following context:
variable = variable op expression;
It can be simplified and shown as follows:
variable op = expression;
• For example:
i=i*2; i *= 2 ;
counter = counter + 10; counter +=10;
Reference: www.netacad.com 35
1.4 Shortcut operators
Reference: www.netacad.com 36
Character and Literals
• char is a type
• literal is a symbol that uniquely identifies its value
• Examples:
• char Character = ‘A’
• char A = 100
• Character: this is not a literal; it’s a variable name
• 'A‘ : this is a literal; when you look at it you immediately know its value
• 100 : it's a literal, too (of type int)
• 100.0 : it's another literal, this time of the float type
• i + 100 : this is a combination of a variable and a literal joined together
with the + operator; such a structure is called an expression.
Reference: www.netacad.com 37
1.5 Character literals
• The C++ language uses a special character backslash( \)
• it named as escape character because by using the \ we
escape from the normal meaning of the character that
follows the slash
• You can also use the escape character to escape from
the escape character.
• For example:
Reference: www.netacad.com 38
1.5 Escape characters
Reference: www.netacad.com 39
1.5 Escape characters
• The C++ language use escape character to denote literals
representing white spaces.
• \n : denotes a transition to a new line and is sometimes called
an LF (Line Feed).
• \r : denotes a return to the beginning of the line and is
sometimes called a CR (Carriage).
• \a : as in alarm if you send this character to the screen, you’ll hear
a short beep.
• \0 : called null (none) is a character that does not represent any
character.