Creating Your First Java Classes
- Features of the Java Programming Language
- Java Program Types
- Saving a Java Class
- Compiling a Java Class
- Running a Java Class
- Modifying a Java Class
Looping
A loop is a structure that allows repeatedexecution of a block of statements. Within a looping structure, a Booleanexpression is evaluated. If it is true, a block of statements called the loopbody executes and the Boolean expression is evaluated again. As long as theexpression is true, the statements in the loop body continue to execute. Whenthe Boolean evaluation is false, the loop ends. One execution of any loop iscalled an iteration.
Figure: Flowchart of a loop structure
Types of Loops
There are three types of loops.
1. A while loop, in which the loop controlling Boolean expression is the first statement in the loop
2. A for loop, which is usually used as a concise format in which to execute loops
3. A do…while loop, in which the loop controlling Boolean expression is the last statement in the loop
The while loop
Syntax
while(conditional statement is true){
//Block of codes come here to perform a particular task.
// update the value of your control variable to make it false at a particular point in time
}
NOTE
1. Initialize the variable that will make the condition to be true in order for the loop to be executed at least once.
2. For a while loop to end at a particular point in time, the variable that makes the condition to be true needs to be updated in order to make the condition false at a particular point in time during the iteration(s)
Examples
Write a program that prints 1 to 10. Thus outputs something like this:
1
2
3
4
5
6
7
8
9
10
Sample Code
public class main
{
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
while(i<=10){
System.out.println(i);
//Increase i so that i will be more than 10 at a point in time to end loop
i++;
}
}
}
NOTE:
The loop will never end if we never increase i. This is because the condition will always be true since i is initialized to 1 and 1 is less than 10.
i++ is the same as i = i+1
++ is an increment and -- is a decrement
Write a program that prints 10 to 1. Thus the program should output something like this:
10
9
8
7
6
5
4
3
2
1
Sample Code
public class main
{
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
while(i>=1){
System.out.println(i);
//decrease i so that i will be less than 1 at a point in time to end loop
i--;
}
}
}
Summary Explanation
Figure: A while loop that displays “Hello” twice
for loop
Syntax:
for(initialize variable; condition to end loop; increment or decrement to change initialized variable){
//block of code to run
}
Examples
Write a program that prints 1 to 10. Thus outputs something like this:
1
2
3
4
5
6
7
8
9
10
Sample Code
public class main
{
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
Write a program that prints 10 to 1. Thus the program should output something like this:
10
9
8
7
6
5
4
3
2
1
Sample Code
public class main
{
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=10;i>=1;i--){
System.out.println(i);
}
}
}
do…while Loop
For the while loop and for loop, the loop body might execute many times, but it is also possible that the loop will not execute at all when the initial condition that will make the loop iterate is false.
Both while loops and for loops are pretest loops—ones in which the loop control variable is tested before the loop body executes.
Sometimes, you might need to ensure that a loop body executes at least one time. If so, you want to write a loop that checks at the “bottom” of the loop after the first iteration. The do…while loop checks the value of the loop control variable at the bottom of the loop after one repetition has occurred. The do…while loop is a posttest loop—one in which the loop control variable is tested after the loop body executes.
Figure: General structure of a do…while loop
Example
Write a program that continually ask a user to enter an even number. The program should quit if a user enters an odd number.
Sample Code
import java.util.Scanner;
public class main
{
public static void main(String[] args) {
// TODO Auto-generated method stub
//Initialize the even number variable
int even_number = 2;
//Create the scanner object to get input from the user
Scanner inputDevice = new Scanner(System.in);
do{
//Inform user to enter an even number
System.out.println("Enter an even number");
//Get the entered value from the keyboard
even_number = inputDevice.nextInt();
}while(even_number%2==0);//Repeat iteration if entered number is even
}
}
NESTED LOOP
Just as if statements can be nested, so can loops. You can place a while loop within a while loop, a for loop within a for loop, a while loop within a for loop, or any other combination.
When loops are nested, each pair contains an inner loop and an outer loop. The inner loop must be entirely contained within the outer loop; loops can never overlap.
Example
Write a programthat outputs the multiple table as shown below:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|
2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 |
3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 |
4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 48 |
5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 |
6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 72 |
7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | 77 | 84 |
8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 |
9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 | 99 | 108 |
10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 110 | 120 |
11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 | 110 | 121 | 132 |
12 | 24 | 36 | 48 | 60 | 72 | 84 | 96 | 108 | 120 | 132 | 144 |
Sample Code
public class NestedLoopExample
{
public static void main(String[] args) {
for(int i=1;i<=12;i++){
for(int j=1;j<=12;j++){
System.out.print(i*j+"");
}
//move to a new line before the next outer loop iteration
System.out.println("");
}
}
}
Explanation
for(int i=1;i<=12;i++){
}
The above for loop is the outer loop and
for(int j=1;j<=12;j++){
System.out.print(i*j+"");
}
and the above for loop is the inner loop. Notice that the integer variable which controls the loops are different in eachloop. When using nested loops, your inner loop control variable should be different from the outer loop control variable. In the above example variable i is used for the outer loop and j is used for the inner loop.
The inner loop has to complete iteration before the next iteration of the outer loop. For the above example, when i is 1, the inner loop has to iterate from j = 1 to j =12 before the next iteration of the outer loop, i =2 and so on.
ASSIGNMENT
Write a program that takes an integer from the user and output the multiplication table for that user. For instance if a user enters 2, the output should look something like this:
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
5 x 2 = 10
6 x 2 = 12
7 x 2 = 14
8 x 2 = 16
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24
Write a program that takes a number from a user 10 times and display the sum of the numbers. Use a loop to take the numbers 10 times.