Header Ads Widget

Ticker

6/recent/ticker-posts

loops (Iteration ) in C (Part 1)

 what is loops (Iteration):-

                                          Iteration is nothing but repetition   of one or more statement in c language .looping construct are used to repeat one or more statement again and again until some condition is met .C language provides following three types of loops.

  1. While
  2. do-while
  3. for
' while ' and ' do-while '  are the Non deterministic loops or indeterminate loops  and the ' for ' loop is called Deterministic loop.

Entry Controlled loops:-
                                        In this loop condition is check before entering the loop.
ex:- while and for loop.

Exit controlled loops:- 
                                    In exit control loop the loop condition is check at the exit of loop.
ex:- do-while.

1) For() :-
               -------------------------
               -------------------------
               for( initialization , condition ,increment/ decrement )
               {
                       first statement
                       -----------------
                       -----------------
                       last statement
                }
                next statement;
                ------------------
                ------------------
       Example:-   first initialization is executed . after initialization condition is checked if it is true then the statement written in the body of loop will be executed . after execution of all the statement increment / decrement will be executed. then again condition will be checked.  if it is true then flow of control goes inside  the loop otherwise the statements after the loops will be executed.

example:-
#include<stdio.h>
int main()
{
int i;
for(i=0;i<5;i++)
printf("Hello world!\n");
return 0;
}
Output:-
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!


2) while()

              ----------------------
              ----------------------
              Initialization;
              while(condition)
              {
                     first statement;
                     ------------------
                     ------------------
                    increment / decrement;
               }
               next statement
               ------------------
               ------------------

      Explanation:- first we initialize the variable then after check the condition of the loop if condition is true then statement written in the body of the loop will be executed . and after execution of the statement increment / decrement will be executed then again condition will be checked if it is true then flow of control goes inside of the loop and if condition is false then the statements after the while loop will be executed.

example:-
#include<stdio.h>
int main()
{
int i;
//initialization
i=0;
while(i<5) // condition
{
printf("Hello world!\n");
i++; // increment/decrement
}
return 0;
}
Output:-
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!


3) do-while()
        
                     ------------------
                    -------------------
                    initialization;
                    do
                     {
                           first statement;
                           -----------------
                           -----------------
                           increment/ decrement
                     }while(condition);
                     next statement;
                     -------------------
                    --------------------

       Explanation:-  first we initialize the variable and then start the execution of  the statements within the loop and after execution of all the statement increment / decrement will be executed and this condition written in the while loop will checked if it is true then again flow of control goes inside of the loop and all the process will be repeated. and if it is false then the statement next to the loop will be executed.

example:-
#include<stdio.h>
int main()
{
int i;
//initialization
i=0;
do
{
printf("Hello world!\n");
i++; // increment/decrement
}while(i<5);// condition
return 0;
}
Output:-
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!



  

Recommended Post:

Hackerearth Problems:-

Hackerrank Problems:-
Data structure:-

Key points:-

 MCQs:-

Post a Comment

0 Comments