Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!

While and do-while statement

do and while are keywords)
Normally these statements will be used if we don't know the number of repetitions. Both statements can be used to repeat one or more statements as long as condition is true.

while statement:

(statements to be typed here)

Condition - an expression whose value is 1 or 0.
Statement - any valid C++ statement.

Example 1:

Example 2:

Example 3: Null statement repeats infinite times as the condition will always be satisfied.

Example 4: Syntax Error - If the condition is missing, than it will generate an error at compile time.

note Any for loop can be converted into while loop and vice-versa. While loop is known as an entry controlled loop. If condition is initially false, then the loop will not execute.

2. do-while statement:


Example 1:

Example 2:

note do-while statement is an exit controlled loop. Even if the condition is initially false, the loop executes at least once. If condition is absent, then it will generate an error at compile time.

CACKLE comment system

Programming Resources
Computer Networking Fundamentals Android Application