C++ Lec 4
C++ Lec 4
3
Precedence
How is an expression with multiple operators evaluated?
Example: float val(5.0); cout << val++ – 7.0/2.0;
Operator precedence determines the order of evaluation, i.e. how operators and operands are grouped. As you can see from
the table, ++ has the highest precedence and / has a higher precedence than -. The example is evaluated as follows:
(val++) – (7.0/2.0). → The result is 1.5, as val is incremented later. If two operators have equal
precedence, the expression will be evaluated as shown in column three of the table.
Example: 3 * 5 % 2 is equivalent to (3 * 5) % 2
14 Computer fundamentals and programming
Simple Assignments
A simple assignment uses the assignment operator = to assign the value of a variable to an
expression. In expressions of this type the variable must be placed on the left and the
assigned value on the right of the assignment operator.
Examples: z = 7.5;
y = z;
x = 2.0 + 4.2 * z;
The assignment operator has low precedence. In the case of the last example, the right side of the expression is first
evaluated and the result is assigned to the variable on the left. Each assignment is an expression in its own right, and its
value is the value assigned.
Example: sin(x = 2.5);
In this assignment the number 2.5 is assigned to x and then passed to the function as an argument. Multiple
assignments, which are always evaluated from right to left, are also possible.
Example: i = j = 9;
In this case the value 9 is first assigned to j and then to i.
11 Computer fundamentals and programming
Compound Assignments
In addition to simple assignment operators there are also compound assignment operators that simultaneously
perform an arithmetic operation and an assignment, for example.
Examples. i += 3; is equivalent to i = i + 3;
i *= j + 2; is equivalent to i = i * (j+2);
The second example shows that compound assignments are implicitly placed in parentheses, as is demonstrated
by the fact that the precedence of the compound assignment is just as low as that of the simple assignment.
Compound assignment operators can be composed from any binary arithmetic operator. The following
compound operators are thus available: +=, -=, *=, /=, and %=. You can modify a variable when evaluating a
complex expression by means of an assignment or the ++, -- operators. This technique is referred to as a side
effect. Avoid use of side effects if possible, as they often lead to errors and can impair the readability of your
programs.
11 Computer fundamentals and programming
Ex
Write a c++ program to enter two floating-
point numbers (x, y) then find the
following:
1- Increase x by y.
2- Multiply the resulted x by y.
3-Divided the new value of x by y.
4-Print each of the above results C++ programming
11 Computer fundamentals and programming
Conditionals Operators
Conditionals use two kinds of special operators: relational and logical. These are used to determine
whether some condition is true or false.
▪ Relational operators:
The relational operators are used to test a relation
between two expressions:
They work the same as the arithmetic operators
(e.g., a > b) but return a Boolean value of either
true or false, indicating whether the relation
tested for holds. (An expression that returns this
kind of value is called a Boolean expression.) For
example, if the variables x and y have been set to
6 and 2, respectively, then x > y returns true.
Similarly, x < 5 returns false.
5 Computer fundamentals and programming
Note:
You cannot use the assignment operator = to
compare two expressions. The compiler will
not generate an error message if the value on
the left is a variable. This mistake has caused
headaches for lots of beginners when
troubleshooting their programs.
11 Computer fundamentals and programming
▪ Logical operators
The logical operators are often used to combine
relational expressions into more complicated
Boolean expressions:
The ! operator is a unary operator, taking only one argument and negating its value:
11 Computer fundamentals and programming
11
C++ programming
5 Computer fundamentals and programming
Precedence of Boolean
Operators
The && operator has higher precedence than ||.
The precedence of both these operators
is higher than the precedence of an assignment
operator, but lower than the precedence
of all previously used operators. This is why it was
permissible to omit the parentheses in
Many the examples.
The ! operator is a unary operator and thus has
higher precedence.
5 Computer fundamentals and programming
Example
C++ programming
Computer fundamentals and programming
Computer fundamentals and programming
Control Flow
The statements needed to control the flow of a program.
if(condition)
Statement;
Computer fundamentals and programming
Example
Enter a number and print “Yes” if its value is greater than (10).
C++ programming
Computer fundamentals and programming
• if-else
The if-else form is used to decide between two sequences of statements referred to as blocks:
if(condition)
{ statementA1;
statementA2;
…
}
else
{ statementB1;
statementB2;
…
}
If the condition is met, the block corresponding to the if is executed. Otherwise, the block
corresponding to the else is executed. Because the condition is either satisfied or not, one of the
blocks in an if-else must execute. If there is only one statement for any of the blocks, the curly
braces for that block may be omitted:
if(condition)
statementA1;
else
statementB1;
Computer fundamentals and programming
Example
Enter a number and print “Yes” if its value is greater than (10) and “No” for other cases.
C++ programming
Computer fundamentals and programming
Example
Enter two numbers ( a and b) then print the summation of the two numbers in the case of (a) is greater than (5)
and (y) is smaller than 10. For other cases, print the multiplication of both numbers.
C++ programming
Computer fundamentals and programming
• else-if
You can use an else-if chain to selectively execute one of several options. An elseif chain implies a series of
embedded if-else statements whose layout is normally as follows:
if ( expression1 )
statement1
else if( expression2 )
statement2
.
.
.
else if( expression(n) )
statement(n)
[ else statement(n+1)]
When the else-if chain is executed, expression1, expression2, ... Are evaluated in the order in which they
occur. If one of the expressions proves to be true, the corresponding statement is executed and this terminates the
else-if chain. If none of the expressions are true, the else branch of the last if statement is executed. If this
else branch is omitted, the program executes the statement following the else-if chain.
Computer fundamentals and programming
Example
Enter two numbers (x and y). In the case of x is greater than y ,print “x is greater than y”. In the case of y is
greater than x , print “ y is greater than x ” and print “a and y are equal” when both numbers are equal.
C++ programming