Showing posts with label introduction. Show all posts
Showing posts with label introduction. Show all posts

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

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

Basic Terms In C++

Basic Terms In C++

    On this page we are going to check out some very basic terminology for the beginners of C++. These terms are often used in programming. So here we go :-)
Variables : As the name suggest, values of a variable may vary from time to time within a program. These variables are stored in memory of our computer (RAM) and can be used to store data of any kind we wish to. Lets have a geek definition of a variable which we can use to impress our teachers:
"A variable is a way of referring to a memory location used in a computer program to hold important data comprising of simple numbers, alphabets, symbols, and numbers with decimal values."
While making a program we can choose any name for our variable just by giving little attention to the following rules:
  • We can only declare a variable with a maximum name length of 32 characters.
Well, our first rule has got exception because the maximum length for name of any variable depend on compiler to compiler. But still If you follow this rule, it will only help you because long variables only results in typing efforts and taking away our precious time. Now lets roll on to our next rules.
  • The first character of any variable can be either an alphabet or an underscore( _ ).
  • No two variables can have same name in the same scope.
For the above rule, there is a little thing that we need to know. Small letters and capital letters are treated differently by computer. This is what we call case sensitivity. So the variable names pet, Pet, pEt, peT, PeT and PET all different variables instead of one.
  • Commas or spaces are not allowed to be used in variable names.
This Stinks!! We cannot use a space!!! What if we want to use a variable name like "my cute pet"? For the solution of this question there is a general trend of using an underscore( _ ) instead of a space. So the variable name can be declared as "my_cute_pet".
  • Except underscore no special symbols can be used while naming a variable.
  • A keyword cannot be used as  a variable name .
I know it feels a little bore to remind all the rules while declaring any variable and check whether or not its satisfying all the rule, but trust me once you start programming, you'll get used to it.
Keywords : These are some words whose meaning are already known to a compiler (in broad sense to a computer).So of course its understood that we cannot define a word which is already defined. And that is what our last rule is trying to tell.

Comments : These are the part of the code that make no sense to compiler. That means if comments are encountered, compiler simply skips them and starts reading the next part of the code, no matter what is written in it. These are generally used to tell
which part of code is designed to do what function to the reader of program and it has nothing to do with compiler. Comments are also known as code tags or simply tags. There are two types of tags in C++ :
  • Multi line comments: These comments are also called C style comments. We can cover any number of lines under their scope. The syntax for using C style comment is as follows....
    /*  comments line 1
    comment line 2
    ...........
    ...........
    comment line n */

  • Single line comments: These comments are also called C++ style comments. We can only cover a single line of code under their scope. They start with double slashes(//) followed by whatever is to be written in comments. So the syntax is
    //Comment covering only 1 line
We will not discuss a lot about comments as we will see how they work in our programs later. For now the given information is sufficient.
Written by T-Rex
<< History of C++                        Programming-Gum Home              Decision Making>>

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