loop control structure

Post on 16-Jan-2017

181 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Loop Control Structure

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

Types Of Loops :

Entry Control Loop

Exit Control Loop

Entry control Loop

For Loop

While Loop

For Loop

Syntax:

for( initializa

tion ; condition;

increment/decrement

operator)

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;

}

Output:

Enter the value of n 3

Sum=3

While Loop

Syntax:

Initialization ;

While(condition)

{

body of loop

updation ;

}

/*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--;

}

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

O

utput

Enter a number. 5

Factorial=120

Do-While Loop

Synt

ax:

Initiali

zation ;

do{

Body

Of L

oop;

Updation;

} whil

e (co

ndition);

Flow Chart of Do-While Loop

#include <stdio.h>

main()

{ int i = 6;

Do{

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

i = i -1;

}while ( i > 0 );

}

Output

Hello 6 Hello 5

Hello 4

Hello 3

Hello 2

Hello 1

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

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

top related