Loops

Almost all programming languages provide a concept called a loop. Without loops, writing programs could take forever. It is one of the most useful programming tools.

Imagine you are a graphic designer; you are asked to produce 100 sketches of the same image. You can accomplish this task by drawing all 100 images with a pencil; however, that will take a long time to finish. A smart designer will mold the design onto a wheel, this way they can produce several images by simply dipping the wheel into ink then rolling the wheel on a canvas. As long as there is enough ink on the wheel, the wheel will produce more images. The same concept applies to programming loops. A loop runs the same sequence of instructions repeatedly until a certain condition has been met, or the specified number of repeats has been met.

 


While Loops

A while statement is statements that will keep running until its condition is no longer true.

Let’s say we wanted to count down, we will keep reducing a number until we get to zero.

This will keep running the code in while block over and over again until (number > 0) is no longer true; inside the block the number is printed then reduced by 1 each time.

So let’s say the value of number is 5. The output of the program will be

5
4
3
2
1
*to avoid writing a program that will make your computer crash, make sure that your loops do not run forever (infinite loop). Ex if my loop is “while(num>0)” num must be counting down, not up. Otherwise, the loop will have no end

 

Ex.

The objective of this program is to keep asking a user to enter a password until the correct password is entered.

 

For Loops

When we want to run a part of a program over and over again, but we only want to run it for a specific number of times we have to use a for loop.  The basic structure of a for loop is as follows

for( define variable; condition; iteration){
    Code to run while condition is true;
}

So a simple loop that prints ‘Hey’ five times would look like this

This would print:

Hey you!  Hey you!  Hey you!  Hey you!  Hey you!

Now suppose instead of printing Hey! 5 times we wanted to print it 100 times, all we have to do is change the 5 to 100

for( int i=1; i<=100; i++){
   System.out.print(" Hey you! ");
}

The variable i starts at 1 (int i=1); i gets incremented by one each time (i++); the loops only keeps running while ‘i’ is less than or equal 100 (i<=100); so the code in the { } bracket will run 100 times.

How may times will each loop run for?

for (int a=0; a<5; a++) {
   //loop 
}

 

for (int b=1; b<8; b++) {
   //loop 
}

 

for (int c=10; c>2; c--) {
   //loop 
}

 

for (int c=0; c<7; c--){
   //loop 
}

 

while (5>9){
   //loop 
}

 

int d=2;
while (d!=7){
   //loop
   d = d+1;
}

For loops can also be used to produce some interesting calculations.. Let’s say we want to automatically create the times table for 3. We will calculate 3X1 then 3X2 then 3X3 all the way to 3X8.

This will print:

3X1=3  3X2=6  3X3=9  3X4=12  3X5=15  3X6=18  3X7=21  3X8=24

We can put a loop inside another loop (nested loops). Let’s say instead of just the 3 times table we wanted to print the same times table for 1 all the way to 10.

We put the previous loop into another loop that runs 10 times; and we replace 3 with the variable j which goes from 1 to 10. This will print

1X1=1  1X2=2  1X3=3  1X4=4  1X5=5  1X6=6  1X7=7  1X8=8
2X1=2  2X2=4  2X3=6  2X4=8  2X5=10  2X6=12  2X7=14  2X8=16
3X1=3  3X2=6  3X3=9  3X4=12  3X5=15  3X6=18  3X7=21  3X8=24
4X1=4  4X2=8  4X3=12  4X4=16  4X5=20  4X6=24  4X7=28  4X8=32
5X1=5  5X2=10  5X3=15  5X4=20  5X5=25  5X6=30  5X7=35  5X8=40
6X1=6  6X2=12  6X3=18  6X4=24  6X5=30  6X6=36  6X7=42  6X8=48
7X1=7  7X2=14  7X3=21  7X4=28  7X5=35  7X6=42  7X7=49  7X8=56
8X1=8  8X2=16  8X3=24  8X4=32  8X5=40  8X6=48  8X7=56  8X8=64
9X1=9  9X2=18  9X3=27  9X4=36  9X5=45  9X6=54  9X7=63  9X8=72
10X1=10  10X2=20  10X3=30  10X4=40  10X5=50  10X6=60  10X7=70  10X8=80

Program: Write a program that uses a for loop to calculate any number X 5. Hint- Add the number to itself 5 times

Back to Course Menu
What do you think about this topic? Share your suggestions / comments