While

Hello everyone...
I just wanted to know that while using the while loop is it alright to do this:

while(left()) {
//do some thing
}

//left is a function

please help me out thanks to anyone who tries to help me...

That is OK as long as the left() function returns a value that can be correctly interpreted as true or false.

Yes, provided that left() returns a datatype that can be zero or non-zero. It is often better form to make an explicit expression that shows what you are testing (although it is actually a test for non-zero) For example:

while (left() == true) // boolean
while (left() != 0) // integer
while (left() != '\0') //char

What type does left() return?

In the early days of C, while(left()) would be preferred because it compiled into less machine code. Today, most of the compilers will optimize any "redundant" expressions such as the three examples I gave above, so that it uses no more code than while(left()). If left() is a function returning a boolean, it can be made more readable by naming it as a proposition and used this way:

while ( somethingIsLeft() )

And why not include the function "left()" inside the while loop? Something like:

while(var == true) {
var = left();
//do some thing
}

luisilva:
And why not include the function "left()" inside the while loop? Something like:

while(var == true) {

var = left();
//do some thing
}

No. That does not work the same. It doesn't work the first time through the block.

aarg:
No. That does not work the same. It doesn't work the first time through the block.

Yes, you are right. So, you can do something like:

do {
var = left();
//do some thing
} while(var == true);

or:

boolean var = true;
while(var == true) {
var = left();
//do some thing
}

No. A do-while always performs one iteration of the code block. That is different logic. Your second example works, almost. But no. It would have to be:

boolean var = left();
while(var == true) {
var = left();
//do some thing
}

aarg:
No. A do-while always performs one iteration of the code block. That is different logic. Your second example works, almost. But no. It would have to be:

boolean var = left();

while(var == true) {
var = left();
//do some thing
}

Can't it also be:

while((var = left()) == true)

Or then even

while(var = left())

Not that I advocate such a thing.