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

CONDITIONAL

STATEMENTS
OBJECTIVES
EXPLAIN CONDITIONAL STATEMENTS
DEMONSTRATE THE TYPES OF CONDITIONAL STATEMENTS
Conditional Statements in Small Basic
If you want the program to perform different functions under different conditions, use conditional statements.

If statement is one such type of conditional statement.


If-Then-Else statement

4
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 5


The word If is followed by the condition. The condition should be within parentheses.

The condition is followed by the word Then and the statements to be executed if the condition is true.

The EndIf comes at the end.


◦ This tells the computer that the conditional execution is over.

Between Then and EndIf, there may be more than one statement.

©Oxford University Press BASICS OF MS SMALL BASIC 6


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 7


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.
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


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


TextWindow.WriteLine("Good Morning World") If (Clock.Hour < 12) Then
EndIf TextWindow.Write("Good Morning. ")
If (Clock.Hour >= 12) Then TextWindow.WriteLine ("How was breakfast?")
TextWindow.WriteLine("Good Evening World") EndIf
EndIf

If (age < 12) Then


TextWindow.Write(“He is a small boy “ )
EndIf

Age = 20
QUIZ
https://quizizz.com/admin/quiz/6264de478fa74b001d9868c4

You might also like