Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

Friday, December 8, 2017

Nested Loop In C Programming

One loop inside another loop is called Nested loop. Here we will discuss about this.

Syntax of a Nested for loop


for ( variable initialization ;  condition ;  variable update )
{
      for ( variable initialization ;  condition ;  variable update )
      {
          // body statement.
      }
}


Example of a Nested for loop. This program will print “Outer Loop” 3 times. After every time it will print “Inner Loop” 2 times.


#include <stdio.h>

int main() 
{
     for(int i = 1;  i <= 3;  i++)
     {
           printf( “%d. Outer Loop \n”, i );
           for(int j = 1;  j <= 2;  j++)
           {
                 printf ( " Inner Loop \n", j );
           }
           printf(“ \n ”);
     }
     return 0;
}

Output

1. Outer Loop
Inner Loop
Inner Loop

2. Outer Loop
Inner Loop
Inner Loop

3. Outer Loop
Inner Loop
Inner Loop

Note : We can use any loop inside any other loop. For an example we can use while loop inside a for loop or vice versa.



Previous Topic

Next Topic



Do While Loop In C Programming

We used loops to execute a block of code several times. do while loop is similar as while loop but in do while loop conditional expression appears at the bottom of the loop instead of the beginning.

Syntax of a While loop


do
{
     // body statement.
}
while ( Condition );

Note
  • Before checking the condition do while loop must be executed once.
  • Conditional expression / statement appears in the bottom of the loop. And there is a semicolon after the conditional expression.

Condition : After checking the condition it decides whether loop needs to continue or not. If the condition is false then the loop will not be executed.


Let’s look into the following program which will print “Hello world” 5 times.


#include <stdio.h>

int main() 
{
      int i = 1;
      do
      {
           printf("Hello World \n");
           i++;  // Update.
      }
      while ( i <= 5 );

      return 0;
}


Output
Hello World 
Hello World 
Hello World 
Hello World 
Hello World 

How it works

Step 1 : First it will go to the body statement, there is a print command and it will be executed.

Step 2 : After that, Update statement will be executed

Step 3 : Start repeating from Step 1.

Step 4 : Then it will check either the condition ( i <= 5 ) is true or not. If the condition is true only then it will start repeating (go to the body statement) from Step 1. Otherwise it will be terminated.



Previous Topic

Next Topic



While Loop In C Programming

We used loops to execute a block of code several times.

Syntax of a While loop


while ( Condition )
{
     // body statement.
}

Condition : After checking the condition it decides whether loop needs to continue or not. If the condition is false then the loop will not be executed.


Let’s look into the following program which will print “Hello world” 5 times.

#include <stdio.h>

int main() 
{
     int i = 1;
     while ( i <= 5 )
     {
         printf("Hello World \n");
         i++;  // Update.
     }
     return 0;
}

Output

Hello World 
Hello World 
Hello World 
Hello World 
Hello World 

How it works

Step 1 : First it will check either the condition ( i <= 5 ) is true or not. If the condition is true only then it will go to the body statement. Otherwise it will be terminated.

Step 2 : In the body statement, there is a print command and it will be executed.

Step 3 : After that, Update statement will be executed

Step 4 : Start repeating from Step 1.



Previous Topic

Next Topic



For Loop in C Programming

We used loops to execute a block of code several times.

Syntax of for loop
for ( variable initialization ;  condition ;  variable update )
{
      // body statement.
}

Variable Initialization : It allows us to assign value into a pre-declared variable or we can declare here at the same time, Variable initialization statement is executed once in the beginning of execution.

Condition : It decides whether loop needs to continue or not after checking the condition. If the condition is false then the loop will not be executed.

Variable update : We can update our variables here. Like increment, decrement etc.


Let’s look into the following program which will print “Hello world” 5 times.

#include <stdio.h>

int main() 
{
      for(int i = 1;  i <= 5;  i++)
      {
           printf("Hello World \n");
      }
      return 0;
}

Output
Hello World 
Hello World 
Hello World 
Hello World 
Hello World 

How it works

Step 1 : First we declare a variable name i and assign value of 1.

Step 2 : Then it will check either the condition ( i <= 5 ) is true or not. If the condition is true then it will go to the body statement. Otherwise it will be terminated.

Step 3 : In the body statement, there is only one print command and it will be executed.

Step 4 : After that, Update statement will be executed first time ( note that update statement will be executed form 2nd iteration and from 2nd iteration variable initialization will be ignored )

Step 5 : Start repeating from Step 2.


Look at program below


#include <stdio.h>

int main() 
{
      int i = 1;
      for( ;  i <= 5 ;  )
      {
           printf("Hello World \n");
           i++;
      }
      return 0;
}

Both programs are valid. In for loop we can either declare variable inside the loop or outside the loop as it will be executed only once. In this case we declare outside of for loop. Same as for updating variable. We increases variable inside the loop as we need to update the value every time.

Note : Semicolon separates each of these section. Any section can be empty but the semicolon must have to be there.



Previous Topic

Next Topic



Loops in C programming

Did you ever write a single word hundred or thousand times as a punishment in your school life ? I guess most of us are familiar with this type of punishment. If yes, then it is very easy to understand what is loop and what it does.

Sometimes you have to repeat some particular block of code several times. Simply you can write those code several times. Surely this is a solution but not the efficient one. Using loop we can execute any block of code several times.

Look at the following example, where I want to print “Hello World” 5 times. You don’t have to be worry about the program/syntax (loop part) right now, we will discuss it on this section. Just see how simply we can repeat any block of code.


#include <stdio.h>

int main() 
{
      printf(“Hello World \n”);
      printf(“Hello World \n”);
      printf(“Hello World \n”);
      printf(“Hello World \n”);
      printf(“Hello World \n”);

      return 0;
}


Using while loop

#include <stdio.h>

int main() 
{
      int cnt = 1;
      while(cnt <= 5)
      {
           printf(“Hello World \n”);
           cnt++;
      }
      return 0;
}


Which one you will prefer when you have to write it millions of time ?

We can follow any of those process as both are valid but loops save our code, effort and time also. Repeating 5 times is not a big deal we can type this 5 times but when we have to do the same thing thousand or million time then loops are the best option.

Note
  • It saves code and time.
  • Makes program simple.

This section we will talk about

  • for loop
  • while loop
  • do while loop
  • Nested loop
  • Infinite loop


Previous Topic

Next Topic