24
Loop Control Structure

Loop control structure

Embed Size (px)

Citation preview

Page 1: Loop control structure

Loop Control Structure

Page 2: Loop control structure

Loops : Loops statements are used to repeat the execution of statements.

Page 3: Loop control structure

Types Of Loops :

Entry Control Loop

Exit Control Loop

Page 4: Loop control structure

Entry control Loop

For Loop

While Loop

Page 5: Loop control structure

For Loop

Page 6: Loop control structure

Syntax:

for( initializa

tion ; condition;

increment/decrement

operator)

Page 7: Loop control structure
Page 8: Loop control structure
Page 9: Loop control structure

Example of for Loop #include <stdio.h> int main(){

int n, count, sum=0; printf("Enter the value of n.\n"); scanf("%d",&n); for(count=1;count<=n; count++) //for loop terminates if count>n { sum+=count; /* this statement is equivalent to

sum=sum+count */ } printf("Sum=%d",sum); return 0;

}

Page 10: Loop control structure

Output:

Enter the value of n 3

Sum=3

Page 11: Loop control structure

While Loop

Page 12: Loop control structure

Syntax:

Initialization ;

While(condition)

{

body of loop

updation ;

}

Page 13: Loop control structure
Page 14: Loop control structure

/*C program to demonstrate the working of while loop*/ #include <stdio.h> int main(){ int number,factorial; printf("Enter a number.\n"); scanf("%d",&number); factorial=1; while (number>0)

{ /* while loop continues until test condition number>0 is true */

factorial=factorial * number; number--;

}

Page 15: Loop control structure

printf("Factorial=%d",factorial); return 0; }

O

utput

Enter a number. 5

Factorial=120

Page 16: Loop control structure

Do-While Loop

Page 17: Loop control structure

Synt

ax:

Initiali

zation ;

do{

Body

Of L

oop;

Updation;

} whil

e (co

ndition);

Page 18: Loop control structure

Flow Chart of Do-While Loop

Page 19: Loop control structure

#include <stdio.h>

main()

{ int i = 6;

Do{

printf("Hello %d\n", i );

i = i -1;

}while ( i > 0 );

}

Page 20: Loop control structure

Output

Hello 6 Hello 5

Hello 4

Hello 3

Hello 2

Hello 1

Page 21: Loop control structure
Page 22: Loop control structure
Page 23: Loop control structure

Any Probleum ????????????????????

Please ask………………………………

Page 24: Loop control structure