You are on page 1of 26

Conditional and

Loop Statements
Prayer

Loving Father,
Come be with us today,
Fill our hearts with joy,
Fill our minds with learning,
Fill our lessons with fun,
Fill our friendships with kindness,
Fill our school with love,
Help us grow in love and kindness more like Jesus everyday.
Amen.
Conditional Statements
I. Conditional Statements

1. If Statement

2. If… Then… Else Statements

3. Select Case Statements


A. If Statement
 Used to evaluate a condition or check a value as “True” or “False”
 If the condition is not met, the succeeding statements or set of statements will be executed.

Syntax:
If condition
Statement
End If
If Statement Examples
1. Input a color. If the color is blue, display the message “The color is blue”.

Algorithm: Flowchart: BEGIN


String Color
Begin
Input Color String
If Color = Blue Color

Print “The color is blue”


End If
End Input Color

Print “The Color is blue” Y N


Color = Blue?

END
A. If Statement Examples
2. Input a score. If the score is 100, display the message “Excellent”

Algorithm: Flowchart: BEGIN


Integer Score
Begin
Input Score Integer
If Score = 100 Score

Print “Excellent”
End If
End Input Score

Print “Excellent” Y N
Score = 100?

END
B. If… Then… Else Statement
 Used to evaluate a condition or check a value as “True” or “False”
 If the condition is not met, the statement or set of statements after will be executed

Syntax: If condition then


If condition then Statement(s)
Statement 1 Else if condition 1 then
Else Statement(s)
Statement 2 Else if condition 1 then
End If Statement(s)
Else
Statement(s)
End If
If… Then… Else Statement Examples
1. Input a grade. If the grade is greater than or equal to 75, display the message “You passed the
subject”, otherwise display “Study hard”

Algorithm: Flowchart: BEGIN


Integer Grade
Begin
Input Grade Integer
Grade
If Grade>=75
Print “You passed the subject”
Else
Input Grade
Print “Study hard.”
End

Print “You passed the Y N


subject” Grade>=75? Print “Study
hard”

END
If… Then… Else Statement Examples

2. Input 2 prices and get the total. If the


Algorithm:
total is less than or equal to 500 pesos,
Double Price 1, Price 2, Total, Discount
get 5% of the total, if the total is more Begin
than 500 pesos but less than or equal to Input Price1, Price2
1,000 pesos, get 10% of the total, but, if Total = Price1 + Price 2
the total is greater than 1,000, get 15% of If Total <=500 Then
the total. Display the output Discount = Total * .05
End If
If Total <=1,000 Then
Discount = Total * .10
End If
If Total > 1,000 Then
Discount = Total * .15
End If
Print Discount
End
Flowchart: BEGIN

Double
Price1, Price2,
Total,
Discount

Input Price1, Price2


N N
Total>1000? Total<=1000?

Total = Price1 + Price2

Y Y

Discount = Total * .15 Discount = Total * .10


N
Total<=500?

END Print Discount Discount = Total * .05


C. Select Case Statement
 It is similar to the use of the If… Then… Else statement
 It is used when a variable is compared to different values or expressions.

Syntax:
Select Case text expression
Case expressionlist1
Statement Block1
Case expressionlist2
Statement Block1
….
End Select
Select Case Statement Example
Algorithm:
Character Conduct
Conduct Grade equivalent
1 – Excellent
Begin
2 – Very Good Input Conduct
3 – Good Select Case Conduct
4 – Satisfactory Case “1”
5 – Needs Improvement Print “Excellent”
Case “2”
Input a conduct grade and display the
Print “Very Good”
equivalent rating.
Case “3”
Print “Good”
Case “4”
Print “Satisfactory”
Case “5”
Print “Needs Improvement”
End Select
End
Flowchart:
Loop Statements

1. For… Next Statement

2. Do… While Loop Statements

3. Do… Until Statements


A. For…Next Statement
 Used to execute a given action in a specified number of times.

Syntax:
For counter=start to end (Step increment)
Statement(s)
Next counter
For…Next Statement Example
Algorithm:
Integer Counter
Display the sentence “Count the number
Begin
of loops.” five times.
Counter = 1
For Counter = 1 to 5
Print “Count the number of loops.”
Counter = Counter + 1
Next Counter
End
Flowchart: BEGIN

Integer
Counter=1

Print “Count the number of loops”

Counter = Counter + 1

N
Counter=5

END
B. Do…While Loop Statement
 Used to evaluate a condition if “true” or “false”,
 If the condition is true, the statements given are executed.
 If false, the loop will stop and the action is terminated.

Syntax:
Do While condition
Statement(s)
Loop
Do…While Loop Statement Example

Input an age. If the age is above or equal


to 18, display the message “You can Algorithm:
vote”. The program will only end if the Integer Age
user is a minor. Begin
Do While Age >= 18
Input Age
Print “You can vote”
Loop
End
Flowchart: BEGIN

Integer Age

Input Age

Y
Age>=18 Print “You can vote”

END
C. Do…Until Loop Statement
 Used to evaluate a condition if “true” or “false”,
 If the condition is false, the statements given are executed
 If true, the loop will stop and the action is terminated.

Syntax:
Do Until condition
Statement(s)
Loop
Do…Until Loop Statement Example

Input your age and your friend’s age. Algorithm:


Displays his/her age until you have the
Integer My_Age, Fr_AGe
same age. The program will only end if
the age of your friend is the same as Begin
yours. Input Age
Do Until My_Age = Fr_Age
Input Fr_Age
Print “Fr_Age”
Loop
End
Flowchart: BEGIN

Integer
My_Age,
Fr_Age

Input My_Age

Input Fr_Age

Print Fr_Age

Y
N
END My_Age=Fr_Age
Remember
1. If you study hard then You are only presented with 1 course of action
You will get a good grade.
End If
2. If you study hard then You are only presented 3 choices with
You will get a good grade. Corresponding outcomes
Else If you cheat
You will be sanctioned.
Else If you cram in the review
You will get a low grade
End If
Remember
3. Select Case Subject You are presented with many options wherein you
Case 1 are asked to choose 1 and its corresponding action
Study Math will take place.
Case 2
Study Science
Case 3
Study English
Case 4
Study Filipino
Case 5
Study History
Case 6
Study CLE
End Select
Remember
4. Do Until Name = “Mark” Until the name becomes Mark it will ask again for
Ask Name the name and display it.
Write Name
Loop

5. Do While Name = “Anna” Until the name remains Anna, it will ask again for
Ask Name the name and display it.
Write Name
Loop

6. For Counter = 1 to 3 It will ask a name and age for three times.
Ask Name
Ask Age
Loop

You might also like