Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

C Programming Part 8

VARDHAMAN COLLEGE OF ENGINEERING - C programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Programming Part 8

VARDHAMAN COLLEGE OF ENGINEERING - C programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C language provides three iterative/repetitive loops.

1 : while loop

2 : do-while loop

3 : for loop

While Loop: Syntax :

variable initialization ;

while (condition)

statements ;

variable increment or decrement ;

while loop can be addressed as an entry control loop. It is completed in 3 steps.

 Variable initialization.( e.g int x=0; )

 condition( e.g while( x<=10) )

 Variable increment or decrement ( x++ or x-- or x=x+2 )

The while loop is an entry controlled loop statement, i.e means the condition is evaluated
first and it is true, then the body of the loop is executed. After executing the body of the loop,
the condition is once again evaluated and if it is true, the body is executed once again, the
process of repeated execution of the loop continues until the condition finally becomes false and
the control is transferred out of the loop.

Example : Program to print first 10 natural numbers


#include<stdio.h>

#include<conio.h>

void main( )

C PROGRAMMING Page 71
int x;

x=1;

while(x<=10)

printf("%d\t", x);

x++;

getch();

Output

1 2 3 4 5 6 7 8 9 10

C Program to reverse number

#include<stdio.h>

#include<conio.h>

main()

int n, reverse=0, rem;

clrscr();

printf("Enter a number: ");

scanf("%d", &n);

C PROGRAMMING Page 72
while(n!=0)

rem=n%10;

reverse=reverse*10+rem;

n/=10;

printf("Reversed Number: %d",reverse);

getch();

Flowchart

C PROGRAMMING Page 73
do-while loop
Syntax : variable initialization ;

do{

statements ;

variable increment or decrement ;

}while (condition);

The do-while loop is an exit controlled loop statement The body of the loop are executed first
and then the condition is evaluated. If it is true, then the body of the loop is executed once again.
The process of execution of body of the loop is continued until the condition finally becomes
false and the control is transferred to the statement immediately after the loop. The statements
are always executed at least once.

Flowchart

C PROGRAMMING Page 74
Example : Program to print first ten multiple of 5

#include<stdio.h>

#include<conio.h>

void main()

int a,i;

a=5;

i=1;

do

printf("%d\t",a*i);

i++;

}while(i <= 10);

getch();

C PROGRAMMING Page 75
Output

5 10 15 20 25 30 35 40 45 50

Example

main()

int i=0

do

printf("while vs do-while\n");

}while(i= =1);

printf("Out of loop");

Output:

while vs do-while

Out of loop

For Loop:
 This is an entry controlled looping statement.

 In this loop structure, more than one variable can be initialized.

 One of the most important features of this loop is that the three actions can be taken at a
time like variable initialization, condition checking and increment/decrement.

 The for loop can be more concise and flexible than that of while and do-while loops.

Syntax : for(initialization; condition; increment/decrement)

Statements;

C PROGRAMMING Page 76
Example:

#include<stdio.h>

#include<conio.h>

void main( )

int x;

for(x=1; x<=10; x++)

printf("%d\t",x);

getch();

Output

1 2 3 4 5 6 7 8 9 10

Various forms of FOR LOOP

I am using variable num in all the below examples –

1) Here instead of num++, I‟m using num=num+1 which is nothing but same as num++.

for (num=10; num<20; num=num+1)

2) Initialization part can be skipped from loop as shown below, the counter variable is declared
before the loop itself.

int num=10;

for (;num<20;num++)

Must Note: Although we can skip init part but semicolon (;) before condition is must, without
which you will get compilation error.

C PROGRAMMING Page 77
3) Like initialization, you can also skip the increment part as we did below. In this case
semicolon (;) is must, after condition logic. The increment part is being done in for loop body
itself.

for (num=10; num<20; )

//Code

num++;

4) Below case is also possible, increment in body and init during declaration of counter variable.

int num=10;

for (;num<20;)

//Statements

num++;

5) Counter can be decremented also, In the below example the variable gets decremented each
time the loop runs until the condition num>10 becomes false.

for(num=20; num>10; num--)

Program to calculate the sum of first n natural numbers

#include <stdio.h>

int main()

int num, count, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &num);

// for loop terminates when n is less than count

C PROGRAMMING Page 78
for(count = 1; count <= num; ++count)

sum += count;

printf("Sum = %d", sum);

return 0;

Output

Enter a positive integer: 10

Sum = 55

Factorial Program using loop

#include<stdio.h>

#include<conio.h>

void main(){

int i,fact=1,number;

clrscr();

printf("Enter a number: ");

scanf("%d",&number);

for(i=1;i<=number;i++){

fact=fact*i;

printf("Factorial of %d is: %d",number,fact);

getch();

C PROGRAMMING Page 79
Output:

Enter a number: 5

Factorial of 5 is: 120

Flow Chart of for Loop :

C PROGRAMMING Page 80

You might also like