Control Structures Selection or Decision or Branching.

42
Control Structures Selection or Decision or Branching

Transcript of Control Structures Selection or Decision or Branching.

Page 1: Control Structures Selection or Decision or Branching.

Control Structures

Selection

or

Decision

or

Branching

Page 2: Control Structures Selection or Decision or Branching.

Decision

Which the statements are executed

Two types:simple alternative ifcompound alternative if...else

Page 3: Control Structures Selection or Decision or Branching.

Single Alternative Decision

An action is taken if the condition is true,

otherwise the control goes to the next

statement.

Page 4: Control Structures Selection or Decision or Branching.

Syntaxif (expression)

statementIf expression is true, statement is executed; otherwise statement is skipped.

no ;

* *

Single Alternative Decision

Example:if (stomach if (stomach == empty) empty)

eat a Snickers bar; eat a Snickers bar;

note: 2 == signs

Page 5: Control Structures Selection or Decision or Branching.

An expression is any combination of variables, constants, or function calls thatevaluate to a value.

ex. 5 x + y a = 3 + jN n++ f(12.3, a, “Yvonne”)

Syntaxif (expression)

statement

Single Alternative Decision

* *

Page 6: Control Structures Selection or Decision or Branching.

Syntaxif (expression)

statementIf expression is true, statement is executed; otherwise statement is skipped.

no ;

* *

Single Alternative Decision

Example:if (stomach if (stomach ==== empty) empty)

eat a Snickers bar; eat a Snickers bar; eat a marshmallow sunday; eat a marshmallow sunday;

note: 2 == signs

Page 7: Control Structures Selection or Decision or Branching.

Single Alternative Decision

Example:if (grade >= 90)if (grade >= 90) cout << “Congratulations!\n”; cout << “Congratulations!\n”;cout << “Your grade is “ << grade << “.\n";

Page 8: Control Structures Selection or Decision or Branching.

The Compound Statement

Syntaxif (expression){{ statement; statement; if (expression) { statement; statement; } }}

Example:if (u > v){{

a = 1;b = 2;

if ( u > z){

x =11;y = 12;

}}}

* * ** *

The compound statement is itself a statement.

Page 9: Control Structures Selection or Decision or Branching.

ifif Examples

Valid:Valid:

if (y != 0.0) z = x/y;

if (a < b && b < c) {

d = a + b + c; cout << "All OK\n";

}

* *

Not ValidNot Valid:

if b == a area = a * a;

if (a < b) && (b < c)if (a < b) ;

Valid But... Valid But... if (a < b) ;

Page 10: Control Structures Selection or Decision or Branching.

ifif Problems

Using Using == in place of in place of ====

What is the difference between these two?

if (toss == 7)if (toss == 7)

cout << “You win the bet.”;cout << “You win the bet.”;

if (toss = 7)if (toss = 7)cout << “You win the bet.”;cout << “You win the bet.”;

Page 11: Control Structures Selection or Decision or Branching.

Compounding 2 ifif Statementsif (j < k){ min = j;

cout << “the smaller number is “ << min;}if (j < k)

cout << "j is smaller than k\n";

More EfficientMore Efficient::if (j < k){ min = j;

cout << “the smaller number is “ << min;cout << "j is smaller than k\n"; } * *

Page 12: Control Structures Selection or Decision or Branching.

Double Alternative Decision

An action (or set of actions) is taken if the condition is true, another action (or set of actions) is taken if the condition is false, then the control goes to the next statement.

if ... elseif ... else is the typical double alternative.

* *

Page 13: Control Structures Selection or Decision or Branching.

The if-elseif-else Statement Syntax

if (expression)statement1

elsestatement2

If expression is nonzero then statement1 is executed and statement2 is skipped. If expression is zero statement1 is skipped and statement2 is executed.

Page 14: Control Structures Selection or Decision or Branching.

if ... elseif ... else Examples

if (stomach == empty)

{ eat a pizza;

eat a Snickers bar;}

else eat a salad;

if ( j < k ){ min = j;

k = k * 3;}else{

min = k;j = j * 3;

} * *

Page 15: Control Structures Selection or Decision or Branching.

Interactive Program Finding the minimum of three values

Page 16: Control Structures Selection or Decision or Branching.

Finding the Minimum of Three Values

* * *

int x, y, z, min;cout << “Input three integers: “;cin >> x >> y >> z;if (x < y)

min = x;else

min = y;

if (z < min)min = z;

cout << “The minimum value is “ << min << ‘\n’;

Page 17: Control Structures Selection or Decision or Branching.

Output:

Input three integers:

Interactive Program Finding the minimum of three values

* *

9 5 -12

The minimum value is -12_

Page 18: Control Structures Selection or Decision or Branching.

The if-elseif-else Statement Syntax

if (expression)

statement1else

statement2

if (a > b)if (a > b)max = a;max = a;

elseelsemax = b;max = b;

expression1 ? expression2 : expression3

max = (a > b) ? a : b;

* * *

Page 19: Control Structures Selection or Decision or Branching.

Nested ifif Statements

A nested if statement is an if statement that is included within another if statement.

Syntax

if (expression1)

{ if (expression2)

statement

}

Page 20: Control Structures Selection or Decision or Branching.

Nested ifif Example

if (number == secretnumber) cout << “You guessed it!”;if (number != secretnumber){ cout << “Sorry, that’s not the number.\n”;

if (number > secretnumber) cout << “You guessed too high.\n”;

else cout << “You guessed too low.\n”;

} * *

Page 21: Control Structures Selection or Decision or Branching.

2 statement1 3 else if (expression2) 4 statement2 5 . . .

6 else if (expressionN) 7 statementN 8 else 9 last statement10 next statement *

Chained if...elseif...else Example

Syntax 1 if (expression1)

Page 22: Control Structures Selection or Decision or Branching.

Chained if...elseif...else Example

if (total >=90)grade = ‘A’;

else if (total >= 80)grade = ‘B’;

else if (total >= 70)grade = ‘C’;

else if (total >= 60)grade = ‘D’;

elsegrade = ‘E’;

next statement* *

Page 23: Control Structures Selection or Decision or Branching.

The Dangling elseelseif (avg >= 60.0)

if (avg < 70.0)cout << “Passing, but marginal”; elsecout << “Failing”;

* *

if (avg >= 60.0){

if (avg < 70.0)cout << “Passing, but marginal”;

}else

cout << “Failing”;

Page 24: Control Structures Selection or Decision or Branching.

The Dangling elseelse

if (avg >= 60.0){

if (avg < 70.0)cout << “Passing, but marginal”;

}else

cout << “Failing”;

Page 25: Control Structures Selection or Decision or Branching.

AND vs. OR

if( (rel == 'S') && (rel == 'M') && (rel == 'F') )cout << "\nImmediate family.\n";

if( (rel != 'S') && (rel != 'M') && (rel != 'F') ){

cout << "\nNot immediate family,\n";cout << " but a close relation.\n";

}

Page 26: Control Structures Selection or Decision or Branching.

Random Numbers#include<stdlib.h>#include<stdlib.h> // defines rand() & srand()// defines rand() & srand()

#include<time.h>#include<time.h> // defines time()// defines time()

in main():

srand(time(NULL));srand(time(NULL));

num1 = 1 + rand() % 3;num1 = 1 + rand() % 3;

num1 = 1 + rand() % 6;num1 = 1 + rand() % 6;

num1 = 6 + rand() % 5;num1 = 6 + rand() % 5;

1, 2, 31, 2, 3, 4, 5, 66, 7, 8, 9, 10

how many numbers

* * * starting number

Page 27: Control Structures Selection or Decision or Branching.

The switchswitch Statement

Similar to if statements

Can list any number of branches

Used in place of nested if statements

Avoids confusion of deeply nested ifs

Page 28: Control Structures Selection or Decision or Branching.

The switchswitch StatementSyntax switch (expression)

{case value1:

statement1;break;

case value2:statement2;break;

case valuen:

statementn;break;

default:statement;

}

no no ;;useuse : :

* *

Page 29: Control Structures Selection or Decision or Branching.

The switchswitch StatementSyntax switchswitch (expression)

{case value1:

statement1;breakbreak;

case value2:statement2;breakbreak;

case valuen:

statementn;breakbreak;

defaultdefault:statement;

}

no no ;;useuse : :

Page 30: Control Structures Selection or Decision or Branching.

The switchswitch Statementswitch (let_grd){

case ‘A’:cout << “Grade is between 90 & 100”;break;

case ‘B’:cout << “Grade is between 80 & 89”;break;

case ‘C’:cout << “Grade is between 70 & 79”;break;

cont.

Page 31: Control Structures Selection or Decision or Branching.

The switchswitch Statementcase ‘D’:

cout << “Grade is between 60 & 69”;break;

case ‘E’:cout << “Grade is between 0 & 59”;break;

default:cout << “You entered an invalid grade.”;

}next statementnext statement

Page 32: Control Structures Selection or Decision or Branching.

The switchswitch Statementswitch (let_grd){

case ‘A’:cout << “Grade is between 90 & 100”;break;

case ‘B’:cout << “Grade is between 80 & 89”;break;

case ‘C’:cout << “Grade is between 70 & 79”;break;

case ‘D’:cout << “Grade is between 60 & 69”;break;

case ‘E’:cout << “Grade is between 0 & 59”;break;

default:cout << “You entered an invalid grade.”;

}

Page 33: Control Structures Selection or Decision or Branching.

The breakbreak Statementswitch (let_grd){

case ‘A’:cout << “Grade is between 90 & 100”;break;

case ‘B’:cout << “Grade is between 80 & 89”;break;

case ‘C’:cout << “Grade is between 70 & 79”;break;

case ‘D’:cout << “Grade is between 60 & 69”;break;

case ‘E’:cout << “Grade is between 0 & 59”;break;

default:cout << “You entered an invalid grade.”;

}

Page 34: Control Structures Selection or Decision or Branching.

The breakbreak Statement

switch (let_grd){

case ‘A’:case ‘B’: cout << “Good Work”;

break;case ‘C’: cout << “Average Work”;

break;case ‘D’:case ‘E’: cout << “Poor Work”;

}

Page 35: Control Structures Selection or Decision or Branching.

The breakbreak Statement

switch (let_grd){

case ‘A’:case ‘a’:case ‘B’:case ‘b’: cout << “Good Work”;

break;case ‘C’:case ‘c’: cout << “Average Work”;

break;etc.

Page 36: Control Structures Selection or Decision or Branching.

The switchswitch Statement

* * * * Menu * * * *

1. NY Yankees

2. Orioles

3. Dodgers

Choose either 1, 2, or 3:

Page 37: Control Structures Selection or Decision or Branching.

The switchswitch Statement

switch (choice){ case 1:

cout << “World Champs”; case 2:

cout << “Good Guys”; case 3:

cout << “Da Bums”;}

What will be

the output?

Page 38: Control Structures Selection or Decision or Branching.

The switchswitch Statement

What will be the output when the user enters

1 World ChampsGood GuysDa Bums

2 Good GuysDa Bums

3 Da Bums

* * * ** * * *

4 skips the switch

Page 39: Control Structures Selection or Decision or Branching.

The switchswitch Statement

*

switch (choice){ case 1:

cout << “World Champs”; case 2:

cout << “Good Guys”; case 3:

cout << “Da Bums”;}

switch (choice){ case 1:

cout << “World Champs”; break;

case 2: cout << “Good Guys”; break;

case 3: cout << “Da Bums”; break;

default: cout << “Enter a 1, 2, or 3”;

}

Page 40: Control Structures Selection or Decision or Branching.

Common Errors

Using = in place of ==Using = in place of ==

Improper braces in nested ifsImproper braces in nested ifs

Too deeply nested ifsToo deeply nested ifs

Missing Missing breakbreak statements in switch the statements in switch the statementstatement

Copyright © 1999 by Freedom TLC, Inc.

Page 41: Control Structures Selection or Decision or Branching.

DebuggingDebugging

Syntax errors vs. Logic error

Prevention - plan first!Valuation tablesDisplay values

C++ Debugger

Copyright © 1999 by Freedom TLC, Inc.

Page 42: Control Structures Selection or Decision or Branching.

““I discovered I always have I discovered I always have

choices, and sometimes it’s choices, and sometimes it’s

only a choice of attitude”only a choice of attitude”

Judith M. KnowltonJudith M. Knowlton

Copyright © 1999 by Freedom TLC, Inc.