Download presentation
Presentation is loading. Please wait.
Published byGabriella Bell Modified over 9 years ago
1
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection: Syntax: if (logical expression) statements1; else statements2; 1- One way selection: Syntax: if (logical expression) statements;
2
Example 1: C++ Program // File: double.cpp /*The program corresponds to Example 1 on slide 5. It takes an integer and prints its double value if it is less than 50 */ #include void main ( ) { int n ; cin >> n ; if (n < 50) cout << “The double value is “ << n * 2 << endl ; cout << “ Finished “ << endl; }
3
Example 2: C++ Program // File: Smaller.cpp /*The program corresponds to Example 3 on slide 14. It takes two integers and prints the smaller value using one output statement*/ #include void main ( ) { int num1, num2 ; cin >> num1 >> num2 ; if ( num1 < num2 ) cout << “The smaller value is “ << num1 << endl ; else cout << “The smaller value is “ << num12 << endl ; }
4
Example 3: C++ Program // File: Grades.cpp /*The program corresponds to Example 4 on slide 21. It reads a student’s mark and prints the corresponding grade */ #include void main ( ) { int mark ; cin >> mark; if ( mark 100 ) cout << “ Mark out of range” << endl; else if ( mark >= 90 ) cout << “A” << endl ; else if ( mark >= 80 ) cout << “A” << endl ; else if ( mark >= 70 ) cout << “C” << endl ; else if ( mark >= 60 ) cout << “D” << endl ; else cout << “E” << endl ; }
5
Switch Statement in C++ Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This statement has the same meaning as in the algorithmic language.
6
Example 4: C++ Program // File: music.cpp /*The program corresponds to Example 5 on slide 31. It reads a letter and displays a suitable musical note (do, re, mi, etc.)*/ #include void main ( ) { char ch ; cin >> ch ; switch ( ch ) { case ‘c’ : cout << “ do “ << endl; break; case ‘d’ : cout << “ re “ << endl; break; case ‘e’ : cout << “ mi “ << endl; break; case ‘f’ : cout << “ f “ << endl; break; case ‘g’ : cout << “ sol “ << endl; break; case ‘a’ : cout << “ la “ << endl; break; case ‘b’ : cout << “ ti “ << endl; break; default : cout << “ Invalid note was read “ << endl; }
7
Example 6: C++ Program // File: circle.cpp /*This program corresponds to Example 6 on slide 33. It is a menu driven program. It calculates the area of the circle if letter a is read or calculates the circumference if letter c is read.*/ #include void main ( ) { char ch ; float radius, area, circum; cout << “ Enter the radius of a circle: “ ; cin >> radius; cout << “ Enter a to calculate the area of a circle or c to calculate its circumference:” cin >> ch ; switch (ch) { case ‘a’ : area = 3.14f * radius * radius; cout << “ Area = “ << area << endl; break; case‘c’ : circum = 2 * radius * 3.14f ; cout << “ Circumference = “ << circum << endl; break; default : cout << “ Invalid letter was read “ << endl; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.