while loop and do-while loop

After long time had a time to write a blog post.

Today I will  teach u about while loop and do-while loop in java.

Firstly i will explain u about the while loop

while loop

The while statement continually a block of statements while a particular condition is TRUE.

Its syntax can be expressed as :

while ( expression ){
         statement(s);
}

 Example 1 : while loop


you can also make a infinite loop also using the while loop.

while ( true ){
      // your code comes here
}

i will explain a another simple code with while loop

Example 2 : while loop


do-while loop

Now I will explain u about the do-while loop.

The java programming language also provides a do-while statement , which can be expressed as follows.

do{
     statement ( s )
}while ( expression );


The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top.Therefore, the statements within the do block are always executed at least once.

Let's see a example for do-while loop.

Example 1 : do-while loop



You can see the condition of the above do-while loop is not true.But the statement in the do block is executed once and gave the output.

So that's about the while loop and do-while loop.

I think u get a knowledge about loops from this small post.
See u soon.
Thank u.

Comments

Popular posts from this blog

Algorithm I

love java programming

Loops