2. More Operators
• Check this operator equivalence:
x = x + 5 ---> x+=5
x = x + y ---> x+=y
x = x – z ---> x-=y
x = x*y --->
x*=y
x = x/y --->
x/=y
x = x%y --->
3. 2 important operators
• Instead of x= x+1 or x+=1, c++ has ++
operator.
• Ex: int x= 5;
x++; ----> now x = 6
or
++x ----> now x = 6
4. ++x , x++ ?
• x++ execute the statement then
increment x
• Ex: int x = 6
cout<<x++; ----> print 6 on screen
cout<<x; ----> print 7 on screen
5. ++x , x++ ? (cont)
• ++x increment x, then execute the
statement.
• Ex: int x = 6;
cout<<++x; ---> print 7 on screen
cout<<x; ----> print 7 on screen
6. Example
• Write this program output on a paper
int x =4,y=5;
cout<<x++<<endl;
cout<<y++<<endl;
cout<<++x<<endl;
cout<<++y<<endl;
cout<<++x – y++<<endl;
cout<<x<<endl;
cout<<y<<endl;
14. Let’s calculate the average faster
• Translate the previous flow chart into C++
code.
15. Conditional Statements
• All conditional statements use the (True or False) of a
condition, to determine the path of the program.
16. Conditional in flow chart
• Rhombus are used to express conditional
statements:
• Conditional statements has output 2
arrows one for YES and one for NO
Is X >
60 ?
19. Equality and Relational
Operators
C++ Operator Sample C++ example Meaning
> x > y X is greater than y
< x < y X is less than y
>= x >=y X is greater than or equal y
<= x<=y X is less than or equal y
== x == y X equal to y
!= X != y X not equal to y
Notes:
• “A syntax error will occur if any of the operators ==, !=, >= and <=
appears with spaces between its pair of symbols.”
• In c++:
• False sometimes expressed by an integer zero
• True sometimes expressed by any integer other than zero
20. if-else Statement
// If then.cpp : Defines the sample conditional expressions.
#include <iostream>
using namespace std;
int main()
{
int grade;
cout<<"Enter your grade: "<<endl;
cin>>grade;
if (grade>=50)
cout<<"Congrats, You passed ;)"<<endl;
else
cout<<"See you next semester :( "<<endl;
}
21. Nested if else
// If then.cpp : Defines the sample conditional expressions.
#include <iostream>
using namespace std;
int main()
{
int grade;
cout<<"Enter your grade: "<<endl;
cin>>grade;
if (grade>=50)
if (grade >= 90)
cout<<"Congrats, You are excellent ;)"<<endl;
else
cout<<"Congrats, You passed ;)"<<endl;
else
cout<<"See you next semester :( "<<endl;
}
22. Else if
int main ()
{
int a;
cin >> a;
if( a == 10 ) {
cout << "Value of a is 10" << endl;
}
else if( a == 20 ) {
cout << "Value of a is 20" << endl;
}
else {
cout << "Value of a is not matching" << endl;
}
cout << "Exact value of a is : " << a << endl;
return 0;
}
23. if – else example
Int x = 4, y = 6;
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
24. Dangling - Else
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
These code fragments are not the same logically. Beware of
dangling-else, so it’s recommended to use braces to identify the
scope of the (if-else) block.
25. Training 1
• Let’s help our faculty and make a program that takes
your grade, and tells you whether you got A, B, C, D, F.
26. Training 1 ans.
if ( studentGrade >= 90 ) // 90 and above gets "A"
cout << "A";
else if ( studentGrade >= 80 ) // 80-89 gets "B"
cout << "B";
else if ( studentGrade >= 70 ) // 70-79 gets "C"
cout << "C";
else if ( studentGrade >= 60 ) // 60-69 gets "D"
cout << "D";
else // less than 60 gets "F"
cout << "F";
27. Training 2
• Try to do a program that :
– Take a number from user
– The program see if this number is even or
odd
– Then type a message on the screen says that
if the number is even or odd
30. Logical Operators
• Logical operators that are used to form more
complex conditions by combining simple
conditions. The logical operators are && (logical
AND), || (logical OR) and ! (logical NOT, also
called logical negation).
31. AND (&&) Operator
• Suppose that we wish to ensure that two conditions
are both True before we choose a certain path of
execution. In this case, we can use the && (logical
AND) operator, as follows:
if (isCar == true && speed >= 100 )
speedFine=400;
• To test whether x is greatest number from (x, y, z), you
would write
if ( (x > y) && (x > z))
cout<<“x is the largest number”;
32. OR (||) Operator
• We use it when we have two paths, and if either one is
true or both of them, a certain path of action happen.
if ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) )
cout << "Student grade is A" << endl;
• The && operator has a higher precedence than the ||
operator. Both operators associate from left to right.
33. NOT (!) Operator
• Not Operator enables a programmer to "reverse" the
meaning of a condition.
• The unary logical negation operator is placed before a
condition when we are interested in choosing a path of
execution if the original condition is false.
If(! (Age>=18) )
cout<<“you can’t get a driving license”;
34. Training 3
• What is the final result of each statement,
decide whether it’s (True – False):
• !( 1 || 0 )
• ( 1 || 1 && 0 ))
• !( ( 1 || 0 ) && 0 )
• !(1 == 0)
35. Training 4
make a program that asks a BeDev Trainee if he
skipped a session, and if he skipped an assignment,
and about how many bonuses did he solve.
• if he skipped an assignment and a session and:
– if he solved 6 bonus questions his score won’t
change.
– if he did not solve any bonus he will be kicked out.
• if he skipped only one of them (assignment,
session):
– If he solved 3 bonus question, his score won’t change.
• You are required to tell if his score will decrease,
not decrease(stay as it is, or increase), or that he
will be out!.
36. Solution
bool skippedAssignment, skippedSession;
int bonuses;
cout << "Did you skip an assignment? (1 for yes, 0 for no)"<< endl;
cin >> skippedAssignment;
cout << "Did you skip a session? (1 for yes, 0 for no)"<< endl;
cin >> skippedSession;
cout << "How many bonuses did you have?" << endl;
cin >> bonuses;
Taking input from user
37. Solution cont.
if(skippedAssignment && skippedSession)
{
if(bonuses == 0)
cout << "You'll be kicked out of the course" << endl;
else if(bonuses < 6)
cout << "Your score will decrease" << endl;
else
cout << "your score will not decrease" << endl;
}
else if(skippedAssignment || skippedSession)
{
if(bonuses < 3)
cout << "Your score will decrease" << endl;
else
cout << "your score will not decrease" << endl;
}
else
cout << "your score will not decrease" << endl;
39. Switch Statement
• The switch provides multiple-selection statement to
perform many different actions based on the possible
values of a variable or expression.
40. Switch statement cont.
• The previous code was not readable and hard to understand.
There is an easier way to write this using the switch
statement.
• Switch statement written as :
switch(variable){
case value_1:
statement;
break;
case value_2:
statement;
break;
default :
statement;
break;
}
• what if we don’t write a break statement?
42. Training 5
• imagine now, you are making a software for a
restaurant, and the user enters the code of the
ordered food, Then prints the price of sold item.
• For example:
• 1 -> Sandwich,
• 2 -> Juice,
• 3 -> water ,
• 4 -> chocolate.
• How you will make this using a switch
statement ?
43. Training 6
• Let’s write our simple calculator
1. Take a number from user
2. Take an operator (+, - , * , / )
3. Take another number from the user
4. Print out the result of the operation
• Now check the division by zero