“While” and “do…while” statements

Hello ! Today I thought I would talk about 2 other kinds of loops, the “while” and the “do…while” statements.Enjoy!

The while statement continually executes the same block of code as long as a condition is true.

Syntax:

while (condition){

//code here

}

You can implement an infinite loop by using the while statement as follows:

while(true){

//code here

}

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.

do-while syntax:

do{

//your code here

}while(condition);

Notice how there is a semicolon at the end of the do-while statement. This is the only statement that ends in a semicolon so be careful not to forget using it ,thou I am pretty sure that the compiler will keep that in mind for you.

I hope this helped your understanding of the “while” and “do-while” statements.

Keep on coding 😉 !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s