Looping Construct in C Language
Looping Construct in C Language
C Language
What is a Loop?
main()
{
printf(“1”);
printf(“2”);
printf(“3”);
printf(“4”);
printf(“5”);
}
#include <stdio.h>
main()
{
int i;
i = 1;
printf(“%d\n”, i);
i = i + 1;
printf(“%d\n”, i);
i = i + 1;
printf(“%d\n”, i);
i = i + 1;
printf(“%d\n”, i);
i = i + 1;
printf(“%d\n”, i);
}
One possible algorithm for the problem above is:
initialize i to 1
repeat the following while i is less than or equivalent to 5
print the value of i
increment i by 1
• A loop usually has the following components:
1. Initialization of the variable or of several variables
2. Conditions - that would evaluate to either true or false; the condition
check is usually made on the current value of the variable initialized (1)
above
3. Body – which maybe a single statement or a group of statements.
4. A Change of State which is usually a statement inside the body of the
loop that changes the contents of the variable(s)
Repetition or Looping
• The last control structure in C Language.
• Looping is repetition of action/s or statement or series of
statements.
Syntax:
while (condition or expression)
statement(s);
main()
{
int i;
i = 1; //initialization
while (i <=5) //conditional check
{
printf(“i = %d\n”, i); //body (these 2 lines)
i++; //change of state
}
Exercise – While Loop
• Consider the C program on the previous slide. What will happen if we
remove the initialization i = 1? This means that the value of i is not
defined (garbage).
• Consider again the original program. What will happen if we remove the
curly brackets enclosing the body of the loop?
• Consider again the original program. What will happen if the
programmer committed a typographical error, such that instead of
pressing the less than symbol, the greater than symbol was pressed, i.e.,
the condition becomes i >= 5?
• Consider again the original program. What will happen if the
programmer forgot changing the value of i, i.e., what happens to the
program when there is no i++ ?
Exercise – While Loop
Write a C program that will print all the odd numbers from 1 to
100.
Write a C program that will generate the numbers from 10 down
to 1, i.e., 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Example - Write a C program that will print all the odd numbers from 1
to 100.
#include <stdio.h>
void main(void)
{
int i;
i = 1; /* initialization */
while (i <= 100) /* conditional check */
{
printf(“i = %d\n”, i); /* body (these 2 lines */
i = i + 2; /* change of state */
}
}
Variables are not limited to integers. Consider the following problem:
Write a C program that will print the values 0.0, 0.2, 0.4, 0.6, 0.8, 1.0.
#include <stdio.h>
main()
{
float f;
f = 0.0;
while (f <= 1.0)
{
printf(“%.1f\n”, f);
f = f + 0.2;
}
}
For loop
The for loop is actually a “more compact” form of the while loop.
The loop is executed as follows:
perform the initialization
check the condition
if it is 1 (true) execute the <statement>, i.e., the body of loop,
then change the state, and check for the condition again
if it is false, exit from the loop.
Example – For Loop
#include <stdio.h>
main()
{
int i;
For example, if start = 5, end = 10 and step = 2, then the output should be:
5
7
9
Do-while Loop
The syntax for a do while loop is as follows:
do
<statement>
while (<expression>)
The statement will execute first before evaluating the condition/s.
Statements will be repeated until the condition/s becomes TRUE.
• At least once, the statement/s will perform.
To print the numbers from 1 to 100 using a do while loop:
#include <stdio.h>
main()
{
int i;
i = 1; /* initialization */
do /* body of the loop */
{
printf(“%d\n”, i);
i++; /* change of state */
} while (i <= 100); /* condition */
}
Print the numbers 100 down to 0.
#include <stdio.h>
main()
{
int i;
i = 100 // initialization
do // body of the loop
{
printf(“%d\n”, i);
i--; // change of state
} while (i >= 0); //condition
}
What is a Counter?
if (n >= 0)
ctr_positive = ctr_positive + 1; /* change counter */
}
printf(“the number of positive integers = %d\n”, ctr_positive);
}
Exercise – Counter
• Modify the previous program such that it will also count and
print the number of negative integers that were input by the
user.
What is an Accumulator?