Loops ....... Iterations
Consider a situation, when a block of code needs to be executed multiple
number of times. Programming languages provide various control structures that
allow for more complicated execution paths.
Loop Type & Description
Firstly loop is of two types:
(a) Entry Controlled Loop: This means the condition is checked before
entering into the loop. E.g: For Loop,
While Loop
(b) Exit Controlled Loop: This means the condition is checked after
entering into the loop. This also means Exit control loop executed at least
once. E.g: Do…While Loop. Generally for
showing menu in C program this loop is used, so that the at least once the menu
should be shown.
1. “while” loop (Entry controlled)
Repeats a statement or group of statements while a given condition
is true. It tests the condition before executing the loop body.
2. “for” loop (Entry
Controlled)
Executes a sequence of statements multiple times and abbreviates
the code that manages the loop variable.
3. “do…while” loop (Exit
Controlled)
It is more like a while statement, except that it tests the
condition at the end of the loop body.
The
general form of a loop statement in most of the programming languages is this −
The general form of a loop statement in most of the programming languages is this −
When you are programming with loops, it helps to
understand that there are only a few basic formats that are used repeatedly. If
you can master these variations and recognize when they are needed, then
programming will become much easier.
All the loops have a basic structure. There will
be a variable (at least one) that will control how many times the loop runs. It
is necessary to do the following to this variable in each loop that you write.
- Initialize the loop control variable. Initialization
should occur before the loop starts.
- Test the loop control variable.
- Update the loop control variable.Updating
is done somewhere inside the loop, usually as the last statement in the
loop.
If you do not do all three
of these steps in every loop that you write, then you will be writing incorrect
loops that will either never loop, or will loop forever. If you wrongly created
a loop that runs forever, press CTRL-C to stop your program.
Count Loops
To use a count loop is when
the program can determine ahead of time; how many times the loop will be repeated.
This is not possible all the time. So the illustration is
1. The loop always repeats the same number of times. (Deterministic)
2. The program calculates the number of repetitions based upon user
input. (Also Deterministic)
When designing a loop, the first question to ask
is, "How many times will the loop repeat?" Here are examples of count
loops.
a=
5; /* ‘a’ can be user input*/
for(i=1;i<=a;i++)
{
fact=fact*i;
}
Sentinel Loops : Often, it will be necessary
to write loops that repeat until a certain value is entered. These are known as
sentinel loops. The sentinel can be a single value or it can be an entire range
of values. Sentinel loops are more useful than count loops, since they allow
for an undetermined amount of repetitions. There are two common places for
using a sentinel loop
1. Reading an unknown amount of input from the user. This is flexible
way to read data from the user. The program will be able to accept any amount
of input from the user.
2. Validating input. If it is necessary to verify that a user's input
falls within a certain range, then a sentinel loop is required. It is now known
ahead of time how many times the user will enter an invalid number.
Sentinel loops still have three steps for the loop control
variable: initialize, test and update. However, with sentinel loops, the
initialize and update statements look the same. You should remember that one
statement is being used to initialize the loop control variable and that the
other is being used to update it.
Here is the example of sentinel loop
while((ch=getc(stream))!=EOF)
ch=getc(stream);
Notice that the sentinel loops can be used to
test for good values, or to test for bad values. It is important to remember
the abstractions: initialize, test and update. Initialize before the loop, test
in the while statement parenthesis, update inside the loop (usually at the end
of loop body).
Conditional Loops : Conditional loops have common traits with
sentinel and count loops. They are like sentinel loops in that it is unknown
before time how many times they will repeat. They are like count loops in that
they terminate as the result of a calculation, instead of based upon user
input.
As you can see from these
loops, conditional loops combine qualities of both the sentinel and the count
loops. The exit from the loop depends on some initial input from the user and a
calculation based upon that input.
Special sentinel loop
There is a special sentinel
loop that can be used with file handling. There is a special character that
terminates all text files: CTRL-Z. Whenever encounters this character when
reading a file, it terminates all input from that file. The type statement in
DOS.There is a problem with using the CTRL-Z character: once an invalid input
is encountered, no more valid input can be received.
Dig deeper:
Loop control statements change execution from its normal sequence.
When execution leaves a scope, all automatic objects that were created in that
scope are destroyed.
C supports the following control statements.
The Infinite Loop
A loop becomes an infinite loop if a condition never becomes
false. The for loop is traditionally used for this purpose.
Since none of the three expressions that form the 'for' loop are required, you
can make an endless loop by leaving the conditional expression empty.
#include <stdio.h>
int main()
{
for(;;){
printf(“This statement will run forever”);
}
return 0;
}
When the conditional expression is absent, it is assumed to be
true. You may have an initialization and increment expression, but C
programmers more commonly use the for(;;) construct to signify an infinite
loop.
So valid ‘for’ statements are
1. for(;;) (treated as do….while structure)
2. for(i=0;;)
3. for(;i<10;) (Act like While loop statement)
4. for(;;i++)
Remember one thing is loop control variable also follow the ‘C’
variable rules. They all are cyclic value variable. For more info read my
previous blogs.
So on this note
int i;
for (i=32767;i>40000;i++) is an infinite loop.
Because 32767+1 = -32768 and so on….
In “while” one can use
while(‘true’) /*Where ‘true’ means any Non-Zero value*/
generally you can use while(1);
In the next blog I will give some examples of programs
If you like the blog please Like, Comment & Share

No comments:
Post a Comment