Java-do-while loop / Chapter -4/Flow Control




b). do -while loop:-
If we want to execute loop body at-least once then we should go for the do-while loop.

Syntax:-

do
{
         body
}  while (b);         

                         where ; the semicolon is mandatory and b always should be a boolean type.



1. The arguments should be a boolean type . If we are trying to provide any other type then we will get compile time error.

example:
int b=10;
do
{
         body

}  while (b); 

Compile Time Error: Incompatible Type
Found:int
Required:boolean





2.Curly braces{ } are optional and without Curly braces { } we can take one statement under
while which should not be the declarative statement.

Example:-

do
 System.out.println("Hello");
while(true);
                                                                             valid



Example:-

do;
while(true);                                                   valid (semicolon is valid java statement)


Example:-

do                                                                          invalid

 while(true);


Example:-

do
  int x=10;                                                            invalid

 while(true);


                                                                     
Example:-

do
{
    int x=10;                                                            valid

}  while(true);

                 
                Important Loop Holes in do-while loop

1.Take  a look at the following expression which looks like an invalid but they are perfectly valid.

    example:- 
   do  while(true)
            System.out.println("JavaByChetan");
           while(false);

In the above expression, it looks like while the argument is false so no output will be produced but it is not correct .
In do-while execution if flow from do to while that 's why this statement will be executed and we will get the output:
 JavaByChetan
JavaByChetan
infinite times...
 This Expression will be Executed like this:

do  
while(true)
System.out.println("JavaByChetan");

while(false);


2.Unreachable Statement 

example 1:-
do  
{
System.out.println("Hello");

}while(true);

System.out.println("Hii");                             Compile Time Error: Unreachable state


example 2:-
do  
{
System.out.println("Hello");

}while(false);


System.out.println("Hii");            output: Hello
                                                             Hii

example 3:-
int a=10,b=20;
do  
{
System.out.println("Hello");

}while(a<b);



System.out.println("Hii");            output: Hello

                                                            Hello.....infinite times



example 4:-
int a=10,b=20;
do  
{
System.out.println("Hello");

}while(a>b);



System.out.println("Hii");            output: Hello
                                                             Hii





example 5:-
final int a=10,int b=20;
do  
{
System.out.println("Hello");

}while(a<b);




System.out.println("Hii");            Compile Time Error: Unreachable state
                                                             



example 6:-
final int a=10,int b=20;
do  
{
System.out.println("Hello");

}while(a>b);



System.out.println("Hii");               output: Hello                                                          

                                                                Hii



No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com