Java – While loop

In this post I will go through basic while loop’s. The while loop is very similar to the for loop with a small exception that instead of taking and initializing values, the while loop will only see if the conditional is true or not. Basically the while loop will just see if the value is something(or not something) that it is supposed to be, and it will continue the loop forever until the conditional is true(or not true depending on your program logic). Below is a sample code of the while loop

public class doWhile {

public static void main(String[] args) {

int x = 0;

  while(x < 10) {
    System.out.println(x);
    x++;
  }

}

}

and below is the sample output of the above example

0
1
2
3
4
5
6
7
8
9

The code is so self explanatory that it does not need any commenting but something that might be new is the do-while loop. do-while loop is a loop similar to while and for statement’s with a small difference that I will tell you after you take a look at the below code

public class doWhile {

public static void main(String[] args) {

int x= 0;

  do {
    System.out.println(x);
    x++;
  }while(x < 10);
}

}

So basically what happens in the do-while statement is that the compiler will go to the do { loop part and proceed with the loop until the conditional is right. So you see the program will do something whether or not the while loop conditional was supposed to check anything or not for example a wrong input etc. The output for the above example is same as the previous code.

In the below example I tried to demonstrate more the usefulness of the while-loop

import java.util.Scanner;

public class doWhile {

/**
* @author Kurosh Farsimadan
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“Enter a number between 1 – 20: “);
int number = scanner.nextInt();

if(number < 1 || number > 20) {
System.out.println(“WRONG INPUT”);
while(number < 1 || number > 20) {
System.out.println(“Enter a number between 1- 20: “);
number = scanner.nextInt();
}
System.out.println(“The number you entered was: “+number);
}else if(number >= 1 && number <= 20) {
System.out.println(“The number you entered was: “+number);
}

}

}
and below is the output of the values (picture)

pic22

and below is a picture of the code itself(I thought it would look more clear as a picture than if I was to copy paste my code here)

picture33

I am going to publish this, and add more content later gradually. Until then, take a look at my Java section for more updates if this somehow sparked your curiosity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s