Академический Документы
Профессиональный Документы
Культура Документы
CSC1101 PLF
For counting purpose, some languages use base-zero system where the first
element of the array will start from zero not one.
The programmer needs to add 1 to the system so that the first element in the
array will start from one for easy reference.
Some languages use base-one system and it is easier for the programmer to
understand since the first element is the first box, second element is the
second box, and so on.
The counter
The counter is used to count the loop repetitions and it is known as incrementing.
It is used to count the number of items, people, temperatures and so on.
To write the instruction to increment a variable, an assignment statement needs to be
used.
Initialize the counter before starting the loop
Plus sign (+) used to increment the counter
Remark:
Counter is a
variable.
Syntax:
Counter = 0
Counter = Counter + 1
Prepared By Kavitha.S
Page 1 of 8
CSC1101 PLF
The accumulator
The accumulator used to accumulate or sum up the values.
Syntax:
Totalsales = 0
Totalsales = Totalsales + Sales { Sales is the input variable}
Example
Totalsales
100
300
600
Sales
100
200
300
Prepared By Kavitha.S
Page 2 of 8
CSC1101 PLF
While/While-End Loop
Repeats instruction while a condition is TRUE and stops repeating when a condition is
FALSE.
Syntax:
While <Condition(s)>
<Instruction>
<Instruction>
While-End
Trip value / Flag used to control the loop or terminate the loop repetition
Prepared By Kavitha.S
Page 3 of 8
CSC1101 PLF
Begin
While
<Condition
>
T
Instruction
Instruction
Repeat/ Until
A
set
statements
of
will
End
Loop
Syntax:
Repeat
<Instruction>
< Instruction>
Until <Condition(s)>
Prepared By Kavitha.S
Page 4 of 8
CSC1101 PLF
Questions:
Use the above questions to develop the solutions by using REPEAT/UNTIL method.
FLOWCHART ILLUSTRATES THE REPEAT/UNTIL LOOP LOGIC STRUCTURE
Begin
Repeat
Instruction
Instruction
F
Until
<Condition
>
T
End
Prepared By Kavitha.S
Page 5 of 8
CSC1101 PLF
<Instruction>
<Instruction>
<Instruction>
<Instruction>
Loop-end: Counter
Loop-end: Counter
Example:
Loop : c = 1 to 3 step 1
Display c
End-loop: c
Precondition loop
Referred as input assertions.
They are comments that indicate what can be expected to be TRUE before the loop is
Prepared By Kavitha.S
Page 6 of 8
CSC1101 PLF
entered
Example, WHILE/WHILE-END
E.g.
Num = 56
While num > 0
Display num
Enter num
While-end
Postcondition Loop
Referred as output assertions
They are comments that indicate what can be expected to be TRUE when the loop is
exited.
Example: REPEAT/UNTIL
E.g.
REPEAT
ENTER num
DISPLAY num
UNTIL num < 0
Nested Loops
Each loop must be nested inside the loop just outside it.
Example:
Loop: x = 1 to 4 step 1
Loop: y = 1 to 3 step 1
Print *
End-loop: y
Print w/o
End-loop: x
Prepared By Kavitha.S
Page 7 of 8
CSC1101 PLF
Indicators are logical variables that a programmer sets within a program to change the
processing path or to control when the processing of a loop should end.
They are sometimes called flag, switches or trip values.
For example, an indicator for AGE might be 0 or 500 or 300. An indicator for an error
could be a variable with the value TRUE when an error has occurred, and FALSE when
there have been no errors.
Prepared By Kavitha.S
Page 8 of 8