Loop in any Programming language

Programming Loop (For dummies)

What is Loop in programming world ?

Its a way to repeat things in your program code in whatever the language( c ,c++,.. etc) it is written.
To save yourself from re-writing the same code again and again.

There are different types of loops and given below are some examples:

  1. for 
  2. while
  3. do while
  4. foreach   

but all of them serve the same purpose . 

Same..!     

Then question is what's that single purpose ??? and why we have different types if they are doing the same.         Hmm interesting right don't worry you will get it .


Single purpose :-   Repetition of  code for you. To save you from writing it again and again

 Example suppose you have to write a code in whatever language(c,c++,java ,..etc) 
and have to display  a word   "hello"  200  times or 2000 times  on screen. 

Options you have :-

You can do this by writing the displaying command of that language 200 times in code,
For C++ displaying output on screen is done with  ' cout ' command.
    cout<<"hello";                    // <-- write this line 200 times 

Now if you want to display 200 times the ' hello ' word. You have to write 200 times the same
 statement(  cout<<"hello";    ) 

But no worries loop solves the problem for you.. :)

 Single statement of loop will do this for you and saved you form writing  200 times the same statement of your code.

C++ example of for loop:
main()
{

int i=1;              

for(i=1:i<=200;i++)                   //<-- this is  for loop statement   
{         

  cout<<"hello";                

            }

getch();

}

Explaination:

For-loop(is type of  loop ) have 3  things in it

  • i=1                   // starting value of loop statement
  • i<=200             // condition for loop 
  • i++                  // last thing after loop completes its iteration


 First i=1 which is starting value of For-loop statement 

Then i<=200 is a condition i made according to my current need that if value of i (variable i ) is 
less then equal (< = )  to 200 . Repeat whatever is written in those { } brackets  in our case it is just

  cout<<"hello";  

then after running whatever is in side these { }   Loop control goes to last thing in loop statement 
which is i++ 

what i++ is doing is increasing value of variable i with 1 after each iteration of loop 

to make condition i<=200 false to stop loop when it completes it 200 iterations .


[after checking condition i<=200 flow goes inside these {   } after complete execution of statements inside these {  } it comeback at last thing in loop which is i++ in our case ]




Why different types ?  is because while writing programs you don't always have same scenario or you are not just repeating hello word in real time  there are different ways to do the same thing to provide you more comfort and flexibility in writing code

                                                                         
                                                  That's  it  End of concept loop  :) 

                                      do tell me if you want quick video explanation of this 



Loop in any Programming language Loop in any Programming language Reviewed by ULoopDevelopers on 04:09 Rating: 5

2 comments:

  1. Wow! what an easy way to explain, i never learned that much easily anything!

    ReplyDelete

Powered by Blogger.