Suppose we want to display hello on output screen five times in five different lines.
We might think of writing either five printf statements or one printf statement consisting of constant “hello\n” five times.
What if we want to display hello 500 times?
Should we write 500 printf statement or equivalent ?
Obviously not.
It means that we need some programming facility to repeat certain works. Such facility in Programming Languages is available in the form Looping Statements.
Loops are used to repeat something. Also known as Repetition of block of code.
If program requires that group of instructions be executed repeatedly is known as looping.
Looping is of two types
Conditional
Unconditional
Conditional Looping: Computation continues indefinitely until the logical condition is true.
Unconditional Looping : The number of repetition known in advance or repetition is infinite.
Types of Loops in C
Conditional Loops
- While Loop – Pre Condition Loop – Entry Time Conditional Loop
- Do-While Loop – Post Condition Loop – Exit Time Conditional Loop
Unconditional Loop
- For Loop – Counted Loops
- Uncounted Loop – Infinite loop
- Uncounted Loop –infinite loop controlled by control statements.
While Statements
The while statement is used to carry out looping operations, in which a group of statement is executed repeatedly, until some condition has been satisfied.
The general form of statement is
while (test expression)
statement to be executed;
or
while (test expression)
{ statements to be executed }
The statement will be executed repeatedly as long as the test expression is true. The statement can be simple or compound, though it is usually a compound statement.
It must include some features that eventually alters the value of the expression, thus providing stopping condition for the loop.
However, an empty condition is not legal for a while loop as it is with a for loop(discussed in next slides).
Programming with C – Lecture 1
Programming with C – Lecture 2
I found useful article in your blog. thank you for sharing nice info
Nice article. Thank you to describe the while and do-while loop.
Really? It really is excellent to witness anyone ultimate begin addressing this stuff, however Im still not really certain how much I agree with you on it all. I subscribed to your rss feed though and will certainly keep following your writing and possibly down the road I may chime in once again in much more detail good work. Cheers for blogging though!