Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 4 Decision Control Structure

Similar presentations


Presentation on theme: "LESSON 4 Decision Control Structure"— Presentation transcript:

1 LESSON 4 Decision Control Structure

2 Decision Control Structures
if statement if-else statement nested if-else statement switch-case statement

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

4 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++

5 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;

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

7 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

8 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

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

10 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

11 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

12 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

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 : statement-list1 break; case constant2 : statement-list2 break …. default: statement-list3 }

15 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.” ; }

16 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.” ; }

17 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.” ; }

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

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

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


Download ppt "LESSON 4 Decision Control Structure"

Similar presentations


Ads by Google