c basic6
c basic6
A loop consists of two parts, a body of a loop and a control statement. The
control statement is a combination of some conditions that direct the body of the
loop to execute until the specified condition becomes false. The purpose of the
loop is to repeat the same code a number of times.
Types of Loops in C
Depending upon the position of a control statement in a program, looping in C is
classified into two types:
While Loop in C
A while loop is the most straightforward looping structure. Syntax of while loop in
C programming language is as follows:
while (condition)
{
statements;
}
Example :
#include<stdio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d ",num);
num++; //incrementing operation
}
return 0;
}
Output :
1 2 3 4 5 6 7 8 9 10
Description
do
{
statements
}
while (expression);
As we saw in a while loop, the body is executed if and only if the condition is true.
In some cases, we have to execute a body of the loop at least once even if the
condition is false. This type of operation can be achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at least once. After the
body is executed, then it checks the condition. If the condition is true, then it will
again execute the body of a loop otherwise control is transferred out of the loop.
Similar to the while loop, once the control goes out of the loop the statements
which are immediately after the loop is executed.
The critical difference between the while and do-while loop is that in while loop
the while is written at the beginning. In do-while loop, the while condition is
written at the end and terminates with a semi-colon (;)
Example:
#include<stdio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d ",2*num);
num++; //incrementing operation
}
while(num<=10);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Description :
For loop in C
A for loop is a more efficient loop structure in 'C' programming. The general
structure of for loop syntax in C is as follows:
Example:
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Description :
Break
The break statement is used inside loop or switch statement. When compiler finds
the break statement inside a loop, compiler will abort/terminate the loop and
continue to execute statements followed by loop.
Example:
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
for(int i=0;i<=x;i++)
{
if(i==3)
break;
printf("%d ",i);
}
}
Output : 6
012
Continue Statement
The continue statement is also used inside loop. When compiler finds the
continue statement inside a loop, compiler will not skip all the following
statements in the loop and resume the loop.
Example:
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
for(int i=0;i<=x;i++)
{
if(i==3)
continue;
printf("%d ",i);
}
}
Output: 6
012456
GOTO Statements
The goto statement is a jump statement which jumps from one point to another
point within a function.
return 0;
}
Output: Statement 1. Output : 15
Statement 2.
Statement 3.
End of Program.