Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.

Similar presentations


Presentation on theme: "LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5."— Presentation transcript:

1 LESSON 4 Decision Control Structure

2 Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5. switch statement

3 if statement if (condition) { statement1; statement2; }

4 if Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main() 5. { 6. int grade = 77; 7. if (grade >= 67) 8. { 9. cout<<“You passed C++”; 10. } 11. getch(); 12. return 0; 13. } You passed C++

5 if Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main() 5. { 6. int grade = 50; 7. if (grade >= 67) 8. { 9. cout<<“You passed C++”; 10. } 11. getch(); 12. return 0; 13. }

6 If-else statement if (condition) { statementA1; statementA2; } else { statementB1; statementB2; }

7 If-else Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int x= 6; 7. int y= 2; 8. if (x > y) 9. cout<<“x is greater than y \n”; 10. else 11. cout<<“y is greater than x \n”; 12. getch(); 13. return 0; 14. } x is greater than y

8 If-else Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int x= 4; 7. int y= 8; 8. if (x > y) 9. cout<<“x is greater than y \n”; 10. else 11. cout<<“y is greater than x \n”; 12. getch(); 13. return 0; 14. } y is greater than x

9 Nested if Statement if (condition1) { statementA1; statementA2; } else if (condition2) { statementB1; statementB2; }

10 Nested if Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int x= 6; 7. int y= 2; 8. if (x > y) 9. cout<<“x is greater than y \n”; 10. else if (y<x) 11. cout<<“y is greater than x \n”; 12. else 13. cout<<“x and y are equal \n”; x is greater than y 14. getch(); 15. return 0; 16. }

11 Nested if Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int x= 2; 7. int y= 6; 8. if (x > y) 9. cout<<“x is greater than y \n”; 10. else if (x<y) 11. cout<<“y is greater than x \n”; 12. else 13. cout<<“x and y are equal \n”; y is greater than x 14. getch(); 15. return 0; 16. }

12 Nested if Statement Sample 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int x= 6; 7. int y= 6; 8. if (x > y) 9. cout<<“x is greater than y \n”; 10. else if (y<x) 11. cout<<“y is greater than x \n”; 12. else 13. cout<<“x and y are equal \n”; x and y are equal 14. getch(); 15. return 0; 16. }

13 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.

14 The switch - case syntax  The general syntax of a switch statement is: switch (expression) { case constant1 : statementA1; statementA2; break; case constant2 : statementB1; statementB2; break; default: statementZ1; }

15 switch Example int day =3; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; break ; case 3: cout << “Wednesday” ; break ; case 4: cout << “Thursday” ; break ; case 5: cout << “Friday” ; break ; default: cout << “Invalid day.” ; break ; }

16 switch Example int day =5; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; break ; case 3: cout << “Wednesday” ; break ; case 4: cout << “Thursday” ; break ; case 5: cout << “Friday” ; break ; default: cout << “Invalid day.” ; break ; }

17 switch Example int day =6; switch ( day ) { case 1: cout <<“Monday” ; break ; case 2: cout << “Tuesday”) ; break ; case 3: cout << “Wednesday” ; break ; case 4: cout << “Thursday” ; break ; case 5: cout << “Friday” ; break ; default: cout << “Invalid day.” ; break ; }

18 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

19 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”; break ; default: cout<< “Invalid Input.”; break ; }

20 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” ; break ; default: cout<<“Invalid Input.”; break ; }

21 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” ; break ; default: cout<< “Invalid Input.”; break ; }


Download ppt "LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5."

Similar presentations


Ads by Google