SELECTION STATEMENTS
1. Simple IF condition
IF THEN
statement1
statement2
ENDIF
Example 1:
IF x < 0 THEN
OUTPUT "Negative"
ENDIF
2. IF…THEN…ELSE statement
IF THEN
statement1
statement2
ELSE
statement3
statement4
ENDIF
Example 1:
IF x < 0 THEN
OUTPUT "Negative number"
ELSE
OUTPUT "Positive number"
ENDIF
3. IF…ELSE…IF statement
Syntax:
IF <condition1> THEN
statement1
ELSE IF <condition2> THEN
statement2
ELSE
statement3
ENDIF
ENDIF
Example:
INPUT mark
IF mark > 80 THEN
OUTPUT "Grade A"
ELSE IF mark > 60 THEN
OUTPUT "Grade B"
ELSE IF mark > 40 THEN
OUTPUT "Grade C"
ELSE
OUTPUT "GRADE D"
ENDIF
CASE…OF
For loop is called a count-controlled loop. It is used when a fixed number of iterations are required.
Example 1:
Program to print “Hello” 10 times.
OUTPUT "Enter a digit to convert to word.
INPUT Digit
CASE Digit OF
1: OUTPUT "One"
2: OUTPUT "Two"
3: OUTPUT "Three"
4: OUTPUT "Four"
OTHERWISE: "Invalid input"
ENDCASE
LOOPS
FOR…NEXT loop
For loop is called a count-controlled loop. It is used when a fixed number of iterations are required.
Example 1:
Program to print “Hello” 10 times.
FOR num ← 1 TO 10
OUTPUT "Hello"
NEXT num
Example 2:
Program to print numbers from 1 to 10.
FOR count ← 1 TO 10
OUTPUT count
NEXT count
Example 3:
Program to print sums of all odd numbers and even numbers from 1 to 100.
FOR number ← 1 TO 100
IF number % 2 = 0 THEN
odd_sum ← odd_sum + number
ELSE
even_sum ← even_sum + number
ENDIF
NEXT number
OUTPUT "Sum of Odd numbers =",odd_sum
OUTPUT "Sum of Even numbers =",even_sum
WHILE…DO loop
Syntax:
WHILE <condition> DO
statement1
statement2
ENDWHILE
Example 1:
Write pseudocode for printing “Hello” 10 times.
count ← 1
WHILE count < 11 DO
OUTPUT "Hello"
count ← count + 1
ENDWHILE
This loop executes 10 times, for all the values of count from 1 to 10. The variable count is used as a counter and is initialised before the beginning of the loop. This variable can be called a loop control variable. This variable needs to be updated inside the variable.
REPEAT…UNTIL loop
Syntax:
REPEAT
statement1
statement2
UNTIL
#statements1 and statement2 will continue executing repeatedly until the condition becomes True.
REPEAT loop executes until the given condition becomes TRUE. The condition is evaluated at the end of the loop, hence this loop is called as post-condition loop. This loop will execute at least once even if the condition is FALSE.
count ← 1
REPEAT
OUTPUT "Hello"
count ← count + 1
UNTIL COUNT = 11
// This program prints the word 'Hello' 10 times.
Example 1
Ask the user to input a number between 1 and 5 and validating the input.
REPEAT
OUTPUT "Enter a number between 1 and 5:"
INPUT number
UNTIL number >=1 AND number <=5