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

Paper 2 Part 2 (Programming Structures)

This document discusses different logic statements, loops, arrays, and rogue values. It explains simple, complex, and nested conditional logic statements. It also describes for, repeat until, and while loops and how they differ in their conditional checking. Working with arrays is demonstrated by accessing elements by index, swapping values, and printing all elements using a for loop. The concept of a rogue value is introduced as a value that terminates a sequence but is of the same data type.

Uploaded by

송준혁
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Paper 2 Part 2 (Programming Structures)

This document discusses different logic statements, loops, arrays, and rogue values. It explains simple, complex, and nested conditional logic statements. It also describes for, repeat until, and while loops and how they differ in their conditional checking. Working with arrays is demonstrated by accessing elements by index, swapping values, and printing all elements using a for loop. The concept of a rogue value is introduced as a value that terminates a sequence but is of the same data type.

Uploaded by

송준혁
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Logic Statements:-

You should understand the difference between Simple Logic, Complex Logic and Nested IF Statements.
PRINT “enter a positive number”
INPUT number This is a simple condition
If number > 0 In which case it is only determining is the
if number > 10 number is positive.
then
print “this number is greater than 10” This is a nested if statement as it is part
else if number>=5 and number<=10 of another if statement (i.e. number>0).
then
print “this number is between 5 and 10 (inclusive)” This is a complex condition as it is taking
else in account of two conditions.
print “this number is less then 5” (number>=5 and number<=10) and since
else there is an “and” operator in between the
print “this number is not positive” two condition, both sub-statements have
to be true for this statement to be true.
Case…OF…OTHERWISE…ENDCASE
input choice
case of choice A variable “choice” will hold some value which
1: print “Have fun” is to be investigated and only one statement is
2: print “good luck” to be executed based on the contents of choice.
3: print “good night” Of the input is among 1..4 then the
4: print “good bye” corresponding statement will execute.
otherwise: However, if anything else in the “choice” then
Print “you have not selected a valid option” the default condition will execute.
end case
Loops:-
We will be solving the following problem using different
types of loops:-
“take 10 numbers as input and output the largest
number”
FOR…TO…NEXT…ENDFOR REPEAT…UNTIL WHILE…DO…ENDWHILE

INPUT BiggestSoFar INPUT BiggestSoFar INPUT BiggestSoFar


FOR Counter ß 1 to 9 REPEAT WHILE Counter < 9
INPUT NextNumber INPUT NextNumber INPUT NextNumber
IF NextNumber>BiggestSoFar IF NextNumber>BiggestSoFar IF NextNumber>BiggestSoFar
THEN THEN THEN
BiggestSoFar ßNextNumber BiggestSoFar ßNextNumber BiggestSoFar ßNextNumber
END IF END IF END IF
NEXT Counter ß Counter + 1 Counter ß Counter + 1
END FOR UNTIL Counter = 10 END WHILE
PRINT BiggestSoFar PRINT BiggestSoFar
For Loop is an Unconditional Loop which Repeat Until loop is a Post Conditional While Loop is a Pre Conditional Loop.
means that is will run the defined number Loop. There are no defined number of steps There are no defined number of steps and
of time no matter what. i.e. 9 times (counter the loops terminates when the condition the loops terminates when the condition
1 to 9). tested after each step becomes true. In this tested before each step becomes false. i.e
The statements between FOR and ENDFOR case that condition is Counter = 9. In which Counter<9. In which case if we start counter
are to be repeated 9 times. case if we start counter at 0 this program will at 0 this program will run 10 times before
run 10 times before exiting loop. exiting loop.
A rogue value is a value used to terminate a sequence of values. The rogue value is of the same
data type but outside the range of normal expected values.

BiggestSoFar ß 0 This algorithm works even if the sequence consists


REPEAT of only one non-zero input. However, it will not
INPUT NextNumber work if the only input is 0.
IF NextNumber>BiggestSoFar
THEN
BiggestSoFar ßNextNumber
END IF
UNTIL NextNumber = 0
PRINT BiggestSoFar
If the rogue value is entered the
Repeat Until statement will still
execute its statements whereas the
While Loop will not as it tests its
condition before entering the loops
statements.
Working with Arrays:-
This 1D array is given an identifier, for example MyList, and each element within the
array is referred to using this identifier and its position (index) within the array. For
example, MyList[4] refers to the fourth element in the MyList array.

Arrays are data structures that store the same type of data usually in random order.

Temp ßMyList[6]
MyList[6]ßMyList[3]
MyList[3]ßTemp
Swapping two values in an array

PRINT MyList[1] //prints 25


INPUT MyList[5] // overwrites 41 with whatever is being input

FOR count 1 to 7
PRINT MyList[count]
NEXT
END FOR //index position changes from count 1 to 7
//and prints all elements in array. 25,34,98…

You might also like