The while statement

The while statement

The simplest of all the looping structure in java is while statement. The while is an entry controlled loop statement. The test condition is evaluated, and if the test condition is true , then the body of the loop is executed. After the execution of the body the test condition is once again evaluatedand if it is true the body is once gain executed. The process of repeated execution of the body continues until the test condition finally becomes false and control is transferred out of the loop. On exit the program continues with the statement immediately after the body of the loop.

Example :Program illustration for while loop

Class whiletest

{

Public static void main(String args [ ])

{

Stringbuffer string = new stringbuffer();

Char c ;

System.out.println( “ Enter a string “);

Try

{

While(( c =(char)system.in.read())! ‘ ’)

{

String.appendic(c);

}

}

Catch(Exception e)

{

System.out.println( “ Error in input “);

}

System.out.println( “You have entered “);

System.out.println(string);

}

}

 

Note :Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control