Loop statements | do-while | while | for | #Tut-05

 In this tutorial we are going to cover the types of loops..

What is loop..?

In the program so many times we come across the situation where we want to execute some part of the program again and again for some particular number of times. In such situations we make use of loop statements.

When we want to repeat some part of procedure for some particular number of times then we make use of loops..

In this language there are three types loops

1. do-while

2. while

3. for


Basic syntax for the working of any loop..?


working of loops..



While Loop..

- while statement is one of the frequently used loop statement in programming. The      while statement tells the compiler to repeat the series of statements as long  as the    certain condition is true.  As soon as the certain condition becomes false computer stops repeating and goes to the next statement.

syntax :

   
while(condition)
    {
        //body
    }

- in this loop condition is given at the top of loop

- In while loop first the condition is checked, if the condition is true then and then only our control goes inside the while loop and then the pointer is incremented by one and again condition is get checked. So in this way till our condition is true loop will keep runing..


Program :

#include<stdio.h>

int main()
{
    int i = 0;

    while (i < 5)
    {
        printf("i = %d\n",i);
        i++;
    } 
    return 0;
}

Output :

i = 0
i = 1
i = 2
i = 3
i = 4




Do - While loop..

Do - while loop is executed at least once, it is said because here loop statements are executed first and then the condition is checked. Now if we run our loop with a false condition then whatever statements present in the loop are executed first and then the condition is checked.

So the condition is given at the bottom of loop.

Syntax : 

do
{
    /* code */

} while (/* condition */);




Code Snippet : 


#include<stdio.h>

// printing even numbers between 1 to 10 using do-while loop
int main(){
    int i = 1;

    do
    {
        if (i%2 == 0)
        {
            printf("i = %d\n", i);
        }
        i++;
    } while (i <=10); //condition is checked at bottom
   
    return 0;
}

 

output : 

i = 2
i = 4
i = 6
i = 8
i = 10


Now let us see the basic difference between while loop and do-while loop..!




For Loop..

For loop..

The for loop consist of three actions 

- initialization 

- condition 

- increment/decrement

Each action should be separated by the semicolon(;).

Why use for loop..?

  • The for loop is used to iterate the statements or a part of program several times.
  • It is used to traverse the data structures like an array, linked list etc.
  • It has little different syntax than while and do-while loop which help to reduce the line of codes in out program.
Syntax : 

//  for(expression1; expression2; expressino3)
    for (int i = 0; i < size; i++)
    {
            /* code */
    }



Properties :




Code Snippet : 

#include<stdio.h>

// let us print the multiplication table of any number using for loop
int main(){
   
    int num = 3;
    printf("multiplication table of %d is :\n", num);
    for (int i = 1; i <= 10; i++)
    {
        printf("%d X %d = %d\n", num, i, num*i);
    }
   
    return 0;
}


Output : 


multiplication table of 3 is :
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30



There are few more conditional statements in C language
1. break;
2. continue;
3. goto;

Break Statement..

  • The break statement is used when we want to break the cycle of execution after satisfying the certain condition.
  • This statement breaks the further operation of  loop and stops the process of execution and comes out of the loop.
  • If break statement is inside a nested loop then it breaks the inner loop in which it is contained. The break statement is used with while, do-while and for loop.
Code Snippet : 

#include<stdio.h>
// using break statement..

// do the same program by using the while loop and do-while loop

int main(){

    for (int i = 0; i <= 5; i++)
    {
        // as soon as the condition becomes true loop will break
        if (i == 2)
        {
            break;
        }
        printf("i = %d\n", i);
    }
    printf("out of the for loop");
    return 0;
}


Output:

i = 0
i = 1
out of the for loop


Continue Statement..
  • The continue statement is used when execution of further statements in the loop is to be skipped.
  • This statement bypass the execution of next statement and transfers control to the start of the loop for further execution of loop.
  • This statement should be used only within a loop.

Code Snippet :

#include<stdio.h>

// perform continue with while and do-while loop

int main(){

    for (int i = 0; i <= 5; i++)
    {
        if (i == 3 || i == 2)
        {
            continue; // whatever condition we give it will skip that part of program
        }
        printf("i = %d\n", i);
    }
   
    return 0;
}

Output:

i = 0
i = 1
i = 4
i = 5


Goto and Lables..

The goto statement is also called as a jump statement because here also flow our program jumps from one statement to another statement.




Code Snippet:

#include <stdio.h>
// using goto and lables
int main()
{
    /*
    1. Un-conditional goto;
       here we have not given any condition so,
       it will keep printing numbers

    int i = 1;
    top : printf("i = %d\n",i);
    i++;
    goto top;
    */

    // conditional goto;
    // after satisfying the condition loop stop
    int i = 1;

    top : printf("i = %d\n", i);

    i++;

    if (i < 8)
    {
        goto top;
    }
    return 0;
}


Ouput :

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7


Comments

Popular posts from this blog

Introduction to C language | #Tut-01