0% found this document useful (0 votes)
11 views10 pages

1.8 Flow Control - Loops

The document discusses repetition in programming, focusing on loop control structures such as for, while, and do-while loops in C. It explains the purpose of loops, the concept of iterations, and the importance of loop guards and initialization. Additionally, it provides examples of each loop type and common programming exercises related to loops.

Uploaded by

carllongs1000
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)
11 views10 pages

1.8 Flow Control - Loops

The document discusses repetition in programming, focusing on loop control structures such as for, while, and do-while loops in C. It explains the purpose of loops, the concept of iterations, and the importance of loop guards and initialization. Additionally, it provides examples of each loop type and common programming exercises related to loops.

Uploaded by

carllongs1000
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/ 10

2/22/2024

Repetition in Programs
 Three types of program control structure:

 sequence, selection, repetition


Flow Control - Loops  Loop: a control structure that repeats a group of steps in a program

 C loop control statements are:

 while, for, and do-while

 loop body: the statements that are repeated in the loop

2 Loops 2/22/2024

Repetition in Programs… Repetition in Programs…


 The purpose of a loop is to repeat the same action a number of times  Two main forms of repetition:

 We refer to each time an action is repeated as an iteration. 1) Fixed Count Loops: Repetition over a finite set (for loops).
 We continue repeating the action until a terminating condition is satisfied. 2) Variable Count Loops: Repetition as long as a given condition
 This terminating condition is called a loop guard holds (while and do-while loops).
 The loop guard is represented by a boolean expression in the same way as  Loops can be further classified as being either pre-test (while loops)

the condition of an IF statement. or post-test (do-while or repeat-until loops).


 We could add recursion to the above (routine calls itself).
 Before the loop starts we initialize variables involved in the loop
 Although not used so much in imperative languages recursion is the
 In C there are a number of ways to represent loops principal program construct used in logic and functional languages.

3 Loops 2/22/2024 4 Loops 2/22/2024

1
2/22/2024

Flow Diagram of Loop Choice Process Loop Kinds

6 Loops 2/22/2024

The for loop Example for loop


 three loop control components for (i = 0; i < 10; i = i ++)
 initialization of the loop control variable printf("i is %d\n", i);
 test of the loop repetition condition  i=0 is the initialization of the loop counter

 change (update) of the loop control variable  i < 10 is the loop guard i.e. repeat until i is greater or equal to 10

 /* Display nonnegative numbers < max */  i = i +1; this is the counter increment ; it is more commonly written i

for (i = 0; i < max; i ++) ++


printf("%d\n", i);  printf("i is %d\n", i); is the loop statement

7 Loops 2/22/2024 8 Loops 2/22/2024

2
2/22/2024

The increment operator ++ While Loop


 The increment operator ++ takes a single variable as its operand.  The most basic loop in C is the while loop.

 for (counter = 0; counter < limit; ++counter)  The general syntax of a while loop is

 prefix increment while( expression )


 ++ is placed immediately in front of its operand statement
 value of the expression is the variable’s value after incrementing  The expression represents the loop guard.

 postfix increment  While it is true repeat the statement.

 ++ comes immediately after the operand  Terminate when the expression evaluates to false
 expression’s value is the value of the variable before it is incremented

9 Loops 2/22/2024 10 Loops 2/22/2024

Example While Loop While Loops


#include <stdio.h>
 Will execute only if condition is true.
main()
{  Condition is tested before execution
int x
x = 2;  Do while loops will execute once before testing the condition.
while(x < 1000)
{
printf("%d\n", x);
x = x * 2;
}
}
This program repeatedly prints out powers of two until we generate a number greater
than or equal to 1000
The terminating condition is thus when the power of two equals or exceeds 1000

11 Loops 2/22/2024 12 Loops 2/22/2024

3
2/22/2024

Do While Loop Example Do While Loop


do #include <stdio.h>
{} main()
while ( condition ); {
int x;
 Notice that the condition is tested at the end of the block instead of the x = 0;
beginning, so the block will be executed at least once. If the condition is true, we do
{
jump back to the beginning of the block and execute it again
/* "Hello, world!" is printed at least one time even though the
 A do..while loop is almost the same as a while loop except that the loop body is condition is false */
printf( "Hello, world!\n" );
guaranteed to execute at least once }
 A while loop says "Loop while the condition is true, and execute this block of while ( x != 0 );
getchar();
code", a do..while loop says "Execute this block of code, and then continue to
}
loop while the condition is true".

13 Loops 2/22/2024 14 Loops 2/22/2024

Example Do While Loop… Sample loop questions


 Keep in mind that you must include a trailing semi-colon after the while  1: Write a program to calculate the integer square root of a number e.g. 7

in the above example. integer square is 2 etc

 A common error is to forget that a do..while loop must be terminated

with a semicolon (the other loops should not be terminated with a  2:Write a program which continually reads in integers until 45 is read in
semicolon, adding to the confusion).  3: Use a Do while loop to calculate the minimum of 100 integers being
 Notice that this loop will execute once, because it automatically executes read in.
before checking the condition.

15 Loops 2/22/2024

4
2/22/2024

Read until 45 Integer Square Root


#include <stdio.h> #Include <stdio.h>
main()
main() { int i,a;
{int x; printf(“enter number \n”);
scanf(“%d”,&a);
x =1; If (a < 0)
while(x != 45) {printf(“No Square Root available”)}
else
{ printf(“enter no”); { i= 0;
scanf(“%d", &x); while (i*i < a)
} {i++};
printf(“The integer square root of %d is %d”,a,i);
} }
}

Read until 45 Minimum


#include <stdio.h>
#include <stdio.h> main()
main() {int x,min,counter;
counter = 0;
{int x; do
do { printf(“enter no”);
scanf(“%d", &x);
{ printf(“enter no”); if (counter= = 0)
scanf(“%d", &x); {min = x;}
else
} {If (x < min) min = x; }
while(x != 45) ; counter++;}
While (counter < 100) ;
} printf(“minimum is %d”,min);
}

5
2/22/2024

Minimum with while loop


#include <stdio.h>
main()
More questions
{int x,min,counter;
counter = 1;  4: Read in 20 integers and
printf(“enter no”);
scanf(“%d", &x); count the even numbers
N 100
Xn
min = x;


While (counter < 100)  5: Write a program to
{ printf(“enter no”);
scanf(“%d", &x); calculate
( X 2n )
If (x < min) min = x;

N 1
counter++;}
printf(“minimum is %d”,min);
}

More Questions… No of even Integers


#Include <stdio.h>
 Write a program to calculate
main()
e = 1/1! + 1/2! + 1/3! Etc { int i, a,sum;
sum = 0;
Do it for 6 terms
for (i=0;i < 20; i++)
{ printf(“enter number \n”);
scanf(“%d”,&a);
If ((a%2)==0)sum = sum + +;
};
printf(“total even nos is %d”,sum);
}

6
2/22/2024

Sum of function Factorial


#Include <stdio.h> #Include <stdio.h>
#Include <math.h> #Include <math.h>
main() main()
{ int n, x;
float enum,denom,sum; { int fac, n, x;
sum = 0.0; float enum,denom,sum;
printf(“enter number \n”); sum = 0.0;
scanf(“%d”,&X); for (n=1;n <= 6; n++)
If (x == 0) printf(“sum not defined”) {
else fac = 1;
{
for (n=1;n <= 100; n++) for (x =1; x <= n; x++){
{ fac = fac*x;
enum = pow(x,n); }
denom = pow(x,2*n); enum = 1;
sum = sum + (enum/denom); denom = 1.0/fac;
}; sum = sum + (enum/denom);
printf(“total %f ”,sum);
} };
} printf(“total %f ”,sum);
}

Sum of function Example Remember


#Include <stdio.h>
#Include <math.h>  Loops Consist of :
main()
{ int n,i, fac;  1: Initialization
float enum,denom,sum;
sum = 0.0;
for (n=1;n <= 100; n++)  2: Terminating Condition (Guard)
{
fac = 1;  3: Loop Body
for (i=1, i <=n, i++)
{ fac = fac*i;  4: Terminating Action
}
enum = n-1;
denom = fac*pow(n,2);
If (denom == 0) printf(“sum not defined”)
sum = sum + (enum/denom);  For Counted Loops Iterations use a For loop
};
printf(“total %f ”,sum);
}  Otherwise use a while or a do while loop

7
2/22/2024

The increment operator ++ Exercises


 The increment operator ++ takes a single variable as its operand.

 for (counter = 0; counter < limit; ++counter)

 prefix increment  Write C code to print the information contained in each of the slides that

 ++ is placed immediately in front of its operand follows:

 value of the expression is the variable’s value after incrementing

 postfix increment

 ++ comes immediately after the operand

 expression’s value is the value of the variable before it is incremented

29 Loops 2/22/2024 30 Loops 2/22/2024

31 Loops 2/22/2024 32 Loops 2/22/2024

8
2/22/2024

33 Loops 2/22/2024 34 Loops 2/22/2024

35 Loops 2/22/2024 36 Loops 2/22/2024

9
2/22/2024

END

37 Loops 2/22/2024

10

You might also like