C++ - For Loop
C++ - For Loop
Decision Making
Often it is required to decide whether a certain block of code be executed or not, based on some predefined
conditions. This decision making requires the use of relational, equality and logical operators. Let us first
introduce these operators.
Page 1 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
les_04_code_01.cpp
1. #include<iostream>
3. int main ()
4. {
5. int num1 = 10, num2 = 5, num3 = -9, num4 = 5;
6. cout<<(num1>num2)<<endl;
7. cout<<(num2<num3)<<endl;
8. cout<<(num2>=num4)<<endl;
9. cout<<(num3<=num4)<<endl;
10. cout<<(num2==num4)<<endl;
11. cout<<(num1!=num2)<<endl;
12. return 0;
13. }
Logical Operators
In cases where more than one relational and equality operators simultaneously decide the final result, logical
operators help in getting the simultaneous relation. Logical operators work on Binary Operands, their result
depends on the combined states of operands.
There are two logical operators (of our interest right now).
i. OR (||)
ii. AND (&&)
Page 2 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
Logical operators work on Binary Operands, their result depends on the combined states of operands. For
the two operands x and y (both Boolean), the OR (||) operator works according to the following truth table.
x y x||y
0 0 0
0 1 1
1 0 1
1 1 1
les_04_code_02.cpp
1. #include<iostream>
3. int main ()
4. {
5. int num1 = 10, num2 = 5, num3 = -9, num4 = 5;
7. res1 = (num1<num2)&&(num2>num3);
8. cout<<"\t(num1<num2)&&(num2>num3)\t"<<res1;
9. res2 = (num1<num2)||(num2>num3);
10. cout<<"\n\t(num1<num2)||(num2>num3)\t"<<res2;
13. return 0;
14. }
Page 3 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
At this point it is important to discuss the associativity and precedence order of these new operators
introduced. The following screenshot from textbook page 55 will help you to understand. Precedence
decrease from top to bottom.
For more details please read section 2.7 of textbook (Dietel & Dietel)
if statement
if statement is used to execute a certain block of a program, based on predefined decision.
------------------------------syntax-----------------------------------------
if (relational expression)
{
//body of if statement
-----------------------------------------------------------------------------
les_04_code_03.cpp
1. #include<iostream>
3. int main()
4. {
5. int hoursWorked;
6. double rate, grossPay;
7. cout << "Enter the hours worked: ";
8. cin >> hoursWorked;
9. cout << "Enter the rate of pay: ";
Page 4 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
21. }
Output Run 1
Output Run 2
if else statement
if else statement is used to choose between certain blocks based on the relational expression if it is true or
false. Both have a separate block and only one of them will be executed.
Note : There will be only one relational expression for if else
------------------------------syntax-----------------------------------------
if (relational expression)
{
//body of if statement
statements to be executed if relational expression is true
}
else
{
//body of else statement
statements to be executed if relational expression is false
}
----------------------------------------------------------------------------
Page 5 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
les_04_code_04.cpp
1. #include<iostream>
3. int main()
4. {
5. int hoursWorked;
6. double rate, grossPay;
15. else
16. {
17. grossPay = (40 * rate) + ((hoursWorked-40) * (rate * 1.5));
18. }
19. cout << "Gross pay is: " << grossPay << endl;
20. return 0;
21. }
Nested if else
les_04_code_05.cpp
1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. int age;
6. cout<<"Please enter your age : ";
7. cin>>age;
8. if(age<18)
9. {
10. cout<<"You can't vote."<<endl;
11. if (age>=16)
12. {
Page 6 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
19. }
20. else
21. {
22. cout<<"You can vote."<<endl;
23. cout<<"You can drive."<<endl;
24. if(age>=21)
25. {
26. cout<<"You can also participate in elections."<<endl;
27. }
28. else
29. {
30. cout<<"But you can't participate in elections."<<endl;
31. }
32. }
33. return 0;
34. }
Output Run 1
Output Run 2
Page 7 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
Output Run 3
Output Run 4
if else if construct
Sometimes it is required to decide among several blocks based on certain conditions e.g. To decide the
grade of a student based on marks obtained. This can be achieved by the nested if - else construct. But it is
not a good programming practice. Better way is to use if else if construct.
-----------------------------------------------------------------------------
SYNTAX
-----------------------------------------------------------------------------
if (test condition 1)
{
body of if
}
Page 8 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
else
{
body of else // this is optional and will only be executed if none of the
test
// conditions are true
}
-----------------------------------------------------------------------------
EXAMPLE
-----------------------------------------------------------------------------
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^GradingScale^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
00 - 59 F
les_04_code_06.cpp
1. #include<iostream>
3. int main ()
4. {
5. // suitable variables declaration
6. int marks;
7. string letterGrade;
12. if(marks>=90)
13. {
14. letterGrade = "A";
15. }
Page 9 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Grading Scale^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97 - 100 A+
93 - 96 A
90 - 92 A-
87 - 89 B+
83 - 86 B
80 - 82 B-
77 - 79 C+
73 - 76 C
70 - 72 C-
67 - 69 D+
63 - 66 D
60 - 62 D-
00 - 59 F
Page 10 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
les_04_code_07.cpp
1. #include<iostream>
3. int main ()
4. {
5. // suitable variables declaration
6. int marks;
7. string letterGrade;
12. if((marks>=97)&&(marks<=100))
13. {
14. letterGrade = "A+";
15. }
16. else if ((marks >= 93)&&(marks <= 96))
17. {
18. letterGrade = "A";
19. }
20. else if ((marks >= 90)&&(marks <= 92))
21. {
22. letterGrade = "A-";
23. }
24. else if((marks>=87)&&(marks<=89))
25. {
26. letterGrade = "B+";
27. }
28. else if ((marks >= 83)&&(marks <= 86))
29. {
30. letterGrade = "B";
31. }
32. else if ((marks >= 80)&&(marks <= 82))
33. {
34. letterGrade = "B-";
35. }
Page 11 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
Task
In the lesson_04 folder, there is a file game.exe, run this file and observe how it works
(the correct answer is Watson). Your assignment is to write a code to replicate the
same program.
Bonus Program
les_04_code_08.cpp
1. #include <iostream>
3. int main()
4. {
Page 12 of 13
EE-163(C&P) lesson_04 EED MHUH, FR
9. if (op == '*')
10. {
11. result = operand1 * operand2;
12. cout << "result: " << result << endl;
13. }
14. else if (op == '/')
15. {
16. result = operand1 / operand2;
17. cout << "result: " << result << endl;
18. }
19. else if (op == '+')
20. {
21. result = operand1 + operand2;
22. cout << "result: " << result << endl;
23. }
24. else if (op == '-')
25. {
26. result = operand1 - operand2;
27. cout << "result: " << result << endl;
28. }
29. else
30. cout << "Bad expression." << endl;
31. return 0;
32. }
Page 13 of 13