answersLogoWhite

0

The while() statement is used in iterative structures (loops), such that a statement block will iterate so long as a conditional expression evaluates true. The conditional expression can be evaluated at the start or at the end of a loop, thus making it more flexible than the standard for() statement which always evaluates the conditional expression at the start of a loop.

In C++, the while() statement has the following forms:

while( condition ) {

statement;

}

do {

statement;

} while( condition );

In the first form, the statement block only iterates if the condition evaluates true. In the second form, the statement block is executed at least once, and only iterates if the condition evaluates true.

Infinite loops have the following forms:

while( 1 ) {

statement;

}

do { statement;

} while( 1 );

The latter form is redundant since both loops will iterate at least once, however the former is deemed more acceptable in terms of readability than the infinite for() statement:

for(;;) {

statement;

}

As with for() statements, while() loops can use break, return or goto to conditionally exit the loop at any point, or use continue to prematurely begin a new iteration.

Although for() and while() are interchangeable to an extent, the form you use is largely dependant upon which is more readable or makes the most sense in your code. If the condition must be evaluated at the end of the loop then you must use do..while(), but for all other loops you often have a choice of using for() or while(). However, there are differences. Generally speaking, for() statements are used whenever you need to operate upon a control variable in a consistent manner (such as incrementing an index value at each iteration) whereas while() statements are used when changes to the control variable are dependant upon other conditions which are determined within each iteration of the loop itself. Moreover, control variables in while() loops must be in scope prior to entering the loop and will remain in scope when the loop terminates, whereas for() loops can both declare and initialise control variables, rendering them local to the loop. Depending on the type of loop, these facts can help determine which is the better form to use, but in terms of performance there is no real difference. It's all a question of which form makes the most sense for the type of loop you are trying to control.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What does informatica mean in English?

Translation: Computer Science OR Computer Programming


What is the difference between online and offline programming?

Online programming is programming that has to occur while the computer or other device is connected to the internet. Offline programming doe not require an internet connection.


What does computer programming mean in general terms?

well .. talking about in general terms (like making some newbie to understand, what the computer programming is about) so we must say that computer programming is like ..Programming a computer to do some task. in programming we create a sequence of instructions that enables the computer to do something.like we try to make the computer understand what we want to do so that it can help us to do that work. or even sometimes the computer can do that task itself and give the desired output.sourabh882008@yahoo.com


Is computer design the same as computer programming?

Depends what you mean. Computer programming is the actual act of writing code for the computer. Computer design could mean two things. First, software design. This is not coding, so would thus not be considered computer programming. Usually it could mean either graphic manipulation or designing software by way of a visual aid. If you meant design as in the actual act of designing the hardware of a computer, then no. Obviously they are two completely different things.


What does label mean in computer terms?

It is used in programming to denote the name of an object


Does being good at math but bad at computer programming mean you are bad at science?

no.


What is the difference between a bug and an error?

"Bug" means "error" in Computer Programming. So it means they're exactly the same.


Is Python used for computer programming or software programming?

Computer programming is the process of making software. The expression software programming isn't much used, but would mean exactly the same.Python is a general purpose interpreted programming language. It is not suitable for highly optimised code, or for operating system programming, but a good choice for many other situations.


What do you mean by procedure oriented programming?

A list of instructions telling a computer, step by step, what to do.


What does crlf mean?

In computer programming, cr is carriage return, lf is line feed.


Advantages of computer programming?

The advantages of computer programming is that it pays well since programming is a creative task. Computer programming helps human beings solve problems on a regular basis.


What is the diffirence of computer programming and computer science?

Computer science is a more abstract study of computing techniques; which will include computer programming. Computer programming, as it implies, is the practise of writing programs for computers.