0% found this document useful (0 votes)
38 views14 pages

CBCP2202 E-Tutorial 4 Repetition Control Structure

This document discusses repetition structures in C programming such as for, while, and do-while loops. It provides examples of using these loops to display numbers, input and calculate totals/averages of numbers, and control loop execution using continue and break statements. Nested loops are also covered. The key differences between for and while loops are explained.

Uploaded by

djaljd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views14 pages

CBCP2202 E-Tutorial 4 Repetition Control Structure

This document discusses repetition structures in C programming such as for, while, and do-while loops. It provides examples of using these loops to display numbers, input and calculate totals/averages of numbers, and control loop execution using continue and break statements. Nested loops are also covered. The key differences between for and while loops are explained.

Uploaded by

djaljd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CBCP 2202

Repetition Control
Structure
E-TUTORIAL 4
Learning Outcomes
1. Implement C programs using repetition structure such as for, while and do-
while statements;
2. Differentiate between for, while and do-while statements
3. Use continue and break statements to jump instruction
4. Write nested loops depending on the problem given.
For Statement
The for statement is to write loops in an easy format, but the number of loops are
to be known at the time of writing the program.

Syntax:
1. Intialisation
2. Expression
for (initialisation, expression, counter) 3. Statement
{ 4. Counter
5. Repeat 2 till false
statement;
}
For loop: Code 1
Question: Display numbers 1 to 10 using for loop:

#include <stdio.h>
int main()
{
int x;
for(x=1; x <= 10; x++) Output:
{ 1 2 3 4 5 6 7 8 9 10
printf("%d\t", x);
}
return 0;
}
For loop: Code 2
Question: Input 10 numbers, then output the total and average
#include <stdio.h> Output:
int main() Enter an integer value: 5
{ Enter an integer value: 3
int x, number, total=0; Enter an integer value: 2
float average; Enter an integer value: 8
for (x = 0; x < 10; x++) Enter an integer value: 9
{ Enter an integer value: 1
printf("Enter an integer value: "); Enter an integer value: 4
scanf("%d", &number); Enter an integer value: 7
total += number; Enter an integer value: 6
} Enter an integer value: 5
average = (float) total /10; Total : 50
printf("Total : %d\n”, total); Average : 5.00
printf(“Average : %.2f\n”, average);
return 0;
Relationship between for and while statements
If we compare the flow of the for statement execution with the while statement
execution, there is a similarity.

For loop can be changed into a while loop, and vice versa.
Controlling Loops
What we know is that non-zero represents true and zero represents false in C
language.

This allows us to control the repetition of the execution of the body in a loop
statement.
printf("Input a number larger than 0: ");
scanf("%d", &num);
x = num - 1;
total = 0;
while (x != 0)
{
total += x;
x --;
}
Endless loop
When the repetition statement is written, we need to see how the stopping
condition is written also.

We must ensure that the execution of the statement will eventually end.
printf(“Input a number: “);
scanf(“%d”, &num);
x = 0;
while (x != num)
x++;
printf(“Value of x is %d\n”, x);

Question: if num is entered as a negative number, what happens to the code above?
While loop controlled by condition
While loops in the examples before this are determined by the condition.

These loops will continue its repetition until a certain condition is met.
#include <stdio.h>
if (num % 2 == 0)
int main()
countEven++;
{
else
int count, x, num, countEven, countOdd;
countOdd++;
x= countEven = countOdd = 0;
x++;
printf(“Number of data: “);
}
scanf(“%d”, &count);
printf(“Number of even numbers: \t%d\n”, countEven);
printf(“Number of odd numbers: \t%d\n”, countOdd);
while (x < count)
return 0;
{
}
printf("Enter a positive integer: “);
scanf(“%d”, &num);
While loop controlled by sentry
A loop controlled by sentry is a loop that will repeat according to a value that is
called a sentry value.
Continue statement
Output:
Question: Displaying even numbers
0 even number
#include <stdio.h> Prints even numbers only!
int main() 2 even number
{ Prints even numbers only!
int x; 4 even number
for (x = 0; x <= 9; x++) Prints even numbers only!
if (x % 2 !=0) 6 even number
Continue; Prints even numbers only!
else 8 even number
printf("%d even number\n", x); Prints even numbers only!
printf(“Prints even numbers only!\n”);
return 0;
}
Break statement
Question: Totaling numbers from 1 till total is more than 100
#include <stdio.h>
int main()
{
int n, total = 0;
for (n = 1; n <= 25; n++)
{ Output:
total = total + n;
if (total >= 100) Total value = 105;
break; n value = 14
}
printf(“Total value = %d]\n”, total)
printf(“n value = %d\n”, n);
return 0;
}
Nested loops
Nested loops refer to the looping statement where the body has a looping
statement.

As an example, a for statement can have the body with a while loop.
for (i = 0; i < 2; i++) Output:
{
for (j = 0; j < 5; j++)
0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4
{
printf(“%d, %d\t”, i, j);
}
}
The End

Q&A
Thank you.

You might also like