" while " statement is also used as the looping statement in any programming languages. It only works with the condition and we can use updation statement inside the block otherwise it will go for infinite loop if the condition is true.
Syntax of while statement is :
while ( condition ) { }
Some of the invalid code with the use of while statement
while ( true ) { }
System.out.println (" Error ") ;
Syntax of while statement is :
while ( condition ) { }
Some of the invalid code with the use of while statement
while ( true ) { }
System.out.println (" Error ") ;
Here compiler give compilation error saying " unreachable statement " because it will go for infinite loop and never execute the statements after while block.
while ( false ){ System.out.println (" Error ") ; }
Here compiler give " unreachable statement " compilation error and condition is false so block statement will never execute.
while ( ) { }
The above syntax is incorrect because there must be some condition to be checked while executing while block.
Syntax of do-while statement :
do {
// statement
< updation > }
while ( condition ) ;
Here it will execute statement of do block even if while condition is false.
while ( false ){ System.out.println (" Error ") ; }
Here compiler give " unreachable statement " compilation error and condition is false so block statement will never execute.
while ( ) { }
The above syntax is incorrect because there must be some condition to be checked while executing while block.
" do-while " looping statement
" do - while " is same as " while " but the only difference is that in do-while the statement will execute at least once and rest execution will depend upon the condition.
Syntax of do-while statement :
do {
// statement
< updation > }
while ( condition ) ;
Here it will execute statement of do block even if while condition is false.
nice article for beginners.thank you.
ReplyDeletejava loop
java loop