 # While and until Loops in Perl

I will start this chapter by asking you to take your friend's name as input. The solution is simple, you will use '<>' operator. If you have to ask the name of two friends, then you will use '<>' two times. Now, suppose you have to take the input of the name of 50 students.
No, no, no, using '<>' 50 times is not a good option. This kind of situation can be easily handled using a loop. You will learn while and until loop in this chapter.

A loop does the same work as its name. It repeats the codes inside its body to a fixed number of times ( not necessarily ). Let's see an example of while loop to print the multiplication table of 14 on the screen:

```\$i = 1;
while (\$i<=10){
print \$i*14,"\n";
\$i++;
}
```
14
28
42
56
70
84
98
112
126
140

So, the syntax of while loop is

```while (condition){
statement
statement
statement
statement
....
}
```

A loop will run till its condition gets satisfied or condition is true. The condition of the while loop in the above question is \$i<=10. This means that the codes inside the while loop will repeat if 'i' is less than or equal to 10.

Initially, the condition of while \$i<=10 gets satisfied since the value of i is 1 ( i is less than 10 ). Thus, the codes inside the body of 'while' gets executed and 'i*14' (which is 14*1) i.e. (14) gets printed on the screen.
In the next line, \$i++ increases the value of i by 1, making its value equal to 2.
Again the condition of while loop with 2 as the value of 'i' is checked. This time also, the condition of the loop is satisfied ( 2 is less than 10 ) and 'i*14' ( 2*14 ) i.e., 28 gets printed on the screen and \$i++ further increases its value by 1 thus making it 3.
Similarly, in the 10th iteration, 'i' becomes 10 and '\$i<=10' gets satisfied and 'i*14' ( 10*14 ) i.e. 140 gets printed on the screen and 'i' gets increased to 11.
Again checking the condition of while loop (\$i<=10) with 'i' equals 11, the condition is not satisfied and the loop stops.

So now you know that in the above example, while loop will stop when i will be greater than 10. We use {} in while loop also to represent its body as used with if, else clause.

### Example including if and else

```#initially more is 1 to run the while loop for atleast once
\$more = 1;
while (\$more==1){
#Taking marks from user for marks
print "Enter you name\n";
\$name = <>;
chomp(\$name);
print "Maths marks\n";
\$maths_marks = <>;
chomp(\$maths_marks);
print "Science marks\n";
\$science_marks = <>;
chomp(\$science_marks);
print "English marks\n";
\$english_marks = <>;
chomp(\$english_marks);
print "Computer marks\n";
\$comupter_marks = <>;
chomp(\$computer_marks);
\$total = \$maths_marks+\$science_marks+\$english_marks+\$comupter_marks;
\$percentage = (\$total/400)*100;
print \$name," your total marks is ",\$total," and your percentage is ",\$percentage,"\n";

#User have to enter y if he want to run it again
print "Want to enter more y/n\n";
\$a = <>;
chomp(\$a);
if (\$a ne "y"){
#if user enters other than 'y' then making 'more' 0 to stop the loop. As condition in while will not be satisfied then
\$more = 0;
}
}
```
sam
Maths marks
90
Science marks
92
English marks
85
Computer marks
94
sam , your total marks is 361 and your percentage is 90.25
Want to enter more y/n
y
rai
Maths marks
98
Science marks
82
English marks
89
Computer marks
90
rai , your total marks is 359 and your percentage is 89.75
Want to enter more y/n
n

The code inside the body of while is simple. It is taking marks as input and calculating the percentage and printing it on the screen.
After printing the total marks and percentage, the user is asked to press 'y' or 'n' depending on whether the user wants to calculate more or not.
Now, if the user enters 'n', the value of more will become 0 and the condition of the loop ( \$more == 1) will not be satisfied and thus the loop will stop. But if the user enters 'y', then there will be no effect on the variable more, and the loop will be executed again.

The above 'while' will run till 'more' is true and it can change if we don't give 'y' to 'a'. (if \$a ne "y" -> more = 0). And if we enter 'y', then the whole loop will run again because 'more' is not changed and is still true.

## Infinite loop

As mentioned above, it is possible for a loop to run forever if its condition is always true. Let's see an example:
```while (1){
print "Perl\n"
}
```
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
...
Press ctrl+c to stop this loop

## Nesting of loops

Nesting means having one loop inside the body of another loop. It means to have a loop inside the body of other loop. You have already studied about having one 'if' under another, it is also similar. Let's first see an example.

```\$a = 5;
\$b = 1;
while (\$a>0){
while (\$b<=5){
print "*"x\$b,"\n";
\$b++;
\$a--;
}
}
```
*
**
***
****
*****

Here, a is 5 and b is 1.
In the first iteration of the outer while loop, 'a' is 1 and the inner while loop is inside the body of the outer while loop. So, the inner while loop will be executed and "*"x1 ( b is 1 ) i.e., "*" will be printed and the values of 'b' and 'a' will become 2 and 4 respectively.
Now, the inner while loop will be executed again ( as 'b' is 2 and \$b<=5 ), so "*"x2 i.e., "**" will be printed and the values of both 'b' and 'a' will become 3.
Again, only the inner loop will be executed and "*"x3 i.e., "***" will be printed. In the last iteration of the inner while loop (with b equals 5), "*"x5 i.e., "*****" will be printed and the values of 'b' and 'a' will become 6 and 0 respectively.
Again the condition of the inner while loop will be checked but it will be found false ( since the value of 'b' is 6 ).
Now, the second iteration of the outer while will occur but since a is 0, so its condition will also be false. So, it will also stop.

In short, there is nothing new in nesting of loops. The inner loop is like the other statements of a loop, which after being executed once, will be completed first and then the rest of the statements of the outer loop will be executed.
Let's have a look at one more example on this:

```\$a = 5;
while (\$a>0){
\$b = 1;
while (\$b<=5){
print "*"x\$b,"\n";
\$b++;
}
\$a--;
}
```
*
**
***
****
*****
*
**
***
****
*****
*
**
***
****
*****
*
**
***
****
*****
*
**
***
****
*****

Try to understand this example yourself. Just go step by step with every while loop and you will understand this.

## last and next

last is used to terminate a loop while next is used to skip the rest of the codes in the loop body and execute the loop again.

By default, last and next operate on the innermost loop. However, we can control the loop to which they relate by labeling the required loop. This will be clearer after some examples.

### last

last is used to come out of the loop whenever we want. Whenever a loop encounters 'last', it just stops there and executes the rest of the code written after the loop. It is similar to the break of C. See this example to understand it more clearly.

```#initially more is true to run the while loop for atleast once
while (1){
#Taking marks from user for marks
print "Enter you name\n";
\$name = <>;
chomp(\$name);
print "Maths marks\n";
\$maths_marks = <>;
chomp(\$maths_marks);
print "Science marks\n";
\$science_marks = <>;
chomp(\$science_marks);
print "English marks\n";
\$english_marks = <>;
chomp(\$english_marks);
print "Computer marks\n";
\$comupter_marks = <>;
chomp(\$computer_marks);
\$total = \$maths_marks+\$science_marks+\$english_marks+\$comupter_marks;
\$percentage = (\$total/400)*100;
print \$name," your total marks is ",\$total," and your percentage is ",\$percentage,"\n";

#User have to enter y if he want to run it again or n to terminate
print "Want to enter more y/n\n";
\$a = <>;
chomp(\$a);
if (\$a eq "n"){
last;
}
}
```
sam
Maths marks
90
Science marks
92
English marks
85
Computer marks
94
sam , your total marks is 361 and your percentage is 90.25
Want to enter more y/n
y
rai
Maths marks
98
Science marks
82
English marks
89
Computer marks
90
rai , your total marks is 359 and your percentage is 89.75
Want to enter more y/n
n

while (1) : The condition of the while loop is always true. This means that the loop will run forever ( infinite loop ). To stop this, we can use last and we have used it.
if \$a eq "n" -> last; : If a user enters 'n' ( n for no ), then the loop will stop there. Any other statement of the loop will not be further executed. It is that simple. The remaining code is just the same as used in the previous example.

We can also label loops to specifically terminate the desired loop instead of the innermost. Let's see an example:

```\$i = 5;
LOOP_OUTER: while (\$i>=0){
\$j = 5;
print "i is ",\$i,"\n";
LOOP_INNER: while (\$j>=0){
print "j is ",\$j,"\n";
if(\$i==3){
last LOOP_OUTER;
}
\$j--;
}
\$i--;
}
```
i is 5
j is 5
j is 4
j is 3
j is 2
j is 1
j is 0
i is 4
j is 5
j is 4
j is 3
j is 2
j is 1
j is 0
i is 3
j is 5

LOOP_OUTER is the label for the outer loop and LOOP_INNER is the label for the inner loop. You can think that we are giving names to these loops. Now, last LOOP_OUTER; will terminate the loop with the label LOOP_OUTER and not the innermost loop. So, when j became 3, it just terminated the outer loop.

### next

next passes control to the conditional test i.e. where the condition is checked. We can also use labels with next, but by default, it will work on the innermost loop. It is similar to 'continue' of C. Let's see its example.

```\$i = 5;
while (\$i>=0){
if (\$i == 2){
#skip the iteration
\$i--;
next;
}
print \$i,"\n";
\$i--;
}
```
5
4
3
1
0

In the above example, when 'i' became 2, then 'next' got executed and the control went to the while loop without executing the further statements. So, 2 didn't get printed in the output.

Let's see an example of using next on labeled loops.

```\$i = 2;
OUTER: while (\$i>=0){
print "i is \$i\n";
\$j = 2;
INNER: while (\$j>=0){
if (\$i == 1){
#skip the iteration
\$i--;
next OUTER;
}
print "j is \$j\n";
\$j--;
}
\$i--;
}
```
i is 2
j is 2
j is 1
j is 0
i is 1
i is 0
j is 2
j is 1
j is 0

You can see that when i became 1, the control passed to the loop with the label OUTER(outer loop) without executing further statements. That's why no value of 'j' was printed when 'i' was 1.

## Making our own digital dice

Though this is not graphical but we will construct the working structure. You can learn to link graphics to this or any game after completing this course. For now, let's do this first.

```print "Give lower limit of dice\n";
\$a = <>;
chomp(\$a);
print "Give upper limit of dice\n";
\$b = <>;
chomp(\$b);
print "type q to Quit or any other key/enter to continue\n";
while (1){
#rand is generating random number between 0 to b-a+1.(excluding b-a+1)
print ">>> ",\$a+int(rand(\$b-\$a+1)),"\n";
\$z = <>;
chomp(\$z);
if (\$z eq "q"){
#if 'q' is entered then come out of loop
last;
}
}
```
Give lower limit of dice
1
Give upper limit of dice
6
type q to Quit or any other key/enter to continue
>>> 1
>>> 2
>>> 2
>>> 5
>>> 4
>>> 3
>>> 6
>>> 3
q

rand function is used to generate random numbers. rand(N) will generate a random number between 0 and N. (excluding N).

## until

until is another loop provided by Perl. It is exactly the opposite of while loop. Thus, it repeats the statements inside its body till its condition is false. Let's understand this with an example.

```\$i = 0;
until (\$i>5){
print \$i,"\n";
\$i = \$i+1;
}
```
0
1
2
3
4
5

When 'i' becomes greater than 5, then the condition ( \$i>5 ) becomes true and the loop stops. Yes, as I mentioned earlier, it is the opposite of a while loop.

## do...while loop

It is just like 'while' but the only difference is that the code in its body is executed once before checking the conditions. It means that the code will be executed once even if the condition is false.

Now, let's see an example for printing 'Hello World' 10 times but this time with do...while loop.

```\$a = 1 ;
do{
print ( "Hello World\n" ) ;
\$a++;
}while ( \$a <= 10 );
```
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Let's understand this.

At first, the codes inside the body of loop (i.e. within the braces { } following do ) are executed without checking the condition. This will print 'Hello World' and a++ increments the value of 'a' by 1. So now, the value of 'a' becomes 2.

Once the code inside the braces { } is executed, the condition 'a <= 10' is checked. Since the value of 'a' is 1, so the condition is satisfied.

Again the code inside the body of loop is executed and the value of 'a' becomes 2. When the value of 'a' is 10 and 'Hello World' is printed for the tenth time, a++ increases the value of 'a' to 11. After this, the condition becomes false and the loop terminates.

## continue

continue is a less used command in Perl. It is different from continue statement of C. So, don't confuse with it. Statements written in continue are executed after every iteration of the loop and before checking the condition again. It is similar to writing statements at the last of a loop. The example given below will make you understand this.

```\$i = 5;
while (\$i>0){
print "i is \$i\n";
}continue{
\$i--;
}
```
i is 5
i is 4
i is 3
i is 2
i is 1

Yes, it is similar to:
while (\$i>0){
print "i is \$i\n";
\$i--;
}

The codes written inside continue will be executed after every iteration before checking the condition.

## redo

redo restarts the loop block without evaluating the condition again. So, it is similar to next but the only difference is that it doesn't evaluate the condition when executed.

```\$i = 5;
while (\$i>0){
print "i is \$i\n";
if (\$i == 1){
\$i--;
redo;
}
\$i--;
}
```
i is 5
i is 4
i is 3
i is 2
i is 1
i is 0

You can see that 'i is 0' is also printed in the output of the program. This happened because the condition was not checked after the execution of redo.

## goto

goto is used to jump to a particular statement and resume execution from there.

```\$i = 5;
LOOP: while (\$i>0){
if (\$i == 2){
\$i--;
goto LOOP;
}
print "i is \$i\n";
\$i--;
}
```
i is 5
i is 4
i is 3
i is 1

We just saw three different types of loops which are used to repeat a certain process some number of times and they were fun as well, I hope. In the next chapter, you will learn about array for storing more than one data.