LESSON 4 Decision Control Structure
Decision Control Structures if statement if-else statement nested if-else statement switch-case statement
if statement if (condition) { statement1; statement2; }
if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main() { int grade = 77; if (grade >= 67) cout<<“You passed C++”; } getch(); return 0; You passed C++
if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main() { int grade = 50; if (grade >= 67) cout<<“You passed C++”; } getch(); return 0;
If-else statement if (condition) { statementA1; statementA2; } else statementB1; statementB2;
If-else Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 6; int y= 2; if (x > y) cout<<“x is greater than y \n”; else cout<<“y is greater than x \n”; getch(); return 0; } x is greater than y
If-else Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 4; int y= 8; if (x > y) cout<<“x is greater than y \n”; else cout<<“y is greater than x \n”; getch(); return 0; } y is greater than x
Nested if Statement if (condition1) { statementA1; statementA2; } else if (condition2) statementB1; statementB2;
Nested if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 6; int y= 2; if (x > y) cout<<“x is greater than y \n”; else if (y<x) cout<<“y is greater than x \n”; else cout<<“x and y are equal \n”; getch(); return 0; } x is greater than y
Nested if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 2; int y= 6; if (x > y) cout<<“x is greater than y \n”; else if (x<y) cout<<“y is greater than x \n”; else cout<<“x and y are equal \n”; getch(); return 0; } y is greater than x
Nested if Statement Sample #include <iostream> #include <conio.h> using namespace std; int main( ) { int x= 6; int y= 6; if (x > y) cout<<“x is greater than y \n”; else if (y<x) cout<<“y is greater than x \n”; else cout<<“x and y are equal \n”; getch(); return 0; } x and y are equal
The switch-case Statement Clean way to implement multi-way selection The switch statement evaluates an expression, then attempts to match the result to one of several possible cases The match must be an EXACT match.
The switch - case syntax The general syntax of a switch statement is: switch (expression) { case constant1 : statement-list1 break; case constant2 : statement-list2 break …. default: statement-list3 }
switch Example int day =3; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; case 3: cout << “Wednesday” ; case 4: cout << “Thursday” ; case 5: cout << “Friday” ; default: cout << “Invalid day.” ; }
switch Example int day =5; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; case 3: cout << “Wednesday” ; case 4: cout << “Thursday” ; case 5: cout << “Friday” ; default: cout << “Invalid day.” ; }
switch Example int day =6; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; case 3: cout << “Wednesday” ; case 4: cout << “Thursday” ; case 5: cout << “Friday” ; default: cout << “Invalid day.” ; }
To Switch or not to Switch The expression of a switch statement must result in an integral type, meaning an integer (whole number) or a char ONLY It cannot be a floating point value (float or double) You cannot perform relational checks with a switch statement
To Switch or not to Switch The expression of a switch statement must result in an integral type, meaning an integer (whole number) or a char ONLY char letter =‘b’; switch ( letter ) { case ‘a’: cout<<“A”; break ; case ‘b’: cout<<“B”; default: cout<< “Invalid Input.”; }
To Switch or not to Switch It cannot be a floating point value (float or double) float x =3.14; switch ( x ) { case 3.14: cout<<“A” ; break ; case 3.16: cout<<“B” ; default: cout<<“Invalid Input.”; }
To Switch or not to Switch You cannot perform relational checks with a switch statement int x =123; switch ( x ) { case x>5: cout <<“A” ; break ; case x>4: cout<<“B” ; default: cout<< “Invalid Input.”; }