END IF

advertisement
Control Structures
(Selections)
Topics to cover here:
Selection statements in the algorithmic language:

One-Way Selection
Two-Way Selection
Multi-Way Selection
Nested Structures

Selection statements in C++ language



Programming Fundamentals by
Dr. Nadia Y. Yousif
1
Selections .. cont.

The selection statement allows to choose a set of
statements for execution.

The selection depends on the validity of a condition
when the program is executed.

The condition is a logical expression that has values
as true or false.
Programming Fundamentals by
Dr. Nadia Y. Yousif
2
One-Way Selection
IF .. END IF statement (in pseudo code):
 Syntax:
IF (condition) THEN
statements
END IF

-
-
The semantics (execution) of this statement:
If the value of the “condition” is true, the statements
between IF .. END IF are executed and the execution
continues to the statement after END IF.
If the value of the “condition” is false, the statements
between IF .. END IF are ignored and the execution
continues from the statement that follows END IF.
Programming Fundamentals by
Dr. Nadia Y. Yousif
3
One-Way Selection
The following figure shows the execution of this
selection statement.
Condition
false
true
statements
continue
Programming Fundamentals by
Dr. Nadia Y. Yousif
4
One-Way Selection .. Examples
Example 1:
Write an algorithm that takes an integer and prints its double
value if it is less than 50.

First, we have to analyze the problem to understand what is its
requirements and how to solve it.
1- Analysis stage:

Problem Input:
- An integer, say n

Problem Output:
- The double value of n

Criteria
if n < 50, print its double value
Programming Fundamentals by
Dr. Nadia Y. Yousif
5
Example 1 .. cont.
2- Algorithm Design
ALGORITHM Double
INPUT n
IF ( n < 50 ) THEN
OUTPUT “The double value is “ , n * 2
END IF
OUTPUT “ Finished”
END
Programming Fundamentals by
Dr. Nadia Y. Yousif
6
Example 1 .. cont.
3- Testing the algorithm
n (n < 50)
12 --true
The output:
The double value is 24
Finished
Programming Fundamentals by
Dr. Nadia Y. Yousif
7
Selection Statements in C++
If
Statement in C++
1- One way selection:
Syntax:
if (logical expression)
statements;
Semantics:
These statements have the same meaning as in the algorithmic
language.
Programming Fundamentals by
Dr. Nadia Y. Yousif
8
Example 1: C++ Program
// File: double.cpp
/*The program corresponds to Example 1 on slide 5. It takes an
integer and prints its double value if it is less than 50 */
#include <iostream.h>
void main ( )
{ int n ;
cin >> n ;
if (n < 50)
cout << “The double value is “ << n * 2 << endl ;
cout << “ Finished “ << endl;
}
Programming Fundamentals by
Dr. Nadia Y. Yousif
9
Example 2
Write an algorithm that takes two integers and prints the smallest
number. Use only one output statement.
1- Analysis stage:
 Problem Input:
- Two integers, say num1 and num2

Problem Output:
- The smaller number
Criteria
Print the smaller number using only one output statement
Programming Fundamentals by
Dr. Nadia Y. Yousif
10
Example 2 .. cont.
2- Algorithm Design
ALGORITHM Smallest
INPUT num1, num2
temp  num1
IF ( num2 < num1 ) THEN
temp  num2
END IF
OUTPUT “The smallest number is “ , temp
END Smallest
Programming Fundamentals by
Dr. Nadia Y. Yousif
11
Example 2 .. cont.
3- Testing the algorithm
num1 num2
9
3
temp
--9
(num2<num1)
true
3
The output:
The smallest number is 3
Programming Fundamentals by
Dr. Nadia Y. Yousif
12
Two-Way Selection
IF .. THEN .. ELSE statement (in pseudo code):
This statement chooses- statements to run- from two
sets of choices.
Syntax:
IF (condition) THEN
statements1
ELSE
statements2
END IF
Programming Fundamentals by
Dr. Nadia Y. Yousif
13
Two-Way Selection .. cont.
The semantics (execution) of this statement:
- If the value of the “condition” is true, the statements
after THEN are executed and the execution continues
to the statement after END IF.
- If the value of the “condition” is false, the statements
after ELSE are executed and the execution continues
from the statement that follows END IF.

-
The following figure shows this execution:
Programming Fundamentals by
Dr. Nadia Y. Yousif
14
Two-Way Selection .. cont.
false
condition
statements2
true
statements1
continue
Programming Fundamentals by
Dr. Nadia Y. Yousif
15
Two-Way Selection .. Examples

Example 3:
Write an algorithm that takes two integers and prints the
smallest number with appropriate message.
1- Analysis stage:
 Problem Input:
- Two integers, say num1 and num2

Problem Output:
- The smaller number
Programming Fundamentals by
Dr. Nadia Y. Yousif
16
Example 3 .. cont.
2- Algorithm Design
ALGORITHM Smaller
INPUT num1, num2
IF ( num1 < num2 ) THEN
OUTPUT “The smaller number is “ , num1
ELSE
OUTPUT “The smaller number is “ , num2
END IF
END Smaller
Programming Fundamentals by
Dr. Nadia Y. Yousif
17
Example 3 .. cont.
3- Testing the algorithm
num1 num2
9
3
(num2<num1)
--true
The output:
The smaller number is 3
Programming Fundamentals by
Dr. Nadia Y. Yousif
18
Selection Statements in C++
2- Two way selection:
Syntax:
if (logical expression)
statements1;
else
statements2;
Programming Fundamentals by
Dr. Nadia Y. Yousif
19
Example 2: C++ Program
// File: Smaller.cpp
/*The program corresponds to Example 3 on slide 14. It takes
two integers and prints the smaller value using one output
statement*/
#include <iostream.h>
void main ( )
{ int num1, num2 ;
cin >> num1 >> num2 ;
if ( num1 < num2 )
cout << “The smaller value is “ << num1 << endl ;
else
cout << “The smaller value is “ << num12 << endl ;
}
Programming Fundamentals by
Dr. Nadia Y. Yousif
20
Multi Way Selection


(a)
(b)
You can choose statement(s) to run from
many sets of choices.
There are two cases for this:
Multi way selection by nested IF
structure
Multi way selection by SWITCH structure
Programming Fundamentals by
Dr. Nadia Y. Yousif
21
Multi Way Selection by Nested IF Structure

The structure that contains another
structure of the same type is called a
nested structure.

In the Nested IF structure, the statements
that exists between IF and ELSE or
between IF and END IF can contain IF
statement.
Programming Fundamentals by
Dr. Nadia Y. Yousif
22
Multi Way Selection by Nested If Structure .. Cont.

Syntax of one possible structure:
IF (condition1) THEN
Statements1
ELSE IF (condition2) THEN
Statements2
ELSE IF (Condition3) THEN
Statements3
ELSE IF (Condition4) THEN
Statements4
END IF
END IF
END IF
Note: The nest can be to many levels.
The following figure shows the execution of this structure.
Programming Fundamentals by
Dr. Nadia Y. Yousif
23
Multi Way Selection by Nested If Structure .. Cont.
True
Statements1
Condition1
False
True
Statements2
Condition2
Rest of
algorithm
False
True
Condition3
Statements3
False
Statements4
Programming Fundamentals by
Dr. Nadia Y. Yousif
24
Multi Way Selection by Nested If Structure .. Examples

Example 4:
Write an algorithm that inputs a student mark and outputs the
corresponding grade, where grades are as follows:
mark
grade
90-100
A
80-89
B
70-79
C
60-69
D
< 60
E
Programming Fundamentals by
Dr. Nadia Y. Yousif
25
Example 4 .. Cont.
1- Analysis stage:
 Problem Input:
- student’s mark, mark

Problem Output:
- grade

Criteria
- according to the previous grade table
Programming Fundamentals by
Dr. Nadia Y. Yousif
26
Example 4 .. Cont.
2- Algorithm Design
ALGORITHM Grades
INPUT mark
IF ( mark < 0 OR mark > 100 ) THEN
OUTPUT “ Mark out of range”
ELSE IF ( mark  90 AND mark  100 ) THEN
OUTPUT “A”
ELSE IF ( mark  80 ) THEN
OUTPUT “B”
ELSE IF ( mark  70 ) THEN
OUTPUT “C”
ELSE IF ( mark  60 ) THEN
OUTPUT “D”
ELSE
OUTPUT “E”
END IF
END IF
END IF
END IF
END IF
END Grades
Programming Fundamentals by
Dr. Nadia Y. Yousif
27
Example 3: C++ Program
// File: Grades.cpp
/*The program corresponds to Example 4 on slide 21. It reads a student’s mark
and prints the corresponding grade */
#include <iostream.h>
void main ( )
{ int mark ;
cin >> mark;
if ( mark < 0 || mark > 100 )
cout << “ Mark out of range” << endl;
else if ( mark >= 90 )
cout << “A” << endl ;
else if ( mark >= 80 )
cout << “A” << endl ;
else if ( mark >= 70 )
cout << “C” << endl ;
else if ( mark >= 60 )
cout << “D” << endl ;
else cout << “E” << endl ;
Programming Fundamentals by
}
28
Dr. Nadia Y. Yousif
Comparison of Nested IF Structure and a Sequence of IF Structures


(a)
Some times the nested IF structure is a good solution for a
given problem than a sequence of IF structures.
Consider the following example:
Nested case:
IF ( x > 0 ) THEN
pos_count  pos_count + 1
ELSE IF ( x < 0 ) THEN
neg_count  neg_count + 1
ELSE
zero_count  zero_count + 1
END IF
END IF
Programming Fundamentals by
Dr. Nadia Y. Yousif
29
Comparison of Nested IF Structure and a Sequence of IF Structures
(b) Sequence case:
IF ( x > 0 ) THEN
pos_count  pos_count + 1
END IF
IF ( x < 0 ) THEN
neg_count  neg_count + 1
END IF
IF ( x = 0 ) THEN
zero_count  zero_count + 1
END IF
Programming Fundamentals by
Dr. Nadia Y. Yousif
30
Comparison of Nested IF Structure and a Sequence of IF Structures

In the previous example, only one statement should be
executed for any value of x.

In the sequence case, the sequence does not show
clearly that exactly one of the three statements is
executed for any value of x.

In the nested case, only one statement is executed
exactly. Therefore, it is better than the sequence case.
Programming Fundamentals by
Dr. Nadia Y. Yousif
31
Problem : Read three numbers to print the
smallest one.
Program
#include <iostream.h>
void main() {
int a, b, c;
cout<<"\nPlease Enter three numbers:";
cin>>a>>b>>c;
cout<<"\nMin= ";
if ((a < b) && (a < c))
cout<<a;
if ((b < a) && (b < c))
cout<<b;
if ((c < a) && (c < b))
cout<<c;
cout<<endl;
Programming Fundamentals by
}
Dr. Nadia Y. Yousif
32
Program2 (nested if)
#include <iostream.h>
void main() {
int a, b, c;
cout<<"\nPlease Enter three numbers:";
cin>>a>>b>>c;
cout<<"\nMin= ";
if (a < b)
if (a < c)
cout<<a;
else
cout<<c;
else
if (b < c)
cout<<b;
else
cout<<c;
cout<<endl;
}
Programming Fundamentals by
Dr. Nadia Y. Yousif
33
Download