Conditional Execution

15
CGS 3460 Conditional Execution

description

Conditional Execution. If statement. So far, we have had sequential execution (everything is executed) Offers little flexibility May want code executed only when certain condition is met Conditional Execution Execute a statement or statements only if a certain condition is met Or not met. - PowerPoint PPT Presentation

Transcript of Conditional Execution

Page 1: Conditional Execution

     

CGS 3460

Conditional ExecutionConditional Execution

Page 2: Conditional Execution

     

CGS 3460

If statementIf statement So far, we have had sequential execution (everything is

executed) Offers little flexibility May want code executed only when certain condition is

met Conditional Execution

• Execute a statement or statements only if a certain condition is met• Or not met

Page 3: Conditional Execution

     

CGS 3460

If statementIf statement Consists of keyword if

Followed by condition in parenthesis Followed by the body of the if statement

• This is what is executed if the condition evaluates to true• Body can consist of multiple statements if they are enclosed with { }

Operator Meaning Example

== Equal to var == 10

!= Not equal to var != 10

< Less than var < 10

<= Less than or equal to var <= 10

> Greater than var > 10

>= Greater than or equal to var >= 10

Page 4: Conditional Execution

     

CGS 3460

The if StatementThe if Statement Format

if ( condition )program statement;

orif ( condition ){

program statement(s);}

Condition satisfied?

Program statement

Yes

No

Page 5: Conditional Execution

     

CGS 3460

ExampleExample Calculating the absolute value of an integer

number < 0

number = - number;

Yes

No

printf(“type in your number: “);scanf(“%i”, &number);

if ( number < 0 )number = -number;

printf(“\nThe absolute value is %i\n”, number);

Page 6: Conditional Execution

     

CGS 3460

NotesNotes Only multiply by -1 if the number is less than 0 The if statement doesn’t end in semi-colon

The body does, but the if statement doesn’t The if statement begins and ends with brace ( {…} ) The body has one or more statements within the braces ( {…} )

• Can be considered a compound statement since the body can have multiple statements

Page 7: Conditional Execution

     

CGS 3460

If - elseIf - else Sometimes have two sets of statements and only want to

execute one based on a conditional test Definitely want to execute one or the other, but not both If the body of the if statement is skipped (due to the

condition evaluating to false) the body of the else statement is executed

Consists of key word if Followed by condition in parenthesis Followed by the body of the if

Followed by key word else Followed by condition in parenthesis Followed by the body of the else

Page 8: Conditional Execution

     

CGS 3460

The if-else StatementThe if-else Statement Format

if ( condition )program statement 1;

else

program statement 2;

Orif ( condition )

{

program statement(s) 1;

}

else

{

program statement(s) 2;

}

Condition satisfied?

Program statement 1

Yes

No

Program statement 2

Page 9: Conditional Execution

     

CGS 3460

The if-else Statement - ExampleThe if-else Statement - ExampleAre you a gator?

answer == ‘y’

“Go Gators!”

Yes

No

“Gator Bait"

printf(“Are you a gator? (y)es, (n)o “);scanf(“%c”, &answer);

if ( answer == ‘y’)printf(“Go Gators!!!\n”);

elseprintf(“Then you’re gator bait”);

Page 10: Conditional Execution

     

CGS 3460

ExampleExample

printf(“Are you a gator? (y)es, (n)o “);scanf(“%c”, &answer);

if ( answer == ‘y’)printf(“Go Gators!!!\n”);

elseprintf(“Then you’re gator bait”);

Are you a gator? (y)es, (n)oyGo Gators!!!

Are you a gator? (y)es, (n)onThen you’re gator bait!

Are you a gator? (y)es, (n)oYThen you’re gator bait!

Page 11: Conditional Execution

     

CGS 3460

else ifelse if Seen two statements to facilitate conditional execution

if statement• selectively executes a set of statments based on a conditional test.

if..else• allows us to make one of two choices• if the condition is true, the statements in the body of if is executed,• otherwise the statements in the body of the else part are

automatically executed.

Sometimes need to choose between multiple alternatives if … else if … else if … else if … else

Page 12: Conditional Execution

     

CGS 3460

The else if StatementThe else if Statement Format

if ( condition1 )

program statement 1;

else if (condition2)

program statement 2;

Flow

Condition 1 satisfied?

Program statement 1

Yes

No

Condition 2 satisfied?

No

Program statement 2

Yes

Page 13: Conditional Execution

     

CGS 3460

Consider assigning letter gradesConsider assigning letter grades Have a floating point number between 0 and 100.

Want to assign corresponding letter grade to number grade, x• A – if 90 ≤ x ≤100• B – if 80 ≤ x < 90• C – if 70 ≤ x < 80• D – if 60 ≤ x < 70• E – x < 60

Page 14: Conditional Execution

     

CGS 3460

An exampleAn exampleX ≥ 90No

Yes

letGrade = ‘A’ X ≥ 80

Yes

letGrade = ‘B’ X ≥ 70

Yes

letGrade = ‘C’ X ≥ 60

Yes

letGrade = ‘D’

letGrade = ‘E’

No

No

No

Page 15: Conditional Execution

     

CGS 3460

Assigning Letter GradesAssigning Letter Gradesif ( x >= 90 )

letGrade = ‘A’;

else if (x >= 80)letGrade = ‘B’;

else if (x >= 70)letGrade = ‘C’;

else if (x >= 60)letGrade = ‘D’;

elseletGrade = ‘E’