Loop Control Structure

-- Leave a Comment
Loops will help you to do some tasks that need to be do a statement repeating until the time you set or till some conditions.
There are three method in c for where you control loop in c.
Loops are the most for C programming Be prepared for all loops. and work hard on it.

The while loop

The while loop repeats a statement until the test at the top proves false. the while has the form:
while (expression)
{
Statements;
}
For example
int x=3;
while (x>0)
{
printf("x=%d\n",x);
x--;
}
output:
x=3
x=2
x=1

Because the while loop can accept expressions, not conditions.

The do while loop

This is vary similar to the while loop excepted that the test occurs at the end of the loop body. This guarantees that the loop is executed at leas once before continuing. Such a step is frequently used where data is to be read. the rest then verifies the data, and loops back to read again if it was unaccepted The do while loop has the form:
do
{
statement;
}
while(expression);
example
int x=3;
do
{
printf("x=%d\n",x--);
}
while(x>0);
output
x=3
x=2
x=1

The for loop

The for loop works where the number of iteration of the loop is known before the loop variable is entered. The head of loop consists of three parts separated by semicolons.
  • The  first is rune before the loop is entered. This is usually the initialization of the loop variable.
  • The second is a test, the loop is excited when this returns false.
  • The third is a statement to be run every time the loop body is complicated.This is usually an increment for the loop counter.
The C for statement has the following form:
for(expression1; expression2; expression3)
{
statement;
}
expressioninitialises; expressionit the terminate test; expressionis the modifier;
Simple example:
int x;
main()
{
for(x=3;x>0;x--)
{
printf("x=%d\n",x);
}
Output:
x=3
x=2
x=1

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University. I know something about Linux, Android, Java, Nodejs, Unity3D and 3 dots :)

0 comments:

Post a Comment