Sunteți pe pagina 1din 16

CSEC IT Workshop

Syllabus concern areas & Pascal programming


Part B

August 4-6, 2014


Loops
• In general, statements are executed sequentially.
• There may be a situation, when you need to execute a block of code
several number of times.
• A loop statement allows us to execute a statement or group of
statements multiple times.
• Here is the general form of a loop statement in Pascal

Cover three types:


• While
• For
• Repeat
• Allows repetitive statements while some condition is true
• Syntax:
While (condition) Do S; While-Do Loop
• Sample code:
While number > 0 Do
Begin
sum := sum + number;
number := number - 2;
End;
• Example
Program whileLoop;
What will
Var a: integer; be the
Begin output?

a := 10;
While a < 20 Do
Begin
Writeln('value of a: ', a);
a := a + 1;
End;
Readln;
End.
• Allows repetitive statements a specified number of
times
• Syntax For-Do Loop
For < variable-name > := < initial > to < final > Do S;
• Sample Code
For i:= 1 to 10 Do
Writeln(i);
• Example
Program forLoop;
What will
Var be the
output?
a: integer;
Begin
For a := 10 to 20 Do
Begin
Writeln('value of a: ', a);
End;
Readln;
End.
• Similar to a while loop, guaranteed to execute at least one time - while condition false
• Syntax Repeat-Until
Repeat
S1;
Loop
S2;
Until condition;
• Sample code:
Repeat
sum := sum + number;
number := number - 2;
Until number = 0;
• Example
Program repeatUntilLoop;
Var
What will
a: integer; be the
Begin output?
a := 10;
Repeat
Writeln('value of a: ', a);
a := a + 1
Until a = 20;
Readln;
End.
Looping Problem Example
A minibus charges $1.50 per passenger. Write a program to input
the number of passengers for each day terminated by 0. Calculate
and print the daily revenue, to total number of passengers and the
total revenue collected.

Analysis
o Number of iterations unknown
o No For loop
o Can use While-Do loop
o Also possible with Repeat-Until
Repeat
While loop
loop

Input value for


testing

Will
repeat at
least once
Appointment clock
1. In a regional exam 20 students got scores for the mathematics
paper. Input the scores for all 20 students. Calculate and print
the average score and also the lowest and highest scores
achieved.
2. A school has three houses A, B and C. Write a program to read
a house and the points awarded for winning each race in an
athletic finals terminated by “D”. Calculate and print the total
points gained by each house appropriately labeled.

• 12 o'clock appointment
• Discuss, share, solve
• Pascal working solution – include comments
• Present to audience
Arrays
• Data structures which can store a fixed-size sequential
collection of elements of the same type
• Used to store a collection of data, useful to think of an array
as a collection of variables of the same type
• All arrays consist of contiguous memory locations
• Lowest memory address corresponds to the first element and
the highest address to the last element
• Arrays can be multi-dimensional – studying one dimension or
linear arrays
Declaring Arrays
• A programmer may
• declare the type and then create variables of that array or
• directly declare the array variable
• General form of type declaration of one-dimensional array:
type
array-identifier = array[index-type] of element-type;
• Where:
• array-identifier indicates the name of the array type
• index-type specifies the subscript of the array
• element-type specifies the types of values to be stored
• Example:
Type
vector = array [ 1..25] of real;
Var
velocity: vector;
• Now, velocity is a variable array of vector type, which is sufficient to hold
up to 25 real numbers
Initializing Arrays
• Arrays are initialized through assignment, either by specifying
a particular subscript or using a for-do loop
• Examples:

Type
multi = array[1..20] of integer;
Var
square: multi;
square[6] := 36;
n: integer;
square[10] := 100;
Begin
...
For n:= 1 to 20 Do
square[n] := n * n;
….
Accessing Array Elements
• An element is accessed by indexing the array name
a: integer;
a: = square[6];
• This example uses all the above-mentioned concepts: declaration, assignment and
accessing arrays
Direct
Program exArrays; declaration
When the above code is
Var
compiled and executed, it
n: array [1..10] of integer;
produces the following result:
(* n is an array of 10 integers *)
Element[1] = 101
i, j: integer;
Element[2] = 102
Begin Element[3] = 103
(* initialize elements of array n *) Element[4] = 104
For i := 1 to 10 Do Element[5] = 105
n[ i ] := i + 100; Element[6] = 106
(* output each array element's value *) Element[7] = 107
For j:= 1 to 10 Do Element[8] = 108
Writeln('Element[', j, '] = ', n[j] ); Element[9] = 109
End. Element[10] = 110
Arrays Problem Example
Write a program to read the description of 10 items and the price of each item. Print a price list
showing the description, the original price of each item and the new price after tax of 10% is
added. Input a discount percentage for a sale event; output the new price and discounted price
for items 4 and 9.

Analysis
o Arrays needed to store:
o 10 descriptions
o 10 prices
o 2 For loops
o For reading value into arrays
o To output values from arrays
o Output modified values from 2 arrays elements
Appointment clock
13. In a regional exam 20 students got scores for the mathematics
paper. Input the scores for all 20 students. Calculate and print
the average score and also the lowest and highest scores
achieved. Also print the scores received by the 9th and 16th
students.  
14. A school has three houses A, B and C. Write a program to read a
house and the points awarded for winning each race in an
athletic finals terminated by “D”. Calculate and print the total
points gained by each house appropriately labeled. Also print
the 3rd and 5th house and points.

• 6 o'clock appointment
• Discuss, share, solve
• Pascal working solution – include comments
• Present to audience
Closure
• Evaluation
• Future plans
• Collaboration – sharing (drop box)
• Other…

S-ar putea să vă placă și