Pascal Guide 5

download Pascal Guide 5

of 3

Transcript of Pascal Guide 5

  • 8/6/2019 Pascal Guide 5

    1/3

    Iteration

    Iteration is when a process gets repeated several times. Iteration is also sometimes

    called looping or repetition.

    If you created a program that displayed the word hello five times. Imagine if you

    had to write a program that displayed the word hello twenty times or a hundred

    times. You could do this by putting a hundred writeln statements in your program oryou could use iteration.

    There are three different iterative statements in Pascal: while..do, repeat..until, and

    for. The for loop is an example of definite iteration used when it is known how

    many times a process needs to be repeated. While and repeat are examples of

    indefinite resolution when it is not known in advance how many times a process

    needs to be repeated, the process gets repeated until a condition is met. The

    difference between repeat and while is that with repeat the process gets executed one

    or more times, with a while loop the process gets executed zero or more times. Type

    in the following program, compile and run it:

    program countingVersion1;

    beginwriteln(1);writeln(2);writeln(3);writeln(4);writeln(5);readln;

    end.

    The program works and displays the numbers 1 to 5. If you wanted to display the

    first 100 numbers then you would need to have 100 writeln statements. An alternative

    program that does the same thing would be:

    program countingVersion2;

    var count:integer;

    beginfor count:=1 to 5 dobegin

    writeln(count);

    end;readln;

    end.

    Type in the program, compile and run it.

    Type in the following program, compile it and try running it a few times:

    Exercise 1

    Alter the program countingVersion2 so that the first 20 numbers are displayed

    instead of the first 5.

  • 8/6/2019 Pascal Guide 5

    2/3

    Iteration

    program averageStudentMark;{A program that calculates the average mark that a class of students got on a test}

    var noofstudents, total, count, mark:integer;var average:real;

    begintotal:=0;write(How many students are there in the class?);readln(noofstudents);for count:=1 to noofstudents dobegin

    write(Enter a mark);readln(mark);total:=total+mark;

    end;average:=total / noofstudents;writeln(The average mark is: , average:6:2);readln;

    end.

    The while loop is an indefinite loop. The compound statement gets executed

    continuously until a certain condition becomes true, with the for loop it was known

    exactly how many times to repeat. Type in the program below, compile and run it.

    program sayHello;

    var continue:char;

    beginwrite(Press y to continue or any other key to finish );readln(continue);while continue=y dobegin

    writeln(hello);write(Press y to continue or any other key to finish );

    readln(continue);end;end.

    Exercise 2

    a) Write a Pascal program that gets the user to enter a name. The name is stored

    in a string variable. The program then needs to ask the user to enter a number,

    that will be stored in an integer variable. The program then uses a for loop to

    display the name entered by the user the number of times requested by the user.

    b) Write a program that asks the user to enter the number of stars per row. The

    program then asks the user to enter the number of rows to be displayed. The

    program then uses for loops to display these stars. For example, entering 4 and 3should display:

    ****

    ****

    ****

    Hint: You can nest two for loops put one for loop inside another

  • 8/6/2019 Pascal Guide 5

    3/3

    Iteration

    This program keeps writing the word hello on the screen until the user presses any

    key other than y. Notice that the instructions in the loop may not be executed at all, if

    the user presses a key other than y at the start then the condition for the while loop

    will not be met and so the loop will not be executed at all.

    The operators that can be used in a condition for a while loop are the same as thosethat could be used for a condition in an if statement.

    The repeat loop is also an indefinite loop. The compound statement gets executed

    continuously until a certain condition becomes true. The difference between repeat

    and while is that with repeat the compound statement always gets executed at least

    once, as the condition is tested at the end of the loop. With a while loop the condition

    is tested at the start of the loop meaning that the compound statement might not get

    executed at all. Type in the program below, compile and run it.

    program sayHello;

    var continue:char;

    beginrepeat

    writeln(hello);write(Press y to continue or any other key to finish );readln(continue);

    until continue y;end.

    Exercise 3

    a) Write a Pascal program that gets the user to enter a number. Store this

    number in an integer variable. Use a while loop to print the number and then

    take one away from it. Keep doing this until the number becomes 0.

    b) What would happen if the user entered a negative number when asked for a

    number in your program from part a? Extension: how could you alter your

    program so that the countdown will only happen when a positive number hasbeen entered?

    c) Write in a program that reads in numbers from the user and adds them to a

    running total using a while loop (make sure that your total variable is set to 0

    before the loop starts). Each time the user is asked if they want to enter another

    number, if they type in n then the loop should stop. After the user enters n then

    the total should be displayed and then the program finishes.

    d) Alter your program in part c so that instead of asking the user if they want to

    enter another number the loop stops if the user enters the number -1. This is an

    example of a rogue value. Make sure the rogue value is not added to the total.

    Exercise 4

    Alter your program from exercise 1 part c from the previous Pascal guide (about

    selection). The program should make use of a repeat statement to keep asking

    the user for a password until a valid password is entered.