Nothing Special   »   [go: up one dir, main page]

Conditional Statements 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

CONDITIONAL

STATEMENTS 2
Objectives
• Recall the IF statement
• Explain how to code using IF else statements
If-Then Statement

• If-Then statement is the simplest conditional statement in Small Basic.

• Its syntax is:


If (condition) Then
(Statements to be executed if the condition is true)
EndIf

• The words If, Then, and EndIf are reserved words as they have special
meaning in Microsoft Small Basic.

©Oxford University Press Basics of MS Small Basic 4


Let’s analyze the first three lines of the program. You’d have already figured out that this line tells the computer that if the
Clock.Hour is lesser than 12, print out “Good Morning World.” The words If, Then and EndIf are special words that are understood
by the computer when the program is run. The word If is always followed by a condition, which in this case is (Clock.Hour < 12).
Remember that the parentheses are necessary for the computer to understand your intentions. The condition is followed by then
and the actual operation to execute. And after the operation comes EndIf. This tells the computer that the conditional execution
is over. Between the then and the EndIf, there could be more than one operation and the computer will execute them all if the
condition is valid. For example, you could write something like this

If (Clock.Hour < 12) Then If (Clock.Hour < 12) Then


TextWindow.WriteLine("Good Morning World") TextWindow.Write("Good Morning. ")
EndIf TextWindow.WriteLine ("How was breakfast?")
If (Clock.Hour >= 12) Then EndIf
TextWindow.WriteLine("Good Evening World")
EndIf
If-Then-Else Statement
• If-Then-Else statement goes a step further by also specifying the action
to be performed in case the condition results false.

• This statement is used in the following manner:


If (condition) Then
(Statements to be executed if condition is true)
Else
(Statements to be executed if condition is false)
EndIf

©Oxford University Press Basics of MS Small Basic 6


• Notice that the statements between If, Else, and EndIf are indented.
Indentation is not required but it helps to understand the structure of the
program and finding errors will become easier.

• If-Then-ElseIf statement is used when you need to check a number of


conditions in the program.
If (age < 12) Then
TextWindow.WriteLine(“He is a small boy")
Else
TextWindow.WriteLine(“He is a big boy")
EndIf
• Its syntax is:
If (condition 1) Then
(Statements to be executed if condition 1 is true )
ElseIf (condition 2) Then
(Statements to be executed if condition 1 is false and
condition 2 is true)
ElseIf (condition 3) Then
(Statements to be executed if condition1 and 2 are false
and condition 3 is true)
Else
(Statements to be executed if all conditions are false)
EndIf

©Oxford University Press Basics of MS Small Basic 9


QUIZ
• https://quizizz.com/admin/quiz/62684410c002b9001d807b51

You might also like