
Algorithm and Structur Data 1
1. If…Then…Else
Syntax
If condition Then
Action-1
Else
Action-2
Endif
Example :
x : integer
x = 2
x = x + x
If ( x >= 0 ) Then
Output ( “ positive “ )
Else
Output ( “ negative “ )
Endif
After running the program then the program will show the output, that’s positive.
How the principle working of the example program above :
- Variable x above has data type of integer
- Then variable x given the first value that’s equal to 2.
- In the next row variable x given new value that’s x = x + x. It mean we enter the first value of variable x that’s equal to 2. Then we count it 2 + 2 = 4. So the new value of variable x equal to 4.
- The next row contain selection command If..Then..Else. In the example program above the first condition is if x >= 0 then output positive. The second condition is if the first condition doesn’t do then output negative.
- The example program above result the output positive because new value of variable x equal to 4. And 4 >= 0.
2. Repeat...Until
Syntax
Repeat
statement
…
Until statement
Example :
z : integer
z = 1
Repeat
Output (z)
z = z + 2
Until z > 9
After running the program then the program will show the output, that’s
1
3
5
7
9
How the principle working of the example program above :
- Variable z above has data type of integer
- Then we give the first value to variable z. That’s z = 1.
- In the next row we use looping command Repeat..Until. First, that command will output the value of variable z. That’s z = 1. Number 1 will show as output.
- In the next row variable z given the new value that’s z = z + 2. It mean we enter the first value of variable z that’s equal to 1. Then we count it 3 = 1 + 2. Do the number 3 > 9 ? No. Then Output ( 3 ).
- Then we enter the new value of variable z that’s equal to 3. Next we count it 5 = 3 + 2. Do the number 5 > 9 ? No. Then Output ( 5 ).
- Then we enter the new value of variable z that’s equal to 5. Next we count it 7 = 5 + 2. Do the number 7 > 9 ? No. Then Output ( 7 ).
- Then we enter the new value of variable z that’s equal to 7. Next we count it 9 = 7 + 2. Do the number 9 > 9 ? No. Then Output ( 9 ).
- Then we enter the new value of variable z that’s equal to 9. Next we count it 11 = 9 + 2. Do the number 11 > 9 ? Yes. If in this statement has the value TRUE, program will stop. And no output because we given command that until z > 9.