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

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

if-else Statements

if-else Statements

    Till now,we just threw light on uses of 'if'. Now its about time we should head for the counter part of 'if' i.e. 'else'. Its very easy to take down this topic in one shot.  So we'll discuss just a single program regarding this topic.
    What basically 'else' refers to is the left over of 'if'. In simple language, consider we write our 'if' condition as if(i<8), then this 'if' will include all the values of i which are smaller than 8(not including 8). Now the left over of this 'if' are the values of i including 8,9,10 ......and all above values.These values are covered in else.The syntax of writing 'if' with 'else' is as follows:

if(condition)
    statement 1;
else
    statement 2;

Remember, unlike 'if' statements we do not need to write any condition in 'else' statements which are covered by a pair of brackets. All the conditions which are not a part of 'if' are automatically covered in 'else'. In the last example we read in 'if' topic which had a logical NOT operator (!), the second 'if' can be replaced by else.Why is it so you ask? It is because all the conditions not satisfied by 'if' were FALSE. And NOT operator changes all these FALSE conditions to TRUE. So indirectly it is including all the conditions left by 'if'. Lets consider an example.

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
    int a;
    cout<<"\nEnter value of a : ";
    cin>>a;
    if(a==2609)
        cout<<"\n Holla!!!! Thats my birthday";
    else
        cout<<"\nNop! Thats not my BD.";
    system("PAUSE");
    return 0;
}

For creating a link between this program with the one we studied earlier(one having NOT operator), I'll only say,we could have written that program in the following way:

#include <cstdlib>                     
#include <iostream>                     //Header Files
using namespace std;
int main()                             //main function
{
    system("CLS");                     //clears any printed, if any
    int condition;
    cout<<"\nEnter value of condition : ";
    cin>>condition;
    if(condition)        // 1st if
        cout<<"\nYour condition evaluates to TRUE.";
    else                    // Replacement of 2nd if
        cout<<"\nYour condition evaluates to FALSE.";
    system("PAUSE");                  // Freeze the screen to show the output
    return 0;
}

The concept is easy and self explanatory.The next topic we'll be covering is "multiple statement under if and else".

Written By T-Rex
<<'if' Statements                                  Home                              Multiple statement under if-else>>

Decision Making

Decision Making


    Its obvious tht we all face conditions in our life when we aren't sure about performing some task until we don't have sufficient data to make our decision. These situations are also encountered by programmers when their client isn't sure about the data and asks the programmer to make a program that reacts immediately when input is provided. This is when decision making plays its role. Under this section we are going to cover the following topics:
  •  if-else statements
  •  switch case statements

    Knowledge of these topics will enable you to make programs that can take different actions depending upon the data provided by user, no matter whatsoever the data may be. So lets deal with our first topic i.e. if-else statements.
Written by T-REX


<<Basic Terms In C++                                HOME                                'if' Statements>>

History Of C++

History Of C++

    Our History begins in  the year 1979, when their lived a genius called Bjarne Stroustrup, working hard for his PhD thesis. By this time he was a master of a language, used for simulation, and so called Simula. Simula 67, which Stroustrup worked with is also known to be the first language to support a very important feature of programming called Object Oriented Programming (OOP).
    The Problem he encountered with Simula was with its speed. It was too slow to be used for any practical purpose.
    Stroustrup was a great supporter of the language 'C', which was raised by Dennis Ritchie in 1972. He began to work on a project which he called " C with Classes ". The main objective of his project to integrate the features of OOPs with C language.
    The first  'C with Classes' compiler was derived from C compile 'CPre' and was named as 'CFront'. It was programmed to translate a 'C with Classes' code to an ordinary 'C' language code. CFront left a legacy of 'C with Classes' and a way ahead of it was a long journey of development of many software. In 1993 it was abandoned because of the difficulties encountered while integrating it with new features of 'C with Classes'.
    In 1983 name of the language 'C with Classes' was changed to its current name 'C++'. Since the time C++ came into sight, it has always been a great choice for programmers to be used as a reliable, fast and secure language to develop software.
    This is how the glorious language, which we call C++, was born.
"History is written by victors
and many victors
used C++
to write their program."
written by T-Rex
                                                Programming-Gum Home               Basic terms in C++ >>