Loops are formed to “Repeat ” one or more continuous desired, number of times.
Loops in C and C++
C offers following types of loops:
1. for loop
2. do while loop
3. while loop
for loop:
Syntax:
for(initialization of variables(s); condition; incrementation/decermentation of variable(s)) { statements(s); }
- when control enters for the first argument gets invoked for the first time only. Generally the initial value of the variable is given there. Ex: a=1;
- if more than one variable are given here then it must be separated by comma(,).
- After that control goes to second argument, where a condition is given.
- if the condition is true control enters inside the loop and execute the statement given there.
- After executing them control goes back for.
- While going back, it automatically, internally invokes the third argument.
- In the third argument generally the values of variable is incremented or decremented.
- This way the loop works till the condition remain true.
- As soon as the condition becomes false the control comes to the next line of the scope of for.
EX:1
Program:
#include<stdio.h> #include<conio.h> main() { int a,b; clrscr(); for(a=1; a<=5; a=a+1) { for(b=1; b<=5; b=b+1) { printf("*"); } printf("\n"); } getch(); }
Output:
*****
*****
*****
*****
*****
Remark:
The outer loop is the number of lines and the inner loop is controlling number of * in each line.
EX:2
For output:
*
**
***
****
*****
In EX:1 program write:
for(b=1; b<=a; b=b+1)
EX:3
For output:
*****
****
***
**
*
#include<stdio.h> #include<conio.h> main() { int a,b; clrscr(); for(a=5; a>=1; a=a-1) { for(b=1; b<=a; b=b+1) { printf("*"); } printf("\n"); } getch(); }
EX:4
Output:
12345
1234
123
12
1
In EX: 3 program write:
In the place printf(“*”); write
printf(" %d", b);
While loop:
Syntax:
while (condition) { statements(s); }
- One or more condition may be given with while.
- If the condition is true, control enters inside the loop and executes the statement given there.
- After executing them, control goes back to while and checks the condition again.
- This way the loops works till the condition remains true.
- As soon as the condition becomes false control comes to the next line of the scope of while.
- The scope of while is define using
{ }
is optional , if a single statement is given with while otherwise it’s become compulsory.
- While applying any type of loops only those lines should be given within the loop which needs to be repeated.
Program:
#include<stdio.h> #include<conio.h> main() { int c=1; clrscr(); while (c<=3) { printf("ADMEC \n"); c=c+1; } getch(); }
Output:
ADMEC
ADMEC
ADMEC
Do-while
Syntax:
do { statement(s); } while (condition);
- The basic difference between while and do while is that , do while works at least once.
- While is a precheck loops, whereas do while is a post check loop.
- When while is used no semicolon (;) is give after while , but in do while it is compulsory to give semicolon (;) after while.
Program:
#include<stdio.h> #include<conio.h> main() { int a=1; clrscr(); do { printf("%d \n", a); a = a + 1; }while (a<=3); getch(); }
output:
1
2
3
Loops are very important topic in any language and it is very typical to understand the concept of while, do-while and for. We try to understand some important point of loop statement in C and C++ in above topic. We also discuss control statement in C and C++ in our next blog….