Download presentation
Presentation is loading. Please wait.
Published byMildred Watkins Modified over 9 years ago
1
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples of selection : 2-way Selection:if…else statement Multi-way Selection:switch statement else-if construct C++ Programming
2
Logical Data and Operators data valuelogical meaning zero (0)FALSE non-zero (>0, <0)TRUE e.g. x=0 x is false x=-2 x is true x=3 x is true
3
Logical Operators
6
/*Logical Operators. */ #include int main ( void ) {int a = 4, int b = -1, int c = 0 ; printf( " %2d && %2d is %2d\n", a, b, a && b ) ; printf( " %2d && %2d is %2d\n", a, c, a && c ) ; printf( " %2d && %2d is %2d\n", c, a, c && a ) ; printf( " %2d || %2d is %2d\n", a, c, a || c ) ; printf( " %2d || %2d is %2d\n", c, a, c || a ) ; printf( " %2d || %2d is %2d\n", c, c, c || c ) ; printf( "!%2d && !%2d is %2d\n", a, c, !a && !c ) ; printf( "!%2d && %2d is %2d\n", a, c, !a && c ) ; printf( " %2d && !%2d is %2d\n", a, c, a && !c ) ; return 0 ; }
7
complement Relational Operators e.g. (x =y) (x>y) is same as !(x<=y)
8
/*Relational Operators. */ #include int main ( void ) {int a = 1 ; int b = -2 ; printf( " %2d < %2d is %2d\n", a, b, a < b ) ; printf( " %2d == %2d is %2d\n", a, b, a == b ) ; printf( " %2d != %2d is %2d\n", a, b, a != b ) ; printf( " %2d > %2d is %2d\n", a, b, a > b ) ; printf( " %2d <= %2d is %2d\n", a, b, a <= b ) ; printf( " %2d >= %2d is %2d\n", a, b, a >= b ) ; return 0 ; }
9
false (0)true (!=0) Two-Way Selection C uses the if…else statement to make 2-way selection. statement1statement2 expression Syntax: if ( expression ) statement1 else statement2
10
Examples of if…else : (1) if ( i==6 ) ++a; else --a; (2)if ( ++num > 12 ) { printf( “\n” ); num = 0; } printf( …… ); The semicolons (;) are required by ++a, --a statements, not by if…else !! Side-effect of this if (…) : num is incremented before testing for the limit 12. [ Purpose: we want to go to a new line after writing every 12th items. ]
11
(3)if ( j != 4 && k == 3 ) { ++j; --a; printf( “%d%d”, j,a ); } else { --j; ++a; printf( “%d%d”, j,a ); } Use braces {…} to combine multiple statements into one compound statement. Both if and else can be followed by : 1. null (nothing) 2. one-line statement 3. compound statement
12
/*if…else ( two-way selection )*/ #include int main ( void ) {int b ; printf( "Please enter two integers: " ) ; scanf ( "%d%d", &a, &b ) ; if ( a <= b ) printf( "%d <= %d\n", a, b ) ; else printf( "%d > %d\n", a, b ) ; return ; }
13
Nested if : we can have if…else inside an if…else statement. true statement1statement2 statement3 false expression2 truefalse expression1 Syntax : if ( expression1 ) if ( expression2 ) statement1 else statement2 else statement3
14
/*Nested if in two-way selection. */ #include int main ( void ) {int a, b ; printf( "Please enter two integers: " ) ; scanf ( "%d%d", &a, &b ) ; if ( a <= b ) if ( a < b ) printf( "%d < %d\n", a, b ) ; else printf( "%d == %d\n", a, b ) ; else printf( "%d > %d\n", a, b ) ; return ; }
15
Programming Tips: C always pair an else with the most recent unpaired if. When an if has no else to pair with, enclose it in braces{…} if ( expression1 ) if ( expression2 ) statement1 else statement2 C pairs this if and else!! if ( expression1 ) { if ( expression2 ) statement1 } else statement2 {…} avoids problem for if without else
16
The else - if construct When the case values are not integral (e.g. a float), we use the else - if construct, a special style of coding (program writing): true false score>=90 true false score>=80 true false score>=70 true false score>=60 grade=‘A’ grade=‘B’ grade=‘C’ grade=‘D’grade=‘F’ if ( score >= 90) grade = 'A' ; else if ( score >= 80 ) grade = 'B' ; else if ( score >= 70 ) grade = 'C' ; else if ( score >= 60 ) grade = 'D' ; else grade = 'F' ; same variable is being tested in expression
17
/* This program reads a test score, calculates the letter (A-F) grade for the score, and prints the grade.(using else-if ) */ #include int main ( void ) {char scoreToGrade ( int score ) ; int score ; char grade ; printf( "\nEnter the test score (0-100): " ) ; scanf( "%d", &score ) ; grade = scoreToGrade ( score ) ; printf( "The grade is: %c\n", grade ) ; return 0 ; }/* end of main body*/ Prototype Declarations Local Declarations
18
/*scoreToGrade -- function to calculate letter grade for a score. using else - if construct */ char scoreToGrade ( int score ) {char grade ; if ( score >= 90 ) grade = 'A' ; else if ( score >= 80 ) grade = 'B' ; else if ( score >= 70 ) grade = 'C' ; else if ( score >= 60 ) grade = 'D' ; else grade = 'F' ; return grade ; }/* end of scoreToGrade function*/ Local Declaration
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.