36
Selection (decision) control structure Learning objective Learning Objectives . . .. . . . . . . . . . . . . . . . .. . . . . . . page 2 If Statements . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . page 3

Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Embed Size (px)

Citation preview

Page 1: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureLearning objective

• Learning Objectives . . .. . . . . . . . . . . . . . . . .. . . . . . . page 2• If Statements . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . page 3

Page 2: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureMenu

Upon completion of this module, the student will be able to:

1. Understand, discuss, and trace the flow of control in single and nested if statements given the flow chart or pseudocode.

2. Write an If-Then-Else statement that will perform a given task.

3. Write an If-Then statement that will perform a given task.

4. Write a set of nested If statements that will perform a given task.

Page 3: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

The main selection control structure or branching statement is the IF-then-Else statement.

Branching : is the selections of one or two alternate paths for execution based upon the evaluation of a conditional expression.

The selection structure alter the control (or flow) of the program based on the value that is returned when a logical expression is evaluated. The returned value is known as the Boolean value. These values return either true or false.

A Boolean Value: is an expression that is either true or false (see conditional expressions)

When a single alternative selection structure returns the value of true, the actions on the true branch of the structure are executed. If the expression returns false, no actions are executed and the program control continues to the next executable statement structure in the program.

Page 4: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

An example of a single alternative selection is a program that is designed to allow the user to input two numbers. The program will then display the difference of the two numbers, Only if the first number is greater than the second number. The flow chart for this program would look like this :

This statement is referred to as a dual alternative statement because there are two possible courses of action. The actions chosen depends on the results of evaluating a Boolean expression. A true conditions one course of action; a false condition contains another course of action.

Dual alternative : The if, then, else statement is a dual alternative statement because there is an action to be taken on the true branch and on the false branch. Example:

A Boolean expression : is an expression that is either

true or false (see conditional expressions)

Page 5: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

The format of the IF-Then-Else statement is :

if (Boolean expression to be evaluated) then

(statement or statements to be executed if the expression is true) else

(statement or statements to be evaluated is the statement is false).

Note some programming language do not use the word then:

if (condition)

(true alternative)

else

(false alternative)

Example: if (the sun is shining) then

I will go fishing

else

I will play computer games.

Are there two alternatives? (see flow chart for example).

Page 6: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

A simple version of the IF statement is a single alternative statement. This is the IF-Then statement with no else (or false) alternative.

Example : If (I have a fishing pole) thenI am going fishing.

Note this statement does not tell you what to do if the condition is false.

Flow chart example

Another example: If revenue > cost) then Print: Profit (revenue cost).

Note: We made a profit.

C++ code example : If (revenue > cost) cout < < “Profit is $ “ < < revenue –

cost;

Page 7: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

If more than two alternatives are needed it is necessary to use nested if statements. Two or more is statements are combined so that then or else part of the first if statements, is an if statement and so on. The form looks like this

Nested IF: A nested if is an If statement in which the true or false alternative is itself an If statement.

Page 8: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

If ( ) then

.

.

.

Else

If( ) then

.

.

.

Else

.

.

.

Executions will continue through the nested If statement until a true condition is found, and its corresponding statement is executed, then control transfers to the statement following the nested statement.

Page 9: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

Control exists the nested if when a true condition is executed.

Example : A classic example of a nested if statement is to determine a students letter grade given the numerical grade.

See the flow chart

Page 10: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

See the Pseudocode

Notice: when the correct letter grade is determined the nested If

statement is excited at that point. Notice: The grade F (If (grade > 49 ) ) does not require evaluation. If the

grade is not greater than 59 ( ( grade > 59 ) Evaluates to false. The default grade of F is assigned.

Page 11: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statementsThis task can also be completed by using a sequence of its statements executed one after the other.

If (grade > 89) thenletter grade = A

EndifIf (grade > 79) thenletter grade = B

EndifIf (grade > 69) thenletter grade = C

EndifIf (grade > 59) thenletter grade = D

Endif

Elseletter grade = F

Endif

This method seems to be less efficient since each if statement is always executed. There is no early out when a true condition is found.

Page 12: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Introduction to condition statementsQuick Check

When a dual alternative statement is needed the program uses an:

If Then Else Nested IfIf Then

None of these are correct

Page 13: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 14: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 15: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Introduction to condition statementsQuick Check

Consider the following statement:

If (x = 8) then

Print x

Else

Print “x is too large”

What is printed when x = 9?

9 Nothing is printedX is too large

Page 16: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 17: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 18: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Introduction to condition statementsQuick Check

When a single alternative is needed the best way would be to use an:

If Then Else Nested IfIf Then

None of these are correct

Page 19: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 20: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 21: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Introduction to condition statementsQuick Check

When more than two alternatives are needed the best way would be to us a:

If then Else Nested IfIf Then

None of these are correct

Page 22: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 23: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 24: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureQuick Check

An If statement is some times called a Branching statement?

TRUE FALSE OR

Page 25: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 26: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 27: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureQuick Check

All component statements of an If statement must always be executed.

TRUE FALSE OR

Page 28: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 29: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 30: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureQuick Check

One type of selection statement is an If Then statement.

TRUE FALSE OR

Page 31: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 32: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 33: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureQuick Check

One alternative of the If Then statement is always executed.

TRUE FALSE OR

Page 34: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

TRY AGAIN!

Page 35: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

CORRECT!

Page 36: Selection (decision) control structure Learning objective Learning Objectives........................... page 2page 2 If Statements

Selection (decision) control structureiF statements

You have completed the Selection decision control structure lesson.