WHILE LOOP EXAMPLE
while (boolean-expression){
Statement //Boody of the loop
}
import textio.TextIO;
/* Program to print welcome to Java based on user suggested numbers*/
class Main {
public static void main(String[] args) {
int i;//Declare variable
int j;//Declare variable
/* Initialise variable i*/
System.out.println( "Enter an integer to initialise i: ");
i = TextIO.getInt();//Initialising i by providing an integer
System.out.println( "Enter an integer to initialise j: ");
j = TextIO.getInt();//Initialising j by providing an integer
/* Condition and statement block*/
while(i <= j){
System.out.println("Welcome to Java");
/* Update expression*/
i++;
, }
}
}
PROGRAM OUTPUT
Enter an integer to initialise i: 1
Enter an integer to initialise j: 5
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
-----------------------------------------------------------------------------
Do..While Example
import textio.TextIO;
/* Program to print welcome to Java based on user suggested numbers*/
public class doWhileLoop {
public static void main(String[] args) {
int i;//Declare variable
int j;//Declare variable
/* Initialise variable i*/
System.out.println( "Enter an integer to initialise i: ");
i = TextIO.getInt();//Initialising i by providing an integer
System.out.println( "Enter an integer to initialise j: ");
while (boolean-expression){
Statement //Boody of the loop
}
import textio.TextIO;
/* Program to print welcome to Java based on user suggested numbers*/
class Main {
public static void main(String[] args) {
int i;//Declare variable
int j;//Declare variable
/* Initialise variable i*/
System.out.println( "Enter an integer to initialise i: ");
i = TextIO.getInt();//Initialising i by providing an integer
System.out.println( "Enter an integer to initialise j: ");
j = TextIO.getInt();//Initialising j by providing an integer
/* Condition and statement block*/
while(i <= j){
System.out.println("Welcome to Java");
/* Update expression*/
i++;
, }
}
}
PROGRAM OUTPUT
Enter an integer to initialise i: 1
Enter an integer to initialise j: 5
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
-----------------------------------------------------------------------------
Do..While Example
import textio.TextIO;
/* Program to print welcome to Java based on user suggested numbers*/
public class doWhileLoop {
public static void main(String[] args) {
int i;//Declare variable
int j;//Declare variable
/* Initialise variable i*/
System.out.println( "Enter an integer to initialise i: ");
i = TextIO.getInt();//Initialising i by providing an integer
System.out.println( "Enter an integer to initialise j: ");