Summary:
• Last time: Variable, Building blocks of Algorithm: Sequence
• Conditional statements
o Motivations
o Systax and example1: ageMarks
o Relational operators
o Handling all cases
o Example2: age comparison
o Example 3: shopeKeeper
o Logical operator
1.
2. Conditional statements
a. Motivation
o Program to take 3 test score of a student and if avg > 95 display congrats
message
• Syntax / standards for algorithm
1. condition
If (condition)
{
Statement or group of statement
}
Example 1:
If (averAge is greater than 95 ) => instead of English like statement use relational operators
{
output "Ali can be a member of basketball team"
}
If (aliHeight > 6) // no space in variable name, lower camel case
{
output "Ali can be a member of basketball team"
}
Detail Exmple 1
Sample Execution:
Please enter the score of first quiz: 100
Please enter the score of first quiz: 98
Please enter the score of first quiz: 95
Averge Score: 97
Excellent
Solution:
start
integer quiz1, quiz2, quiz3, total, avgMarks
output "Please enter the score of first quiz: "
input quiz1
output "Please enter the score of first quiz: "
input quiz2
output "Please enter the score of first quiz: "
input quiz3
total = quiz1 + quiz2 + quiz3
avgMarks = total / 3
if (avgMarks > 95)
{
output "Excellent";
}
end
• Example explanation
1. integer division truncate the decimal part
2. Relational operator > , other operators are <,>= , <=, != , == are the binary operators
3. Difference between = and == operators
Modify the above algorithm to handle if avg marks greater than 95 and less than 95 cases.
3. If/else structure
a. Multiple if statement vs if else
i. Only If statements
if (avgMarks > 95)
{
output "You won! Congrats on getting the prize.";
}
if (avgMarks <= 95)
{
output "Good luck next time!"
}
ii. If else structure
if (avgMarks > 95)
{
output "You won! Congrats on getting the prize.";
}
else
{
output "Good luck next time!"
}
Programming Error for not handling all possible cases:
Algorithmic error or oversight in an "if" case could result in the loss of many lives. On April 2,
1982, a war unfolded between Argentine and British forces on the Falkland Islands. Argentina
procured weapons from Western countries like France. When Argentina fired a missile at the
British forces, the expensive British antimissile system identified the incoming missile but did not
take any action due to the friendly relations with France, the missile's manufacturer. The failure to
check the missile's trajectory resulted in significant loss of life, highlighting a programming error
in the trajectory-checking aspect of the antimissile system
Example 2: AGE COMPARISION
I) Multiple if statements
real ahmadAge, moosaAge
output "Enter the age of Ali"
input ahmadAge
output "Enter the age of Moosa”
input moosaAge
if (ahmadAge >= moosaAge) # if statement is only executed if the condition is true
{
output "Ahmad is older than Moosa or both are of same age”
}
if (ahmadAge < moosaAge)
{
output "Ahmad is younger than Moosa”
Ii) if else statement
real ahmadAge, moosaAge
output "Enter the age of Ali"
input ahmadAge
output "Enter the age of Moosa”
input moosaAge
if (ahmadAge >= moosaAge) # if statement is only executed if the condition is true
{
output "Ahmad is older than Moosa or both are of same age”
}
else (ahmadAge < moosaAge)
{
output "Ahmad is younger than Moosa”
Exmaple3 (Home work): A shopkeeper announces a package for customers that he will give 10
% discount on all bills and if a bill amount is greater than 5000 then a discount of 15 %(15.0/100
make sure about integer division). Write an algorithm which takes amount of the bill from user
and calculates the payable amount by applying the above discount criteria and display it on the
screen.
Please enter the amount of the bill 6500
The discount at the rate 15 % is Rupees 975
The payable amount is Rupees 5525
Use of LOGICAL operator:&& and || and ! not operator
If (age > 18 && height>6)
output ‘you can be a basket ball team player.’