Download presentation
1
The If/Else Statement, Boolean Flags, and Menus Page 180
9/18/06 CS150 Introduction to Computer Science 1
2
CS150 Introduction to Computer Science 1
Formally defined if( expression ) { statement 1; statement 2; . . . statement n; } Just like a function, start at the top and execute in order to the bottom What is an expression? 9/18/06 CS150 Introduction to Computer Science 1
3
CS150 Introduction to Computer Science 1
More on Truth Expressions that evaluate to non-zero are considered true int x=5, y=0; // what will get executed? if ( x + y) { cout << “x+y is True” << endl; } if ( y ) cout << “y is True” << endl; 9/18/06 CS150 Introduction to Computer Science 1
4
Floating Point and Relational Operators
Floating point math may not work out as you expect because of round off errors. Math Class: 6 * 2/3 = 4 C++: 6.0 * = 6.0 * = 6.0 * = 6.0 * ( 2.0 / 3.0 ) = 4.0 9/18/06 CS150 Introduction to Computer Science 1
5
CS150 Introduction to Computer Science 1
Example (page 180) double result; result = 6.0 * ; if ( result == 4.0 ) { cout << “result == 4.0” << endl; } cout << setprecision(6) << fixed; cout << result << endl; cout << setprecision(2) << result; cout << endl; 9/18/06 CS150 Introduction to Computer Science 1
6
CS150 Introduction to Computer Science 1
Example 9/18/06 CS150 Introduction to Computer Science 1
7
Floating Points in Relational Operators
How can we get around this problem? 9/18/06 CS150 Introduction to Computer Science 1
8
CS150 Introduction to Computer Science 1
Boolean Flags We have seen how to store the value of a relational expression to a bool variable. bool bIsSquare = ( length == width); if ( bIsSquare ) { } Why would you want to do this? Why not use the relational expression directly? 9/18/06 CS150 Introduction to Computer Science 1
9
CS150 Introduction to Computer Science 1
Boolean Flags This use of a bool variable is called a flag. It is used to keep track of a condition so that the expression is evaluated only once 9/18/06 CS150 Introduction to Computer Science 1
10
CS150 Introduction to Computer Science 1
If Statement We may want to execute some code if an expression is true, and execute some other code when the expression is false. This can be done with two if statements… if( value >= LIMIT ) { // do something } if( value < LIMIT ) { // do something else 9/18/06 CS150 Introduction to Computer Science 1
11
CS150 Introduction to Computer Science 1
If/Else C++ provides a shortcut to combine two if statements: if( expression ) { // do stuff } else // do other stuff The statements in the else clause are executed only when the expression is false. 9/18/06 CS150 Introduction to Computer Science 1
12
CS150 Introduction to Computer Science 1
Example int number; cout << “Enter a number, I’ll tell you“; cout << “ if it is odd: “; cin >> number; // use an if/else statement here 9/18/06 CS150 Introduction to Computer Science 1
13
If/Else: Syntax and Formatting
if( expression ) { // do stuff } else // do other stuff Note the braces with the else keyword and the alignment of the else under the if on its own line 9/18/06 CS150 Introduction to Computer Science 1
14
CS150 Introduction to Computer Science 1
If/Else: Braces if( expression ) { // do stuff } else x += 9; Always use braces with the else! 9/18/06 CS150 Introduction to Computer Science 1
15
CS150 Introduction to Computer Science 1
If/Else: Commenting // the expression I’m using here // checks for . . . if( expression ) { // if the expression is true // I need to ... } else // if the expression is false 9/18/06 CS150 Introduction to Computer Science 1
16
CS150 Introduction to Computer Science 1
Practice Write a program that asks the user for two integers and output the floating point number produced by dividing the second number into the first. Be sure not to divide by zero! 9/18/06 CS150 Introduction to Computer Science 1
17
CS150 Introduction to Computer Science 1
Practice Turn this code into an if/else statement: int x, y; if ( x > y ) { x += y; } if ( y <= x) y += x; 9/18/06 CS150 Introduction to Computer Science 1
18
CS150 Introduction to Computer Science 1
Practice Are these two code snippets equivalent? int x, y; if ( x > y ) { x += y; } if ( y < x) y += x; else 9/18/06 CS150 Introduction to Computer Science 1
19
CS150 Introduction to Computer Science 1
Nested Ifs The second if is only executed if the first if conditional is false Note the indentation of the inner if There may be code between the { with the first else and the second if if ( x > y ) { } else if ( x == 9 ) 9/18/06 CS150 Introduction to Computer Science 1
20
CS150 Introduction to Computer Science 1
Using nested ifs… Write a snippet of code that will: add y to x if x > y add x to y if y > x add 1 to x if x == y int x, y; if ( If ( x > y ) { x += y; } Else { if ( x < y ) y +=x; x += 1; 9/18/06 CS150 Introduction to Computer Science 1
21
CS150 Introduction to Computer Science 1
C++ Shortcut if ( x > y ) { // do A } else if ( x == 9 ) // do B // do C if ( x > y ) { // do A } else if ( x == 9 ) // do B else // do C 9/18/06 CS150 Introduction to Computer Science 1
22
CS150 Introduction to Computer Science 1
Using nested ifs… Write a snippet of code that will: add y to x if x > y add x to y if y > x add 1 to x if x == y int x, y; if ( If ( x > y ) { x += y; } Else { if ( x < y ) y +=x; x += 1; 9/18/06 CS150 Introduction to Computer Science 1
23
Chained If/Else statements
if ( x > y ) { // do A } else if ( x == 9 ) // do B else if ( y > 1 ) // do C else 9/18/06 CS150 Introduction to Computer Science 1
24
CS150 Introduction to Computer Science 1
Using nested ifs… Write a snippet of code that will only do one of the following: add y to x if x == y add x to y if y > x add 1 to x if (2 * x) == y int x = 5, y = 10; if ( If ( x > y ) { x += y; } Else { if ( x < y ) y +=x; x += 1; 9/18/06 CS150 Introduction to Computer Science 1
25
CS150 Introduction to Computer Science 1
Exercise Write a snippet of code that will print the letter grade (A,B,C,D,F) of a student’s exam and set a bool flag to track if it is a passing grade. int examScore; // from 0 to 100 bool bPassingGrade; if ( 9/18/06 CS150 Introduction to Computer Science 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.