LG4 - Conditional and Loop Statements
LG4 - Conditional and Loop Statements
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
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”.
END
A. If Statement Examples
2. Input a score. If the score is 100, display the message “Excellent”
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
END
If… Then… Else Statement Examples
Double
Price1, Price2,
Total,
Discount
Y Y
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
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
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
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
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