Programming with C – “Control Statements – Loop”- Lecture 3

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

 

Published by

Gagan Deep

FOUNDER & DIRECTOR, ROZY COMPUTECH SERVICES, KUK, KURUKSHETRA (HARYANA), INDIA I am in the profession of teaching in Computer Science since 1996.  In 1996, established a professional setup “Rozy Computech Services” for providing Computer Education, computer hardware and software services. In this span of 21 years , in conjunction with Rozy’s, I also associated in Teaching with different Institutions like Assistant Professor in University College, Kurukshetra University, Kurukshetra, Guest Faculty in Directorate of Distance Education, Kurukshetra University, Visiting Faculty in University Institute of Engineering & Technology, Kurukshetra University and a Resource person in EDUSAT, Haryana. Besides, I am also serving as Guide and Mentor in various private educational institutions. Also written 7 books on computer science.

Leave a Reply

Your email address will not be published. Required fields are marked *