Selection Structures C++
Selection Structures C++
Arithmetic
Lecture 4
Maemoona Kayani
Expressions
• A literal value like 34 and a properly declared variable like x are examples of simple expressions. We can use
operators to combine values and variables and form more complex expressions.
• Example shows how the addition operator (+) is used to add two integers.
#include <iostream>
int main() {
int value1, value2, sum;
std::cout << "Please enter two integer values: ";
std::cin >> value1 >> value2;
sum = value1 + value2;
std::cout << value1 << " + " << value2 << " = " << sum << '\n';
}
Expressions contd.
• int value1, value2, sum;
This statement declares three integer variables, but it does not initialize them. As we examine the rest of the
program, we will see that it would be superfluous to assign values to the variables here.
• std::cout << "Please enter two integer values: ";
This statement prompts the user to enter some information. This statement is our usual print statement, but it is not
terminated with the end-of-line marker '\n'. This is because we want the cursor to remain at the end of the printed
line so when the user types in values they appear on the same line as the message prompting for the values. When
the user presses the enter key to complete the input, the cursor will automatically move down to the next line.
• std::cin >> value1 >> value2;
This statement causes the program’s execution to stop until the user types two numbers on the keyboard and then
presses enter. The first number entered will be assigned to value1, and the second number entered will be assigned
to value2. Once the user presses the enter key, the value entered is assigned to the variable. The user may choose
to type one number, press enter, type the second number, and press enter again. Instead, the user may enter both
numbers separated by one of more spaces and then press enter only once. The program will not proceed until the
user enters two numbers.
Expressions contd.
•Three types:
1. boolean AND
2. boolean OR
3. boolean NOT
Boolean AND or Logical AND
• Symbol: &&
•All the conditions must be true for the whole expression to
be true
–Example:
if ( (a == 10)&&(b == 10)&&(d == 10) )
cout<<“a, b, and d are all equel to 10”;
Boolean OR/Logical OR
•Symbol: ||
•ANY condition is sufficient to be true for the whole expression to be
true
–Example:
if (a == 10|| b == 9||d == 1)
// do something
Boolean NOT/Logical NOT
•Symbol: !
•Reverses the meaning of the condition (makes a true condition false,
OR a false condition true)
–Example:
if ( ! (marks > 90))
// do something
Bitwise Operators (integers)
• Bitwise "and" operator &
• Bitwise "or" operator |
• Bitwise "exclusive or" operator ^
–(0 on same bits, 1 on different bits)
• Bitwise "ones complement" operator ~
• Shift left <<
• Shift right >>
Bitwise Operators (Example)
Mathematical Expressions
•An expression can be a constant, a variable, or a combination of
constants and variables combined with operators
•Can create complex expressions using multiple mathematical
operators:
•2
• height
•a + b / c
Using Mathematical Expressions
Precedence Rules
Order of Operations
Associativity of Operators
•*/%+- all associate left to right
3 + 2 + 4 + 1 = (3 + 2) + 4 + 1 = ((3+2)+4)+1 =(((3+2)+4)+ 1)
•parentheses() can be used to override the order of operations
2 + 2 * 2 –2 = 4
(2 + 2) * 2 –2 = 6
2 + 2 * (2 –2) = 2
(2 + 2) * (2 –2) = 0
• 3
Algebraic Expressions