1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus...

25
9/18/0 6 CS150 Introduction to Computer 1 The If/Else Statement, Boolean Flags, and Menus Page 180
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus...

Page 1: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

9/18/06 CS150 Introduction to Computer Science 1 1

The If/Else Statement, Boolean Flags, and Menus

Page 180

Page 2: 1 9/18/06CS150 Introduction to Computer Science 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

Page 3: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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;}

Page 4: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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 ) =

Page 5: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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;

Page 6: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

9/18/06 CS150 Introduction to Computer Science 1 6

Example

Page 7: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

9/18/06 CS150 Introduction to Computer Science 1 7

Floating Points in Relational Operators

How can we get around this problem?

Page 8: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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?

Page 9: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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

Page 10: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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}

Page 11: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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.

Page 12: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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

Page 13: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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

Page 14: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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!

Page 15: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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 ...

}

Page 16: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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!

Page 17: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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;}

Page 18: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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; }

Page 19: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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

Page 20: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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 (

Page 21: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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}

Page 22: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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 (

Page 23: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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{}

Page 24: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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 (

Page 25: 1 9/18/06CS150 Introduction to Computer Science 1 The If/Else Statement, Boolean Flags, and Menus Page 180.

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 (