Java Beginner: Lesson 5

while loop


In order to repeat a certain action or set of actions like in most programming languages even in Java there are loops that make possible such an action. In this lesson we will see the while loop.

In this lesson we will continue customizing our hello world application with which we have worked in the previouse lessons we will add a while loop into it. This loop will help us repeat operation and not exit the application after the first operation.

The state of the main method before we add the for loop is as shown bellow.

Example code:
public static void main(String[] args) {
// TODO code application logic here

Scanner scanner = new Scanner(System.in);
double nr1=0;
double nr2=0;
String operation;
double result =0;
System.out.println("Give the first number: ");
nr1= scanner.nextDouble();
System.out.println("Give the operation: ");
operation = scanner.next();
System.out.println("Give the second number: ");
nr2= scanner.nextDouble();
if(operation.equalsIgnoreCase("+")){
result = nr1+nr2;
}else if(operation.equalsIgnoreCase("-")){
result = nr1-nr2;
}else if(operation.equalsIgnoreCase("*")){
result = nr1*nr2;
}else if(operation.equalsIgnoreCase("/")){
if(nr2!=0){
result = nr1/nr2;
}else{
System.out.println("Can not devide by 0\n");
return;
}
}

System.out.println("The result of nr1 and nr2 is "+ result);
}

We will need to repeat the part of code since we ask for input from the user until before the end of main method by reaching the following result.

Example code:
public static void main(String[] args) {
// TODO code application logic here

Scanner scanner = new Scanner(System.in);
double nr1=0;
double nr2=0;
String operation;
double result =0;
while( true ){
System.out.println("**********NEW OPERATION*********");
System.out.println("Give the first number: ");
nr1= scanner.nextDouble();
System.out.println("Give the operation: ");
operation = scanner.next();
System.out.println("Give the second number: ");
nr2= scanner.nextDouble();
if(operation.equalsIgnoreCase("+")){
result = nr1+nr2;
}else if(operation.equalsIgnoreCase("-")){
result = nr1-nr2;
}else if(operation.equalsIgnoreCase("*")){
result = nr1*nr2;
}else if(operation.equalsIgnoreCase("/")){
if(nr2!=0){
result = nr1/nr2;
}else{
System.out.println("Can not devide by 0\n");
return;
}
}

System.out.println("The result of nr1 and nr2 is "+ result);
}
}

Where the new lines are highlighted in yellow. The wile loop takes into brackets a boolean value, which in our case is the value true, and the actions defined by the statments within the curly brackets of while loop will repeat for as long as this value will be true, in our case infinitely. We will see in the next lessons more complex implementation of the while loop which end after a certen condition is fulfilled by making the condintion within the the brackets of while loop false or by using the break statement.

Bellow in the figure 1 is shown an example of the execution of the HelloWorld application with the while loop added.


Figure 1: while loop

(please report broken link in the comment)

Comments

Popular posts from this blog

Free host and domain

C++ Beginner: Lesson 3 - Simple calculator

C++ Beginner: Lesson 4 - Tic Tac Toe game