lesson 4 decision control structure. decision control structures 1. if statement 2. if-else...
DESCRIPTION
if statement if (condition) { statement1; statement2; }TRANSCRIPT
LESSON 4
Decision Control Structure
Decision Control Structures1. if statement2. if-else statement3. If-else-if statement4. nested if statement5. switch statement
if statement
if (condition){
statement1;
statement2;}
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++
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. }
If-else statement
if (condition){
statementA1;statementA2;
} else{
statementB1;statementB2;
}
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
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
Nested if Statement
if (condition1){
statementA1;statementA2;
} else if (condition2){
statementB1;statementB2;
}
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. }
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. }
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. }
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 : statementA1;
statementA2;break;
case constant2 :statementB1;
statementB2;break;
default: statementZ1;
}
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 ;
}
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 ;
}
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 ;
}
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 ONLYchar letter =‘b’;switch ( letter ) {
case ‘a’: cout<<“A”; break ;
case ‘b’: cout<<“B”; break ;
default: cout<< “Invalid Input.”; break ;
}
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 ;
}
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 ;
}