Vijay Krishnavanshi
Open Source Developer at KDE
“Sometimes it is the people no one can imagine anything of who do the things no one can imagine.” - Alan Turing
Let Us Revisit
Lets Program
Write a program to input a number n and print Hello World n times.
#include<stdio.h>
void main(){
int n;
printf("Enter the number of times");
scanf("%d",&n);
if(n>0)
printf("Hello World");
n=n-1;
if(n>0)
printf("Hello World");
n=n-1;
if(n>0)
printf("Hello World");
n=n-1;
.
.
.
.
//till when n becomes equal to zero.
}
Things are being repeated
How many times: n times
Statements repeating :
While Loop
Syntax:
Initialization
while(condition){
statement 1;
statement 2;
.....
}
So our Code for the problem
#include<stdio.h>
void main(){
int n;
scanf("%d",&n);
while(n>0){
printf("Hello World!");
n=n-1;
}
}
The Other Way
#include<stdio.h>
void main(){
int n,t;
scanf("%d",&n);
t=0;
while(t<n){
printf("Hello World");
t=t+1;
}
}
Do While
Syntax:
do
{
statements;
statements;
}while( condition );
do this
{
statements;
statements;
}if( condition )
By Vijay Krishnavanshi