Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

ITERATIVE STATEMENTS

-Iterative statements (loops) allow set of instructions to be executed or performed several times until certain conditions are met. -It can be predefined as in the for loop, or openended as in while and do-while.

The FOR statement


Also called as for loop Is considered as a predefined loop because the number of times it iterates to perform its body is predetermined in the loops definition. The for loop contains a counter whose values determine the number of times the loop iterates.

General form of the for statement


for (initialization; condition;increment) { statement_sequence; }

Note:
Never place a semi-colon right after the for header. This is a logical error. Never change the value of the for loops counter inside the body of the loop. This affect the result of the program. The increment part of the for loop is execute after the first iteration of the loop.

Write a program that will print the numbers 1 to 10 using a for statement
#include <stdio.h> int x; main() { for (x=1; x<=10; x++) printf(%d\n,x); getch(); }

Output
1 2 3 4 5 6 7 8 9 10

Write a program that will get the sum of all integers from 1 to 10.
#include<stdio.h> int x, sum; main() { sum = 0; for (x=1; x<=10; x++) sum = sum + x; printf(The sum of 1 to 10 is %d\n, sum); getch(); }

Output
The sum of 1 to 10 is 55

The WHILE statement


Also called as while loop -Is an open-ended or event-controlled loop Iterates while condition is TRUE (1). When it becomes FALSE (0), the program control passes to the line after the loop code.

The general form of the while statement is:


while(condition) { statement_sequence; }

Write a program that will print the numbers 1 to 10 using while statement.
#include<stdio.h> int x; main() { x=1; while(x<=10) { printf(%d\n,x); x++; } getch(); }

Output
1 2 3 4 5 6 7 8 9 10

The DO-WHILE statement


- the second type of open-ended or eventcontrolled loop - Also called as do-while loop

The general form of the do-while statement is:


do { statement_sequence; }while (condition);

Write a program that will get the average of all integers from 1 to 10 using do-while loop
#include<stdio.h> int x, sum; float average; main() { sum = 0; x=1; do { sum=sum + x; x++; }while (x<=10) average = sum/10.00; printf(The computed average is %.2f\n, average); getch(); }

Output
The computed average is 5.50

Example
Design a program that calculates the sum of the input given number of n using three looping statements.

Design a program that calculates the sum of the input given number of n using three looping statements.
#include<stdio.h> main(){ int m,e,sum; clrscr(); printf(Enter your desire looping number:); scanf(%d,&m); sum=0; for(e=1;e<=m;e++){ printf(\n%d,e); sum=sum+e;} printf(\n The sum:%d,sum); getche(); }

DO WHILE LOOP
#include<stdio.h> main(){ int m,e,sum; clrscr; printf(Enter your desire looping number:); scanf(%d,&m); e=1; sum=0; do{ printf(\n%d,e); sum=sum+e; e++; }while(e<=m); printf(\n The sum:%d,sum); getche(); }

while
#include<stdio.h> main(){ int m,e,sum; clrscr; printf(Enter your desire looping number:); scanf(%d,&m); e=1; sum=0; while(e<=m){ printf(\n%d,e); sum=sum+e; e++; printf(\n The sum:%d,sum); getche(); }

Design a program that displays the Fibonacci sequence numbers of n which is display number by a user. Apply three solutions using the three looping statements.

FOR LOOP statements


#include<stdio.h> main() { long int m,e,l; int o,t; clrscr(); printf(Enter a number:); scanf(%d,&t); e=1; l=1; m=1; for(o=1;o<=t;o++){ printf(%d,m); If(o>=2){ m=e+s; s=e; e=m; } delay(55100); } getche(); }

DO WHILE LOOP statements


#include<stdio.h> main() { long int m,e,l; int o,t; clrscr(); printf(Enter a number:); scanf(%d,&t); e=1; l=1; m=1; do{ If(o>=2){ m=e+s; s=e; e=m; } o++ delay(55100); }while(o<=t); getche(); }

WHILE LOOP statements


#include<stdio.h> main() { long int m,e,l; int o,t; clrscr(); printf(Enter a number:); scanf(%d,&t); e=1; l=1; m=1; while(o<=t){ print(%d,m); If(o>=2){ m=e+l; l=e; e=m; } o++ delay(55100);} getche(); }

TRY
Design a program that outputs the given whole number using three looping statements. Hint: use decrement.

FOR loop statement


#include<stdio.h> main(){ int m; clrscr(); printf(Example number two looping statements:); for(m=8;m>=1;m--){ printf(\n%d,m); delay(8000);} getche();}

DO WHILE LOOP
#include<stdio.h> main(){ int m; clrscr(); printf(Example number two looping statements:); m=8; do{ printf(\n%d,m); m--; delay(5000); }while(m>=1); getche();}

WHILE loop
#include<stdio.h> main(){ int m; clrscr(); printf(Example number two looping statements:); m=8; while(m>=1){ printf(\n%d,m); delay(5000); m--;} getche(); }

TRY AGAIN
Design a program that reverses the input number of n. Formulate an equation to come up with the answer.

#include<stdio.h> main(){ int m,e; clrscr(); print(\t\t\tAn example of reversed number\n\n); printf(\nEnter a value:); scanf(%d,&m); for(;m!=0;){ e=m%10; printf(%d,e); m=m/10; } delay(50000); getch(); }

You might also like