0% found this document useful (0 votes)
53 views

Looping Construct in C Language

The document discusses different looping constructs in C language including while, for, and do-while loops. It provides examples of using each loop type to iterate through numbers from 1 to 5 or from 10 down to 1. It also discusses the typical components of a loop including initialization, condition checking, loop body, and changing state. Counters and accumulators are introduced as variables used to keep track of counts or accumulated values within loops. Exercises are provided to practice different loop types and using counters and accumulators.

Uploaded by

basti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Looping Construct in C Language

The document discusses different looping constructs in C language including while, for, and do-while loops. It provides examples of using each loop type to iterate through numbers from 1 to 5 or from 10 down to 1. It also discusses the typical components of a loop including initialization, condition checking, loop body, and changing state. Counters and accumulators are introduced as variables used to keep track of counts or accumulated values within loops. Exercises are provided to practice different loop types and using counters and accumulators.

Uploaded by

basti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Looping Construct in

C Language
What is a Loop?

• Consider first this problem.


• Write a program that will output the numbers from 1 to 5. The
output should look like:
1
2
3
4
5
#include <stdio.h>

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.

• In C language, there are three repetitive statements to be used:


1. while loop
2. for loop
3. do-while loop
While Loop

Syntax:
while (condition or expression)
statement(s);

• The <statement> is going to be executed while the <expression>


results into a condition that evaluates to 1 (true). If it evaluates
to 0 (false) then the loop processing is terminated and the
statement after the while loop is executed next.
Example – While Loop
#include <stdio.h>

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 syntax of a for loop is as follows:


for ([initialization]; [condition]; [change of state])
<statement>

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 (i = 1; i <= 5; i++)


printf(“%d\n”, i);
}
Exercise
Write a program that will ask the user to input three integers. Call these
three integers as start, end and step, respectively. Assume that start is
always less than end and that step is a positive integer greater than zero.
Thereafter, the program should print the numbers such that the first item is
start, the second item is equivalent to the first item plus the value of step,
the third item is equivalent to second item plus the value of step and so on.
The last value to be printed should not be greater than end. Use a for loop.

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?

A counter is a variable that is used to keep track of the count (as


the name suggest) of a certain group of items. Usually,
• its data type is int
• it is initialized to a value of 0
• incremented by 1 inside a loop
#include <stdio.h>
main()
{
int i, n;
int ctr_positive; /* this is the counter */
ctr_positive = 0; /* initialization */

for (i = 1; i <= 10; i++)


{
printf(“input integer number %d: ”, i);
scanf(“%d”, &n);

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?

An accumulator is a variable that is used to keep track of the


accumulated value of a certain group of items.
An accumulator:
may have a data type of int, float or double
it is usually initialized to a value of 0
changes by assuming the sum of the current value of the
accumulator and the value of another variable
#include <stdio.h>
main()
{
int I, n;
int sum; /* this is the accumulator */
sum = 0; /* initialization */
for (i = 0; i < 10; i++)
{
printf(“Input integer number %d: “, i);
scanf(“%d”, &n);
sum = sum + n; /* accumulate sum of all input n */
}
printf(“The sum of the integers is = %d\n”, sum);
}
Exercise - Accumulator

• Modify the previous program such that it will compute and


output the sum of positive numbers and the sum of negative
numbers.

You might also like