lesson 4 decision control structure. decision control structures 1. if statement 2. if-else...

21
LESSON 4 Decision Control Structure

Upload: cory-mills

Post on 18-Jan-2018

239 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 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

LESSON 4

Decision Control Structure

Page 2: 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

Decision Control Structures1. if statement2. if-else statement3. If-else-if statement4. nested if statement5. switch statement

Page 3: 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

if statement

if (condition){

statement1;

statement2;}

Page 4: 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

if Statement Sample1. #include <iostream>2. #include <conio.h>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++

Page 5: 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

if Statement Sample1. #include <iostream>2. #include <conio.h>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. }

Page 6: 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

If-else statement

if (condition){

statementA1;statementA2;

} else{

statementB1;statementB2;

}

Page 7: 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

If-else Statement Sample1. #include <iostream>2. #include <conio.h>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

Page 8: 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

If-else Statement Sample1. #include <iostream>2. #include <conio.h>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

Page 9: 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

Nested if Statement

if (condition1){

statementA1;statementA2;

} else if (condition2){

statementB1;statementB2;

}

Page 10: 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

Nested if Statement Sample1. #include <iostream>2. #include <conio.h>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. else13. cout<<“x and y are equal \

n”;

x is greater than y

14. getch();15. return 0;16. }

Page 11: 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

Nested if Statement Sample1. #include <iostream>2. #include <conio.h>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. else13. cout<<“x and y are equal \

n”;

y is greater than x

14. getch();15. return 0;16. }

Page 12: 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

Nested if Statement Sample1. #include <iostream>2. #include <conio.h>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. else13. cout<<“x and y are equal \

n”;

x and y are equal

14. getch();15. return 0;16. }

Page 13: 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

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.

Page 14: 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

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;

}

Page 15: 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

switch Exampleint 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 ;

}

Page 16: 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

switch Exampleint 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 ;

}

Page 17: 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

switch Exampleint 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 ;

}

Page 18: 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

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

Page 19: 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

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 ONLYchar letter =‘b’;switch ( letter ) {

case ‘a’: cout<<“A”; break ;

case ‘b’: cout<<“B”; break ;

default: cout<< “Invalid Input.”; break ;

}

Page 20: 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

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 ;

}

Page 21: 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

To Switch or not to Switch You cannot perform relational checks

with a switch statementint x =123;switch ( x ) {

case x>5: cout <<“A” ; break ;

case x>4: cout<<“B” ; break ;

default: cout<< “Invalid Input.”; break ;

}