Looping

16

description

notes

Transcript of Looping

Page 1: Looping
Page 2: Looping

Contents 1. While-do loop ................................................................................................................................. 4

1.1. Syntax: ..................................................................................................................................... 4

1.2. For Example ............................................................................................................................ 4

1.3. Flow Diagram .......................................................................................................................... 5

1.4. Example ................................................................................................................................... 5

2. For-do LOOP .................................................................................................................................. 6

2.1. Syntax: ..................................................................................................................................... 6

2.2. For example: ........................................................................................................................... 6

2.3. Flow Diagram .......................................................................................................................... 7

2.4. Example: .................................................................................................................................. 7

3. Repeat-Until Loop ........................................................................................................................... 8

3.1. Syntax: ..................................................................................................................................... 8

3.2. For example, ........................................................................................................................... 8

3.3. Flow Diagram: ......................................................................................................................... 9

3.4. Example ................................................................................................................................... 9

4. Nested Loops................................................................................................................................. 10

4.1. The syntax ............................................................................................................................. 10

4.1.1. Nested for-do loop ........................................................................................................ 10

4.1.2. Nested while-do loop .................................................................................................... 10

4.1.3. Nested repeat ............................................................................................................... 10

4.2. Example: ................................................................................................................................ 11

5. Loop Control Statements: ............................................................................................................. 11

5.1. Break statement .................................................................................................................... 12

5.1.1. Syntax ............................................................................................................................ 12

5.1.2. Flow Diagram ................................................................................................................ 13

5.1.3. Example ......................................................................................................................... 13

5.2. Continue statement .............................................................................................................. 14

5.2.1. Syntax ............................................................................................................................ 14

5.2.2. Flow Diagram ................................................................................................................ 14

5.2.3. Example ......................................................................................................................... 14

5.3. goto statement ..................................................................................................................... 15

5.3.1. Syntax ............................................................................................................................ 15

5.3.2. Flow Diagram ................................................................................................................ 16

5.3.3. Example ......................................................................................................................... 16

Page 3: Looping

This section shows loop statements used in Pascal:

# Loop Type Description

1 while-do loop

Repeats a statement or group of statements until a given

condition is true. It tests the condition before executing

the loop body.

2 for-do loop

Executes a sequence of statements multiple times and

abbreviates the code that manages the loop variable.

3 repeat-until loop

Like a while statement, except that it tests the condition

at the end of the loop body.

4 nested loops

You can use one or more loop inside any another while,

for or repeat until loop.

Page 4: Looping

1. While-do loop

A while-do loop statement in Pascal allows repetitive computations till some test

condition is satisfied. In other words, it repeatedly executes a target statement as long as a

given condition is true.

1.1. Syntax: The syntax of a while-do loop is:

while (condition) do S;

Where condition is a Boolean or relational expression, whose value would be true or false

and S is a simple statement or group of statements within BEGIN ... END block.

1.2. For Example

while number>0 do

begin

sum := sum + number;

number := number - 2;

end;

When the condition becomes false, program control passes to the line immediately following

the loop.

Page 5: Looping

1.3. Flow Diagram

Here, key point of the while loop is that the loop might not ever run. When the condition is

tested and the result is false, the loop body will be skipped and the first statement after the

while loop will be executed.

1.4. Example program whileLoop;

var

a: integer;

begin

a := 10;

while a < 20 do

begin

writeln('value of a: ', a);

a := a + 1;

end;

end.

When the above code is compiled and executed, it produces the following result:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

value of a: 16

Page 6: Looping

value of a: 17

value of a: 18

value of a: 19

2. For-do LOOP

A for-do loop is a repetition control structure that allows you to efficiently write a loop

that needs to execute a specific number of times.

2.1. Syntax:

The syntax for the for-do loop in Pascal is as follows:

for < variable-name > := < initial_value > to [down to] <

final_value > do S;

Where, the variable-name specifies a variable of ordinal type, called control variable or index

variable; initial_value and final_value values are values that the control variable can take; and

S is the body of the for-do loop that could be a simple statement or a group of statements.

2.2. For example: for i:= 1 to 10 do writeln(i);

Here is the flow of control in a for-do loop:

The initial step is executed first, and only once. This step allows you to declare and

initialize any loop control variables.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is

false, the body of the loop does not execute and flow of control jumps to the next

statement just after the for-do loop.

After the body of the for-do loop executes, the value of the variable in increased or

decreased.

The condition is now evaluated again. If it is true, the loop executes and the process

repeats itself (body of loop, then increment step, and then again condition). After the

condition becomes false, the for-do loop terminates.

Page 7: Looping

2.3. Flow Diagram

2.4. Example: program forLoop;

var

a: integer;

begin

for a := 10 to 20 do

begin

writeln('value of a: ', a);

end;

end.

When the above code is compiled and executed, it produces the following result:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

value of a: 16

value of a: 17

Page 8: Looping

value of a: 18

value of a: 19

value of a: 20

3. Repeat-Until Loop Unlike for and while loops, which test the loop condition at the top of the loop, the repeat

... until loop in Pascal checks its condition at the bottom of the loop.

A repeat ... until loop is similar to a while loop, except that a repeat ... until loop is

guaranteed to execute at least one time.

3.1. Syntax: repeat

S1;

S2;

...

...

Sn;

until condition;

3.2. For example, repeat

sum := sum + number;

number := number - 2;

until number = 0;

Notice that the conditional expression appears at the end of the loop, so the statement(s) in

the loop execute once before the condition is tested.

If the condition is true, the flow of control jumps back up to repeat and the statement(s) in the

loop execute again. This process repeats until the given condition becomes false.

Page 9: Looping

3.3. Flow Diagram:

3.4. Example program repeatUntilLoop;

var

a: integer;

begin

a := 10;

(* repeat until loop execution *)

repeat

writeln('value of a: ', a);

a := a + 1

until a = 20;

end.

When the above code is compiled and executed, it produces the following result:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

value of a: 16

value of a: 17

value of a: 18

value of a: 19

Page 10: Looping

4. Nested Loops Pascal allows using one loop inside another loop. Following section shows few examples

to illustrate the concept.

4.1. The syntax

4.1.1. Nested for-do loop

The syntax for a nested for-do loop statement in Pascal is as follows:

for variable1:=initial_value1 to [downto] final_value1 do

begin

for variable2:=initial_value2 to [downto] final_value2 do

begin

statement(s);

end;

end;

4.1.2. Nested while-do loop

The syntax for a nested while-do loop statement in Pascal is as follows:

while(condition1)do

begin

while(condition2) do

begin

statement(s);

end;

statement(s);

end

4.1.3. Nested repeat

The syntax for a nested repeat ... until loop Pascal is as follows:

repeat

statement(s);

repeat

statement(s);

until(condition2);

until(condition1);

A final note on loop nesting is that you can put any type of loop inside of any other type of

loop. For example, a for loop can be inside a while loop or vice versa.

Page 11: Looping

4.2. Example: The following program uses a nested for loop to find the prime numbers from 2 to 50:

program nestedPrime;

var

i, j:integer;

begin

for i := 2 to 50 do

begin

for j := 2 to i do

if (i mod j)=0 then

break; {* if factor found, not prime *}

if(j = i) then

writeln(i , ' is prime' );

end;

end.

When the above code is compiled and executed, it produces the following result:

2 is prime

3 is prime

5 is prime

7 is prime

11 is prime

13 is prime

17 is prime

19 is prime

23 is prime

29 is prime

31 is prime

37 is prime

41 is prime

43 is prime

47 is prime

5. Loop Control Statements: Loop control statements change execution from its normal sequence. When execution leaves

a scope, all automatic objects that were created in that scope are destroyed.

Pascal supports the following control statements. Click the following links to check their

details.

Page 12: Looping

Control Statements Description

1 break statement

Terminates the loop or case statement and transfers

execution to the statement immediately following the

loop or case statement.

2 continue statement Causes the loop to skip the remainder of its body and

immediately retest its condition prior to reiterating.

3 goto statement Transfers control to the labeled statement. Though it is

not advised to use goto statement in your program.

5.1. Break statement The break statement in Pascal has the following two usages:

1. When the break statement is encountered inside a loop, the loop is immediately

terminated and program control resumes at the next statement following the loop.

2. It can be used to terminate a case in the case statement (covered in the next chapter).

If you are using nested loops (i.e., one loop inside another loop), the break statement will stop

the execution of the innermost loop and start executing the next line of code after the block.

5.1.1. Syntax

The syntax for a break statement in Pascal is as follows:

break;

Page 13: Looping

5.1.2. Flow Diagram

5.1.3. Example

program exBreak;

var

a: integer;

begin

a := 10;

(* while loop execution *)

while a < 20 do

begin

writeln('value of a: ', a);

a:=a +1;

if( a > 15) then

(* terminate the loop using break statement *)

break;

end;

end

When the above code is compiled and executed, it produces the following result:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

Page 14: Looping

5.2. Continue statement The continue statement in Pascal works somewhat like the break statement. Instead of

forcing termination, however, continue forces the next iteration of the loop to take place,

skipping any code in between.

For the for-do loop, continue statement causes the conditional test and increment portions of

the loop to execute. For the while-do and repeat...until loops, continue statement causes the

program control to pass to the conditional tests.

5.2.1. Syntax

The syntax for a continue statement in Pascal is as follows:

continue;

5.2.2. Flow Diagram

5.2.3. Example

program exContinue;

var

a: integer;

begin

a := 10;

(* repeat until loop execution *)

repeat

if( a = 15) then

begin

(* skip the iteration *)

a := a + 1;

Page 15: Looping

continue;

end;

writeln('value of a: ', a);

a := a+1;

until ( a = 20 );

end.

When the above code is compiled and executed, it produces the following result:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 16

value of a: 17

value of a: 18

value of a: 19

5.3. goto statement

A goto statement in Pascal provides an unconditional jump from the goto to a labeled

statement in the same function.

NOTE: Use of goto statement is highly discouraged in any programming language because it

makes difficult to trace the control flow of a program, making the program hard to understand

and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the

goto.

5.3.1. Syntax

The syntax for a goto statement in Pascal is as follows:

goto label;

...

...

label: statement;

Here, label must be an unsigned integer label, whose value can

be from 1 to 9999.

Page 16: Looping

5.3.2. Flow Diagram

5.3.3. Example

The following program illustrates the concept.

program exGoto;

label 1;

var

a : integer;

begin

a := 10;

(* repeat until loop execution *)

1: repeat

if( a = 15) then

begin

(* skip the iteration *)

a := a + 1;

goto 1;

end;

writeln('value of a: ', a);

a:= a +1;

until a = 20;

end.

When the above code is compiled and executed, it produces the following result:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 16

value of a: 17

value of a: 18

value of a: 19