Download presentation
Presentation is loading. Please wait.
Published byHartono Darmadi Modified over 5 years ago
1
Loops Prof.Gaikwad Varsha P. Information Technology Dept.
Government College of Engg. Aurangabad
2
Introduction Definition: A group of statements is executed repeatedly, until some conditions has been satisfied. Types of loops While loop Do while loop For loop
3
While loop The while loop has a loop condition which controls the execution of the loop statement. Syntax while(loop condition) { statement1; statement2; : statement n; }
4
Conti… If the loop condition evaluates to be true , then the statements (statemet1 to statement n} will be executed. If the loop condition evaluates to be false, control will go to the first statement after the curly bracket enclosing the loop statements. The loop condition is at the beginning of the loop. It can happen that the loop condition is false at the beginning itself. While loop is suitable for iterations that place zero or more times.
5
Example: Write a program to print 10 consecutive numbers
#include<stdio.h> void main() { int i=0; while(i<10) printf(“%d”,i); ++I; } Output:
6
do… while loop The do while loop will be executed whether or not the condition in the loop is true at least for one time Syntax do { statement1; statement2; : Statement n; }while(loop condition);
7
Conti… The loop condition is evaluated after the statements,{statement1,statement2…statement n} are executed once. If the loop condition evaluates to be true, then the statements {statement1,statement2…statement n} will be executed again. If the loop condition evaluates to be false, control will go to the first statement after the while statement.
8
Example: Write a program to print first 10 consecutive numbers
#include<stdio.h> void main() { int i=0; do printf(“%d\t”,i); ++I; }while(i<10); } Output:
9
for loop A group of statements is repeated up to a designed number of times . Syntax for(initialization;test_condition;increment/ decrement operation) { statement1; statement2; : statement n; }
10
Example: Write a program to print first 10 consecutive numbers
#include<stdio.h> void main() { int I ; for(i=0; i<10; i++) printf(“%d\t”,i) } Output:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.