9/18/06 CS150 Introduction to Computer Science 1 1
The If/Else Statement, Boolean Flags, and Menus
Page 180
9/18/06 CS150 Introduction to Computer Science 1 2
Formally defined
What is an expression?
if( expression ){ statement 1; statement 2; . . . statement n;}
Just like a function, start at the top and execute in order tothe bottom
9/18/06 CS150 Introduction to Computer Science 1 3
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:o 6 * 2/3 = 4
C++:o 6.0 * 0.66666 =
o 6.0 * 0.66667 =
o 6.0 * 0.666666 =
o 6.0 * ( 2.0 / 3.0 ) =
9/18/06 CS150 Introduction to Computer Science 1 5
Example (page 180)
double result;
result = 6.0 * 0.666666;
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
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
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
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
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
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
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
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
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// I need to ...
}
9/18/06 CS150 Introduction to Computer Science 1 16
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
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
Practice
Are these two code snippets equivalent?
int x, y;if ( x > y ){
x += y;} if ( y < x){
y += x;}
int x, y; if ( x > y ) {
x += y; } else { y += x; }
9/18/06 CS150 Introduction to Computer Science 1 19
Nested Ifs
if ( x > y ) {}else{
if ( x == 9 ){}else {}
}
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
9/18/06 CS150 Introduction to Computer Science 1 20
Using nested ifs…
Write a snippet of code that will:o add y to x if x > y
o add x to y if y > x
o add 1 to x if x == y
int x, y;if (
9/18/06 CS150 Introduction to Computer Science 1 21
C++ Shortcut
if ( x > y ) {
// do A}else{
if ( x == 9 ){
// do B}else {
// 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
Using nested ifs…
Write a snippet of code that will:o add y to x if x > y
o add x to y if y > x
o add 1 to x if x == y
int x, y;if (
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
Using nested ifs…
Write a snippet of code that will only do one of the following:o add y to x if x == y
o add x to y if y > x
o add 1 to x if (2 * x) == y
int x = 5, y = 10;if (
9/18/06 CS150 Introduction to Computer Science 1 25
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 (