0% found this document useful (0 votes)
30 views21 pages

07 Iterative Constructs

This document introduces iterative constructs (loops) in C programming, including while, do-while, and for loops. It provides examples of each loop type and explains how to use break and continue statements to alter loop flow. Nested loops are also introduced, where one loop can contain another loop. The key learning outcomes are listed as being able to correctly use terms like dowhile loop, while loop, for loop, and understand counter-controlled vs. sentinel-controlled loops.

Uploaded by

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

07 Iterative Constructs

This document introduces iterative constructs (loops) in C programming, including while, do-while, and for loops. It provides examples of each loop type and explains how to use break and continue statements to alter loop flow. Nested loops are also introduced, where one loop can contain another loop. The key learning outcomes are listed as being able to correctly use terms like dowhile loop, while loop, for loop, and understand counter-controlled vs. sentinel-controlled loops.

Uploaded by

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

Introduction to C Programming

Iterative Constructs
Topic & Structure of Lesson

• Iterative Structures
 while statement
 do-while statement
 for statement

• The break and continue Statements


• Nested Loops

CT018-3-1 Introduction To C Programming Iterative Construct Slide 2 (of 28)


Learning Outcomes

If you have mastered this topic, you should be able


to use the following terms correctly in your
assignments :

Dowhile loop
While loop
For loop
counter-controlled loop
sentinel-controlled
Continue

CT018-3-1 Introduction To C Programming Iterative Construct


Iterative Constructs

• An iterative construct or loop is a group of


instructions which is repeatedly executed while
some condition remains true.

• An infinite loop loops forever and must be


avoided.

• Three loops constructs: while, for and do-while

• Two kinds of control within a loop: counter-


controlled loop and sentinel-controlled loop.
CT018-3-1 Introduction To C Programming Iterative Construct Slide 4 (of 28)
while statement

•The while statement is a pre-test condition


controlled loop construct.

• It has the form,


while (expression) condition
T
action
{ statements }
F

CT018-3-1 Introduction To C Programming Iterative Construct Slide 5 (of 28)


Example-1

count_star = 0;
while (count_star < N){
printf(”*”);
count_star++; }
  (or)
count_star = 0;
while (count_star++ < N)
printf(”*”);

CT018-3-1 Introduction To C Programming Iterative Construct Slide 7 (of 28)


Example-2

int num = 1; /*declaration and*/


int total = 0; /*initialization*/
  while (num <= 100)
{
total += num;
num++;
}

CT018-3-1 Introduction To C Programming Iterative Construct Slide 8 (of 28)


Counter controlled vs.
Sentinel controlled

Use Counter-control loops when the


number of iterations are known.

Sentinel control loops are more general.

CT018-3-1 Introduction To C Programming Iterative Construct Slide 9 (of 28)


Counter controlled loop
Example:
#define N 10
. . .
total = 0;
count = 1;
while (count <= N) {
printf (”Enter score: ”);
scanf (”%d”, &score);
total += score;}
count++;
}
avg = (float) total / N;
printf(”Average is %.2f\n”,avg);

CT018-3-1 Introduction To C Programming Iterative Construct Slide 10 (of 28)


Sentinel controlled loop
#define SENTINEL -1
. . .
total = 0;
count = 0;
printf(”Enter score, -1 to end: ”);
scanf (”%d”, &score);
while (score != SENTINEL) {
total += score; count++;
printf(”Enter score, -1 to end: ”);
scanf (”%d”, &score);
}
if (count) {
avg = (float) total/count;
printf(”Average is %.2f\n”, avg);
}else
printf(”No scores were entered\n”);

CT018-3-1 Introduction To C Programming Iterative Construct Slide 12 (of 28)


do-while statement
• The do-while statement is a post-test condition
control loop structure.
• The loop body is executed at least once.
• The general form of the do-while is,
do{ action
statements
T
} while(loop condition); condition

CT018-3-1 Introduction To C Programming Iterative Construct Slide 14 (of 28)


do-while Examples
•Example 1 :-
count = 1;
do {
printf (”%d ”, count);
} while (++count <= 10);
•Example 2:-
do {
printf(“Enter a letter A thru E”);
scanf(“%c”,&letter);}
while(letter< ‘A’ || letter >’E’);

CT018-3-1 Introduction To C Programming Iterative Construct Slide 15 (of 28)


for statement

• The for statement is another pre-test condition-


control loop structure.
• It provides a more compact form for counter-
controlled loops.
• The general form is,
for (initialization expression;
loop condition; update expression)
{ statements; }

CT018-3-1 Introduction To C Programming Iterative Construct Slide 17 (of 28)


for statement

Initial value

condition T statements Increment


F

Example:
for (count_star = 0; /* init */
count_star < N; /* condition */
count_star++) /* update */
printf (”*”);

CT018-3-1 Introduction To C Programming Iterative Construct Slide 18 (of 28)


break and continue statements

• The break and continue statements are used to


alter the flow of a control.

• The break statement in a while, do-while or for


structure causes immediate exit from the
structure.

• The continue statement in a while, do-while and


for structure skips the remaining statements in the
body, to the next iteration of the loop.
CT018-3-1 Introduction To C Programming Iterative Construct Slide 20 (of 28)
Break - Example

/*Using the break in the for stt.*/


main()
{ int x;
OUTPUT:
for(x=1;x<=10;x++){ 1234
if (x == 5) Broke out of loop at 5
break;
printf(%d”,x);}
printf(“\nBroke out of loop at x=
%d”,x);
}

CT018-3-1 Introduction To C Programming Iterative Construct Slide 21 (of 28)


Continue - Example

/*Using continue in a for structure*/


main()
{ int x; OUTPUT:
1 2 3 4 6 7 8 9 10
for(x=1;x<=10;x++){ Used continue to skip printing
if ( x == 5) the value 5
continue;
printf(“%d”,x);
}
printf(“\n Used continue to”);
printf(“skip printing the value 5”);
}
CT018-3-1 Introduction To C Programming Iterative Construct Slide 23 (of 28)
Nested Loops

• A loop body may contain statements of any kind


including other loops.
• A loop that contains another loop is called a
nested loop.

CT018-3-1 Introduction To C Programming Iterative Construct Slide 24 (of 28)


Example
#include<stdio.h>
main(){
int side, temp, asterisk;//asterisk generally refers to
the symbol *
printf("Enter the square side: ");
scanf("%d", &side);
temp = side;
while (side-- > 0) {//controls the number of lines
printed
asterisk = temp;
while (asterisk-- > 0){//controls the number of
character printed on a line
printf("*"); OUTPUT:
} Enter the square side: 3
***
putchar('\n'); }}
***
***
CT018-3-1 Introduction To C Programming Iterative Construct Slide 25 (of 28)
Summary
• An iterative construct specifies that an action is to be
repeated while some condition remains true.
• The iterative statements are while, do-while and for.
• Counter-controlled looping uses a variable as a
counter to determine when a loop should terminate.
• Sentinel values are generally used to control repetition
when the precise number of repetitions is not known
in advance and the loop includes statements that
obtain data each time the loop is performed.

CT018-3-1 Introduction To C Programming Iterative Construct Slide 26 (of 28)


Summary

• The break statement, when executed in one


of the iterative structures (while, do-while, for)
causes immediate exit from the structure.
• The continue statement, skips any remaining
statements in the body of the structure, and
proceeds with the next iteration of the loop.
• Loops that contain other loops are called
nested loops.

CT018-3-1 Introduction To C Programming Iterative Construct Slide 28 (of 28)

You might also like