Selection 1
Selection 1
Programming Fundamentals 1
Control Structures
(Selections)
Topics to cover here:
One-Way Selection
Two-Way Selection
Multi-Way Selection
Nested Structures
Programming Fundamentals 3
One-Way Selection
:Syntax
Condition false
true
statements
continue
Programming Fundamentals 5
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 6
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 7
Example 1 .. cont.
Programming Fundamentals 8
Example 1: C++ Program
#include <iostream>
using namespace std;
void main ( )
{
int n ;
cin >> n ;
if (n < 50)
cout << “The double value is “ << n * 2 << endl ;
cout << “ Finished “ << endl;
}
Programming Fundamentals 9
Two-Way Selection
This statement chooses- statements to run- from two sets of choices.
:Syntax
Programming
Programming
Funamental
Fundamentals
slides 10
Two-Way Selection .. cont.
Programming Fundamentals 11
Two-Way Selection .. cont.
false true
condition
statements2 statements1
continue
Programming Fundamentals 12
Two-Way Selection .. Examples
Example 1:
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 13
Example 1 .. 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 14
Example 1 .. cont.
Programming Fundamentals 15
Example 1: C++ Program
#include <iostream>
using namespace std;
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 16
Example 2
Write an algorithm that takes prices of two items, if there total
is over 100 JDs, calculate a discount of 40% and tax of 16%.
Otherwise, calculate a discount of 20% and tax of 6%. Then
calculate the total price as: Total + tax – Discount.
1- Analysis stage:
Problem Input:
- Two prices, p1 and p2
Problem Output:
- The total price of the two items
Criteria
check whither the total prices is over 100 or not.
Programming Fundamentals 17
Example 2 .. cont.
2- Algorithm Design
ALGORITHM Total_Price
INPUT p1, p2
sub_total p1 + p2
IF (sub_total > 100) THEN
Discount sub_total * 0.4
Tax sub_total * 0.16
ELSE
Discount sub_total * 0.2
Tax sub_total * 0.06
END IF
Total sub_total + Tax - Discount
END Total_Price
Programming Fundamentals 18
Example 2: C++ Program
#include <iostream>
using namespace std;
void main ( )
{ float p1, p2, sub_total, discount, tax, total;
cin >> p1 >> p2;
sub_total = p1 + p2;
if ( sub_total > 100)
{
discount = sub_total * 0.4;
tax = sub_total * 0.16;
}
else
{
discount = sub_total * 0.2;
tax = sub_total * 0.06;
}
}
Programming Fundamentals 19
Multi Way Selection
Programming Fundamentals 20
Multi Way Selection by Nested IF Structure
Programming Fundamentals 21
.Multi Way Selection by Nested If Structure .. Cont
:Syntax
In pseudo code In C++
IF (condition1) THEN if (condition1)
Statements1 Statements1;
ELSE IF (condition2) THEN else if (condition2)
Statements2 Statements2 ;
ELSE IF (Condition3) THEN else if(Condition3)
Statements3 Statements3;
ELSE IF (Condition4) else if(Condition4)
THEN Statements4;
Statements4
END IF
END IF
END IF
True
Condition1 Statements1
False
True
Condition2 Statements2
Rest of
False algorithm
True
Condition3 Statements3
False
Statements4
Programming Fundamentals 23
Multi Way Selection by Nested If Structure .. Examples
Example 1:
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 24
Example 1 .. Cont.
1- Analysis stage:
Problem Input:
- student’s mark, mark
Problem Output:
- grade
Criteria
- according to the previous grade table
Programming Fundamentals 25
Example 1 .. 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 26
Example 1: C++ Program
#include <iostream>
using namespace std;
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 << “B” << endl ;
else if ( mark >= 70 )
cout << “C” << endl ;
else if ( mark >= 60 )
cout << “D” << endl ;
else cout << “E” << endl ;
}
Programming Fundamentals 27
Example 2: : Read three numbers to print the
smallest one.
#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 28
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 29
:Note
In some cases, you may need to use a
variable as the condition of an if
statement. But, how this variable will be
interpreted into the result of the condition?
We can use: If(x)
Programming Fundamentals 30
Example:
{
int x;
cin>> x;
if (x)
cout << “x is any number but zero”;
else
cout << “ x is zero”;
}
Notes: - a value of 0, is considered false in this case, while all other values, either positive or
negative, are considered true.
- The same applies for float variables.
- Character variables always return true.
Programming Fundamentals 31
++Switch Statement in C
++Switch Statement in C
Syntax
switch (selector)
{ case L1: statements1; break;
case L2: statements2; break;
…
default: statements_n;
}
Semantics:
x = x+1; x += 1 x++
x = x-1; x -= 1 x--
41 Programming Fundamentals
Example
Compute the following expression where:
z=3, x=2, y=2
Z+= ++x - y--;
Solution:
Programming Fundamentals 42