4 Topic Four - Looping

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Topic Four: Looping

In this chapter we shall learn how to build repetition


into our programs. Moreover, we shall learn the basic
theory and design principals behind looping algorithms
using pseudo code and flowcharting techniques.
This chapter specifically covers the following topics:
• Pseudo code for looping structures
• Flowcharts for looping structures
• Operators continued
• The while loop
• The do while loop
• The for loop
• break and continue statements
• System calls
An example of pseudo code

while end-of-file == false


process employee payroll
loop
FLOWCHARTS FOR LOOPING STRUCTURES

Using the previous pseudo code

Here if the condition is true, employee payroll is processed and


program control moves back to the beginning of the original
condition. Only if the condition is false does the program flow
terminate
THE WHILE LOOP
The while loop structure is used to create
iteration (loops) in your programs, as
demonstrated in the following program:
#include <stdio.h>
main(){
int x = 0;
while ( x < 10 ) {
printf("The value of x is %d\n", x);
x++;
} //end while loop
} //end main function
The while loop uses a condition (in this case x <
10) that evaluates to either true or false. As long
as the condition is true, the contents of the loop
are executed. Speaking of the loop’s
contents, the braces must be used to denote the
beginning and end of a loop with multiple
statements.
The following program code demonstrates the while
loop’s usefulness in building menus.
#include <stdio.h>
main(){
int iSelection = 0;
while ( iSelection != 4 ) {
printf("1\tDeposit funds\n");
printf("2\tWithdraw funds\n");
printf("3\tPrint Balance\n");
printf("4\tQuit\n");
printf("Enter your selection (1-4): ");
scanf("%d", &iSelection);
} //end while loop
printf("\nThank you\n");
} //end main function
Now, you may write a program using while loop for finding the sum
of a series:
1+2+3+4+……………..+n. This program will be like:
//Program for finding sum of series: 1+2+3+4+……………..+n
#include <stdio.h>
int main(){
int i=1;
int n,sum=0;
printf(“Give the Number of n:”);
scanf(“%d”,&n);
while ( i<= n){
sum = sum+i;
i=i+1;}
printf(“The sum of the series is: %d”,sum);
return 0;}
If you run this program and give 7 as value of n then it will print:
The sum of the series is:28
THE DO WHILE LOOP
The do while loop’s condition is at the bottom of
the loop rather than at the top.
do {
printf("The value of x is %d\n", x);
x++;
} while ( x < 10 ); //end do while loop

In the do while loop’s last statement, the ending


brace comes before the while statement, and
the while statement must end with a semicolon.
Using the do while loop allows one to execute the statements
inside of my loop at least once, even though the loop’s condition
will be false when evaluated.

#include <stdio.h>
main(){
int x = 10;
do {
printf("This printf statement is executed at least once\n");
x++;
} while ( x < 10 ); //end do while loop
while ( x < 10 ) {
printf("This printf statement is never executed\n");
x++;
} //end while loop
} //end main function
Now let us see a program which uses do while loop for
printing numbers 1 to 10.
#include <stdio.h>
int main()
{
int i=1;
do
{
printf(“Number is: %d\n”,i);
i=i+1;
}
while( i<=10);
return 0;
}
THE FOR LOOP
for loop is commonly used in building loops
when the number of iterations is already known.
#include <stdio.h>
main()
{
int x;
for ( x = 10; x > 5; x-- )
printf("The value of x is %d\n", x);
} //end main function
A single for loop statement contains three separate
expressions, as described in the following bulleted list.
• Variable initialization
• Conditional expression
• Increment/decrement
In the example, the first expression, variable
initialization, initializes the variable to 1.It was not
initialized in the variable declaration statement because
it would have been a duplicated and wasted effort. The
next expression is a condition (x > 5) that is used to
determine when the for loop should stop iterating. The
last expression in the for loop (x--) decrements the
variable x by 1.
Now let us see a program which uses for loop for
printing numbers 1 to 10.
#include <stdio.h>
int main()
{
int i;
for( i=1; i<= 10; i++)
{
printf(“Number: %d\n”,i);
}
return 0;
}
You may write a program using for loop for finding the sum
of a series:
1+2+3+4+……………..+n. This program will be like:
//Program for finding sum of series: 1+2+3+4+……………..+n
#include <stdio.h>
main(){
int i,n,sum=0;
printf(“Give the Number of n:”);
scanf(“%d”,&n);
for( i=1; i<= n; i++){
sum = sum+i;}
printf(“The sum of the series is: %d”,sum);
return 0;}
Write a program that will display even numbers
between 10 and 50
#include<stdio.h>
main(){
int a;
for(a=12;a<50;a+=2){
printf("%d\n",a);}
return 0;}
Convert the following while loop to an equivalent for
loop:
#include <stdio.h>
int main() {
int x=1;
int y;
while (x <=10) {
y=x*x;
printf("%d %d \n",x,y);
x+=3; }
return 0; }
BREAK AND CONTINUE STATEMENTS
The break and continue statements are used to manipulate program
flow in structures such as loops.
When a break statement is executed in a loop, the loop is terminated
and program control returns to the next statement following the end
of the loop.
#include <stdio.h>
main(){
int x;
for ( x = 10; x > 5; x-- ) {
if ( x == 7 )
break;
} //end for loop
printf(“\n%d\n”, x);
return 0;}
In this program, the condition (x == 7) becomes true after the third
iteration. Next, the break statement is executed and program control is
sent out from the for loop and continues with the printf statement.
continue statement
The continue statement is also used to manipulate program
flow in a loop structure. When executed, though, any
remaining statements in the loop are passed over and the
next iteration of the loop is sought.
The next program block demonstrates the continue
statement.
#include <stdio.h>
main(){
int x;
for ( x = 10; x > 5; x-- ) {
if ( x == 7 )
continue;
printf("\n%d\n", x);
} //end for loop
return 0;}
when executed number 7 won’t appear in the
output because when the condition x == 7 is
true, the continue statement is executed, thus
skipping the printf() function and continuing
program flow with the next iteration of the for
loop.

You might also like