C Programming - Decision Making - Looping
C Programming - Decision Making - Looping
Looping
In this tutorial you will learn about C Programming - Decision Making - Looping, The
While Statement, The Do while statement, The Break Statement, Continue statement and
For Loop.
During looping a set of statements are executed until some conditions for termination of
the loop is encountered. A program loop therefore consists of two segments one known as
body of the loop and other is the control statement. The control statement tests certain
conditions and then directs the repeated execution of the statements contained in the body
of the loop.
The test may be either to determine whether the loop has repeated the specified number
of times or to determine whether the particular condition has been met.
The simplest of all looping structure in C is the while statement. The general format of
the while statement is:
Example program for generating ‘N’ Natural numbers using while loop:
In the above program the looping concept is used to generate n natural numbers. Here n
and I are declared as integer variables and I is initialized to value zero. A message is
given to the user to enter the natural number till where he wants to generate the numbers.
The entered number is read and stored by the scanf statement. The while loop then checks
whether the value of I is less than n i.e., the user entered number if it is true then the
control enters the loop body and prints the value of I using the printf statement and
increments the value of I to the next natural number this process repeats till the value of I
becomes equal to or greater than the number given by the user.
The Do while statement:
The do while loop is also a kind of loop, which is similar to the while loop in contrast to
while loop, the do while loop tests at the bottom of the loop after executing the body of
the loop. Since the body of the loop is executed first and then the loop condition is
checked we can be assured that the body of the loop is executed at least once.
Do
{
statement;
}
while(expression);
Here the statement is executed, then expression is evaluated. If the condition expression
is true then the body is executed again and this process continues till the conditional
expression becomes false. When the expression becomes false. When the expression
becomes false the loop terminates.
To realize the usefulness of the do while construct consider the following problem. The
user must be prompted to press Y or N. In reality the user can press any key other than y
or n. IN such case the message must be shown again and the user should be allowed to
enter one of the two keys, clearly this is a loop construct. Also it has to be executed at
least once. The following program illustrates the solution.
Sometimes while executing a loop it becomes desirable to skip a part of the loop or quit
the loop as soon as certain condition occurs, for example consider searching a particular
number in a set of 100 numbers as soon as the search number is found it is desirable to
terminate the loop. C language permits a jump from one statement to another within a
loop as well as to jump out of the loop. The break statement allows us to accomplish this
task. A break statement provides an early exit from for, while, do and switch constructs. A
break causes the innermost enclosing loop or switch to be exited immediately.
During loop operations it may be necessary to skip a part of the body of the loop under
certain conditions. Like the break statement C supports similar statement called continue
statement. The continue statement causes the loop to be continued with the next iteration
after skipping any statement in between. The continue with the next iteration the format
of the continue statement is simply:
Continue;
Consider the following program that finds the sum of five positive integers. If a negative
number is entered, the sum is not performed since the remaining part of the loop is
skipped using continue statement.
The for loop provides a more concise loop control structure. The general form of the for
loop is:
When the control enters for loop the variables used in for loop is initialized with the
starting value such as I=0,count=0. The value which was initialized is then checked with
the given test condition. The test condition is a relational expression, such as I < 5 that
checks whether the given condition is satisfied or not if the given condition is satisfied
the control enters the body of the loop or else it will exit the loop. The body of the loop is
entered only if the test condition is satisfied and after the completion of the execution of
the loop the control is transferred back to the increment part of the loop. The control
variable is incremented using an assignment statement such as I=I+1 or simply I++ and
the new value of the control variable is again tested to check whether it satisfies the loop
condition. If the value of the control variable satisfies then the body of the loop is again
executed. The process goes on till the control variable fails to satisfy the condition.
We can include multiple expressions in any of the fields of for loop provided that we
separate such expressions by commas. For example in the for statement that begins
Sets up two index variables I and j the former initialized to zero and the latter to 100
before the loop begins. Each time after the body of the loop is executed, the value of I
will be incremented by 1 while the value of j is decremented by 10.
Just as the need may arise to include more than one expression in a particular field of the
for statement, so too may the need arise to omit on or more fields from the for statement.
This can be done simply by omitting the desired filed, but by marking its place with a
semicolon. The init_expression field can simply be “left blank” in such a case as long as
the semicolon is still included:
For(;j!=100;++j)
The above statement might be used if j were already set to some initial value before the
loop was entered. A for loop that has its looping condition field omitted effectively sets
up an infinite loop, that is a loop that theoretically will be executed for ever.