Showing posts with label for. Show all posts
Showing posts with label for. 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 





 

An Odd Loop

An Odd Loop

As I mentioned about the loop a little twist with its parameters, here's an example :



#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    system("CLS");
    int i=1,j=5;
    for( ; ;i++,j--)
    {
        cout<<"i = "<<i<<" AND j = "<<j<<endl;
        if(i==j)
        {
            cout<<"Hey i and j are equal."<<endl;
            cout<<"Its about time we should get out of loop."<<endl;
            break;
        }
    }
    cout<<"We are out of loop with i = "<<i<<" AND j = "<<j<<endl;
    system("PAUSE");
}

    Here we've already initialize value of i and j in the beginning of program. So there is no need to reinitialize them again loop. So area1 is left blank. 
    The 'if' statement in the loop is working as the condition check. How? What condition check
does is to bring the loop at an end and terminate it. Now,carefully look at our 'if' condition. It has a 'break' statement. Whenever 'break' is encountered, loop immediately gets terminated. No matter we still could have statements left to complete our execution of body of loop. In our program, this break is encountered when value of i and j becomes equal.This is the reason 'if' is serving as condition check in our program.
    Now look at area3. As we discussed earlier, a single area can have multiple statements to execute. In area3, we
have an increment and a decrement statement. You can also add a 'cout' statement or something else by just putting a comma.I leave it upto you to try it yourself.
    So with this knowledge we are ready to solve the problems that we generally face while solving these for loops.

    Here's the output of our program.



<<Evaluation Of for Loop                                    Home

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>>