Digital Learning

Friday, March 6, 2020

Loops in C(for,while & do-while):

C language has three loop: 
1. for 
2. while 
3. do-while. 

The first two are pre-test loop and do-while is a post-test loop. In the post-test circle, the code is constantly executed once. 

1. For Loop: 
Permit a lot of directions to be more than once executed until a specific condition came to. This condition might be foreordained or open finished. 

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

Program: For generation of multiplication tables.
#include<stdio.h> 
#include<conio.h> 
void main() 
int row,column; 
printf("Multiplication Table"); 
for(row=1;row<=10;row++) 
for(column=1;column<=10;column++) 
printf("m",row*column); 
putchar("\n"); 
getch(); 
}} 

2. While
While circle check the test condition at the highest point of the circle, which implies that the body of the circle won't execute if the condition is bogus in the first place. This component may dispense with the need to play out a different restrictive test before the circle. 

Syntax
while(condition) 
expression; 

3. Do while: 
Not at all like for and keeping in mind that loop, which test the circle condition at the highest point of the circle, the do-while circle checks its condition at the base of the circle. This implies a do-while circle consistently executes atleast once. The general type of the do-while circle is given beneath: 

Syntax

do 
 expression

while(condition); 

In spite of the fact that the wavy supports are a bit much when just a single articulation is available, they are typically used to keep away from disarray with the while. The do-while circle repeats until condition turns out to be bogus.

Program: C program to print 1 to 10 numbers using for loop.

#include<stdio.h>

#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("\n %d", i);
}
getch();
}
Output: 
1
2
3
4
5
6
7
8
9
10

Program: C program to print 20 to 30 numbers using for loop.


#include<stdio.h>

#include<conio.h>
void main()
{
int i;
clrscr();
for(i=20;i<=30;i++)
{
printf("\n %d", i);
}
getch();
}
Output:
20
21
22
23
24
25
26
27
28
29
30

Program: C program to print even numbers among 1 to 50 using for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=50;a++)
{
if(a%2==0)
printf("\t %d",a);
}
getch();
}

Output:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

Program: C program to print your name as many your age.

#include<stdio.h>
#include<conio.h>
void main()
{
int i, age;
char name[10];
clrscr();
printf("\n enter name and age");
scanf("%s%d", &name, &age);
for(i=1;i<=age;i++)
{
printf("\n %s", name);
}
getch();
}

Output: enter name and age

Mona 5
Mona
Mona
Mona
Mona
Mona

Program: C program to print a number divided by 3 among 1 to 20.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=20;i++)
{
if(i%3==0)
printf("%d",i);
}
getch();
}

Output:

3
6
9
12
15
18 

No comments:

Post a Comment