Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

PROGRAMMING GUM



This is a page for the very beginners of C++ Programming Language.Here you can find :-


If You Have Any Suggestion, Query, Or Thoughts To Share With Me, You Can Easily Contact Me.My Contact Details Are Here.


Contact Me 





 

Evaluation Of for Loops

Evaluation Of for Loops

The loop we just saw (here) works according to following steps :-

These steps are explained below : 
  1. Area I, in program its initialize i=1.
  2. Area II, in program its check whether i<=5 is true or not. If its true then enter the loop body. If its false then execute the statement immediately after body of loop.
  3. We execute the body of loop just like a normal program. Line by line,Step by step.Just the easy way.
  4. Area III, in program its an increment statement. So increment value of i by 1.
  5. Jump to step (ii) and keep repeating the process until condition gets false.
  6. When Condition gets false, we step out of loop body and the step immediately after the body is executed. In our program, "We are out of loop" gets printed on screen.

Remember area1, area2 and area3 shown above doesn't necessarily need to have initialize  condition check, increment/decrements statement in them.They may have blank space or multiple statement in them.For putting multiple statement we just separate them by commas.Lets check an example.



<<for loop                                                    Home                                               An Odd Loop>>

for Loop

for Loop


This is the most widely used loop among C++ programmers.The general form of this is :
for(initialize variable ; check condition ; increment / decrements value)
{
    Body of Loop
}

Its very easy to take down this topic.All you need is just a bit of practice.Lets begin with an example and then we'll be having a decent explanation of it.

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    system("CLS");
    for(int i=1;i<=5;i++)
    {
        cout<<"C++ is an awesome language."<<endl;
    }
    cout<<"We are out of loop."<<endl;
    system("PAUSE");
}

Here's the output of our program.




















Now we'll see how to evaluate the a 'for' loop and get the output.

<<Loops In C++                                       Home                                         Evaluation Of Loop>>

Loops In C++


Loops In C++


    By now we've just seen programs that are capable of making decisions based n conditions provided to them.But a dark part of these programs is each statement of these programs is executed only once.But in real life we come along many circumstances when we need to execute the program or a piece of it some number of times. According to what we have learnt till now, this task can be completed by writing the code number of times or by running the program again and again. Either way we choose, we only end up completing the task with a lot of efforts.
    So, do we have any choice? Is there any way which can comfort us from both, typing efforts and wastage of precious time? Answer is YES, we do have a choice and this choice is to create a 'Loop'.

What Exactly Creating Loop Is ?

    Loop is a technique that helps us to execute part of our code repeatedly. There are two options we can choose to create our loop with. First is to define a fix number of times our loop should be executed, say 5 times for example. Another option is to execute the loop until a desired condition is satisfied.For example executing a part of code until value of a variable becomes 20.

Types Of Loops

There are three types of loops :
  • for loop
  • while loop
  • do-while loop.
Lets take them down one be one.

<<Multiple Statements Under if-else                      Home                                          for Loop>>