0% found this document useful (0 votes)
2 views

Syntax+Example (if+Case+All Loops)

Uploaded by

yeasinalyeasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Syntax+Example (if+Case+All Loops)

Uploaded by

yeasinalyeasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

The three basic constructs Paper: 2

The Three Basic Constructs:


The three basic programming constructs: sequence, selection and iteration, allows us to control
how the program will execute the statements in it
1. Sequence: A sequence is a control structure in which a set of instructions is each executed
once, in the order in which they are written.

Example: The following program is an example of sequence that calculates the average of two numbers.
declare total as integer
declare average as real
declare a, b as integer
input a
input b
total = a + b
average = total/2
output total, average

2. Selection: Selection is a control structure in which an option of statements is provided and a


condition is used to decide which (if any) statements should be executed. The selection includes:
a) IF statements
b) CASE statements

a) IF statements: The common format/Syntax of IF statements are given below

i) IF <condition> THEN
<Statement>
END IF

ii) IF <condition> THEN


< Statement1>
ELSE
< Statement2 >
END IF

Example 1: (a/i)

IF amount >= 10 Then


Print “You can have Tea”
End If

IF amount >= 20 Then


Print “You can have Coffee”
End If

IF amount >= 50 Then


Print “You can have Ice-cream”
End If

Teacher: Md Nazrul Islam | School: BAF SEMS


Email: [email protected] 1
The three basic constructs Paper: 2

Example 2: (a/ii)

IF mark >= 50 Then


Print “Student Passed”
Else
Print “Student Failed”
End If

Example 3: (a/ii) [Do the dry run of it]

total = 15
k=5

input k

IF k >= 3 THEN
k=k*k
total = total + k
ELSE
total = total + k
END IF

print total

b) Syntax of CASE statements: The common format of CASE statement is given below:

SELECT CASE <expression>


CASE <value1>
<One or more statements>
CASE <value2>
< One or more statements >
CASE <value3>
< One or more statements >

CASE Else
< One or more statements >
END SELECT

Teacher: Md Nazrul Islam | School: BAF SEMS


Email: [email protected] 2
The three basic constructs Paper: 2

Example 1:

Select Case Grade


Case 90 To 100
Print "A*"
Case 80 To 89
Print "A"
Case 70 To 79
Print "B"
Case 60 To 69
Print "C"
Case 50 To 59
Print "D"
Case Else
Print "Fail"
End Select

3. LOOPS: Many problems involve a process that needs to be repeated a specific number of
times. So it is useful to have a technique that performs these loops. There are
number of techniques available such as:

 FOR … NEXT
 WHILE … DO
 REPEAT … UNTIL

a) FOR … NEXT Loop: This is used when the loop is to be repeated a known fixed number of
times. The counter is automatically increased each time the loop is performed. For example, if we
want to perform a loop to add together the ten numbers, we can do that by using For Loop.

Syntax of a For loop:

For <CounterVariable> = <StartNumber> To <EndNumber>


input mark/number/data/value/temp/etc
…………………
…………………
Next <CounterVariable>

Teacher: Md Nazrul Islam | School: BAF SEMS


Email: [email protected] 3
The three basic constructs Paper: 2

Example:
total = 0
For count = 1 to 5 [1, 2, 3, 4, 5]
input number [80, 70, 90, 75, 60]
total= total + number [0+80=80, 80+70=150, 150+90=240, 240+75=315, 315+60=375]
Next count
average = total/5 [75]
Output total, average [375, 75]

b) WHILE … DO Loop: The WHILE … DO loop may be used in preference to the FOR …
NEXT loop; it can also be used when we do not know how many times the loop is to be
performed. The loop is ended when certain condition is true. This condition is checked before
starting the loop.

Syntax of a While..Do loop:

count = 1
total = 0
While <Condition> Do
input mark/number/data/value/temp/etc
…………………
…………………
count = count + 1
Endwhile

Example:

count = 1
total = 0
While count < = 5 Do
input number
total = total + number
count = count + 1
Endwhile
average = total/5
Output total, average

c) REPEAT … UNTIL Loop: The REPEAT … UNTIL loop may be used in


preference to the FOR … NEXT loop; it can also be used when we do not know how many times
the loop is to be performed. The loop is ended when a certain condition is true. This condition is
checked at the end of the loop so a REPEAT loop always has to be performed at least once.

Teacher: Md Nazrul Islam | School: BAF SEMS


Email: [email protected] 4
The three basic constructs Paper: 2

Syntax of a Repeat..Until loop: Example:


count = 1 count = 1
total = 0 total = 0
Repeat Repeat
input mark/number/data/value/temp/etc input number
………………… total = total + number
………………… count = count + 1
count = count + 1 Until count > 5
Until <Condition> Output total

While Loop:
 Pre-conditional loop
 Count=1
 Count=count+1
 Use < or <= sign in condition
 Don’t use > or >= sign

Repeat Until Loop:


 Post-conditional loop
 Count=1
 Count=count+1
 Use > or >= or only = sign in condition
 Don’t use < or <= sign
For Loop:
 No Pre or No Post conditional
 No count=1
 No count=count+1
 Better loop than the other loops

Teacher: Md Nazrul Islam | School: BAF SEMS


Email: [email protected] 5

You might also like