Introduction to Programming : C++ #2

Hey, guys!

We are back  with 2nd part of C++. In case you haven’t been through the 1st part here is the link. :p

At times, you have to make decisions in your life. when you’re running low on your pocket money, you be like “OMG a Bradely Cooper movie or a Ryan Gosling movie”

Pretty much.

It’s really difficult at times! We know! similarly, your program too needs to make decisions sometimes and they do it logically, with the help of some statements. They control the flow of the execution of your program and are called Control structures. There are three types of control structures:

  1. Sequential (Default)

  2. Conditional

  3. Looping.

Let’s learn about each types one by one.

First up. The most basic of all.


When you type and tell your machine to execute your cute little code, the execution needs begin from somewhere, and what’s the best place to begin something. Yes,”From the Start”.

The execution begins from the start and the control is given to the function named main(). Duh. Now, the compiler asks “Now that I have started, what I do with the control”. The answer is the ‘sequential control structure’ .It is just simple common sense, in this the control follows a straight path, No turns. And more importantly NO GOING BACK. Just keep on executing from the start to the end.

Moving on.


This is something better than the previous one, Better because it is not boring as sequential.Conditional control structure actually have various members. Each member has it’s own feature, Now it’s up to you and how you want the execution to happen. Hence giving you the power the control the flow.As the name suggests you get that power through CONDITIONS.And HOW EXACTLY DO WE DO THAT? Using these guys(the members of the conditional control structure).

  1. If statement.

  2. If else statement.

  3. Nested If else

The first two are the most basic conditional statements. Nested if else is made using multiple if else statements.(We’ll see how, further in detail.).Let us now get an idea about what each one of these guys do.Ok? Here we go.

If : It’s Friday night! And you have a sleepover at your friend’s place tomorrow and you ask your mom for persmission. She says that she will allow you only “if” you do the dishes for a week. otherwise you don’t go and continue your routine work which you will do nonetheless. This is exactly what ‘If‘ control statement does in programming languages.



This basically means that if the condition is fulfilled, statement1 is executed , otherwise the statement is skipped. i.e, if you do the dishes you are allowed to go, otherwise you continue to do your routine work.

If else : This is really easy to understand.. This gives you extended power than “if” .A condition’s outcome is true or false. Nothing else .Like “7 is greater than 2?”TRUE “Sky is green?”FALSE “Megan Fox is hotter than your girlfriend?”, You get the point.Right? So you have a statement and you check whether it’s true or false. ‘If’ it is true, execute statement1 else (i.e, it is false) do statement2 (programming is all about execution of statements :P) an if-else statement looks like :





Getting this thing real.


cout<<“2 is greater than 1”;


cout<<“2 is smaller than 1”;

Output : 2 is greater than 1

Here, the first statement is executed since the first condition is true. Else, the second statement is executed.

Nested If else: Nested means inside one another.Nested if else means if-else inside an if-else.Yes, If-else-ception. If the outer “IF” is true than the control moves inside and encounter another “IF” and checks it’s truth value. Same happens for else.

if(condition1) //outer If


if(condition2) //inner if


else //inner else



else if(condition 3) //outer else




So , if condition1 is true and condition 2 is false, Statement2 is excuted.You can try for the rest on your own.

That’s all about conditional control structure.

Lastly, It’s time for the for some looping. Oh yeah!


Just as conditional has members ,viz If, If-else ,Nested if-else. Looping has too, and they are…*drumrolls*

1)The for Loop

2)The while Loop

3)The do-while Loop

The ‘for’ loop: No no, it doesn’t do anything “for” you, well not directly at least, but it performs an instruction or a set of instructions for the condition is true. So, It will keep on reapeating the instructions till the condition is false,basically, it executes a set of statements IN A LOOP. The condition is checked after every loop cycle, and once the condition is false,*bam*the control is thrown outside the loop. This loop works fine only if you know how many times you have to swirl around in the loop. What if I don’t know? Don’t worry we have answer for that too. But first. The for loop.


for (initialization; condition; increase or decrease)


It works in the following way:

  • Initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.

  • condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to the end step.

  • statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.

  • Increment/decrement is executed, and the loop gets back to step 2(where condition is checked).

  • The loop ends: execution continues by the next statement after it.

The three fields in a for-loop are optional. They can be left empty, but in all cases the semicolon signs between them are required. A loop with no condition is equivalent to a loop with true as condition (i.e., an infinite loop).

Let’s see an example to understand it better:

for (int timer = 10; timer > 0 ; timer – – )


cout << timer<< “, “;


cout << “liftoff!”<<endl;

Output : 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!

Now, the for loop has two siblings; while and do while

while: Earlier I mentioned that “for” loop is used when we know how many times to swirl, but what if I don’t know how many times? What do I do? While loop is our savior While loop simply repeats statement while expression is true. If, after any execution of statement, expression is no longer true, the loop ends, and the program continues right after the loop. It is an entry controlled loop which means that a condition is checked before entering the loop.

while (expression) statement

int main ()


int n =32 ;

while (n>0)


cout<<n<<” “;

n = n-5;



Output : 32 27 22 17 12 7 2

The youngest sibling of all the three brothers is the do-while loop. Stubborn as hell! No matter what, it will do what it wants at least once. This part is included in the do part of the loop. Then, the condtion is checked i.e., after the loop runs for the first time then it decides if it wants to enter the loop again or not.

do statement while (condition);

NOTE: this is the only loop in which there is a semicolon after the ‘while’;

Let’s see an example

Let’s think of a situation in which you have a crush on the cutest guy in the college and one day you decide to talk to him. The first time, you will talk to him no matter what. But, there will be a second time or no depends on how the first encounter goes. The do-while loop works in a similar way. It looks something like this:





while ( condition );

for example:


do {

cout<<“Hello, world!” ;


} while ( x != 0 );

here, “Hello, world!” is printed at least once till x=0 eventhough the condition is false.

So, That’s all folks. Stay tuned for new progamming stuff.Please contact us by email if you have any queries or suggestions.

Charmi Jagwani






And umm… wherever else you can find us.


Introduction to programming – C++

Once upon a time, there was an Emperor ruling the Earth. The powerful King would ensure safety and welfare of his people in return for wealth or service, and the commoners would grow food in their farms or cast swords and utensils out of iron or paint and sing and dance to entertain. And thus life would move on- in small, isolated pockets for most of the time.
Activities filled their lives; gaining information about various things, creating various things and to count various things.
But, one fine day, the King was consumed by desire for not only to defend and protect but also to conquer and become ever more powerful than before.


The King’s empire grew to tremendous sizes and reached to many, many kingdoms of the world. The King faced a crisis now. How would he count the gold in his kingdom now, how many minds will be required to keep track of give and take? It would take him months to figure out just a day’s work in his empire. He needed more worthy, fast and moreover accurate people to manage this giant problem of computation, to progress. He alone was just not enough.

And then, the scholars of his kingdom invented a way to do arithmetic. Counting was tedious and tiresome and lacked the required ‘Swag’ that was required to keep track of the gold. It was easier to make a person count a part , then another count another part and add them together. Even easier to make equal parts , count the no. of parts and multiply. This was exceedingly sexy and led to a revolution! The mathematical revolution!

Well , those things also broke down as time passed . Not because of efficiency , mind you , the algorithms i.e steps were extremely efficient. However , we weren’t fast enough to use them. This necessity , gave birth to computers. Devices that would count huge sums in a matter of seconds… well just not at the time of the king , but give or take a few thousand years later

Step inside the time machine and let’s return to our time.. we now have literally a billion times the information the king had to process. And we do it in seconds on our laptops and mobile phones.But , what the hell is really going on down there?

Well , we did invent the computer , but that bloody thing was brainless , so it couldn’t understand what to do when told to “Count all my gold or I’ll have your head!” in English.It did however, understand two simple things… on and off.. pulses high and low…0’s and 1’s.

This is appropriately called the ‘Machine language’. And , it tells the machine what we want and how to do it.

If we wanted to talk to the computer in this way , we could , but it’s a pain in the behind.

If you want to simply say “Hello World”, you will have to type to them

“01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100”

And that’s no fun.




But well , things were made easier.. a lot easier when ‘Higher languages’ were developed. which included a library i.e something that already has 0’s and 1’s inside it , and other basic syntax that was included in the library.

So , if you wanted a computer to add two numbers up , in c++ , the code would look somewhat like this

int a, b, sum;
cin >> a;
cin >> b;
sum = a + b;
cout << sum << endl;

And that’s done.Even if you cannot really understand the code above, you should be able to appreciate how much easier it will be to program in the C++ language as opposed to machine level language

Let’s head towards this charming lady named ‘compiler’. She acts as the Agony Aunt for us romantics as we try to flirt with these shrewd , calculating, data-churning, heart-breaking machines! She has two really hot sisters named interpreter and assembler. More on these two later, so roll your drooling tongues in and sit tight.

Imagine you have a date with a person who doesn’t speak your language and you don’t speak theirs… well , you hired her.. the compiler , to translate what each other are saying. She speaks both the languages fluently.




That’s easy. It’ll have blank spaces , comments and tokens..tokens are those things that the words the compiler can translate , blank spaces are… well blank spaces, and comments are those things you mumble to yourself so you can remember what you said.

Tokens consist of keywords ,  constants and variable (for you to assign something to ) ,and identifiers..used to identify variables and stuff.

Aand lastly operators viz. +(addition), -(subtraction ), *(multiplication), /(division).

The most basic command you can write is the famous “Hello , world” program.

which goes something like

1. // my first program in C++

2. #include <iostream.h>

3. int main()

4. {

5. cout << “Hello, World!” ;
6. return 0;
7. }

Output: Hello World!

Now , lets scrutinize the mumbo jumbo that we have here

the line 1. is a comment …just something you are saying to yourself

the second line , i.e. ‘2.’ is the pre-processor directive , or the library , or as I like to call it , the binary dictionary. It basically tells which words to use… more precisely , it allows a specific set of words to be translated.

The line 3. initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name “main” to the group of code statements that follow. Functions will be discussed in detail in a later, but essentially, their definition is introduced with a succession of a type ‘int’, a name ‘main’ and a pair of parentheses ‘()’, optionally including parameters(more on these dudes later).

The function named main is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function regardless of where the function is actually located within the code.

The { on line 4. and the } on line 6. are the things that are included in the ‘main’ function.

And , line 5. ‘cout << “Hello , world”;

Is you telling the machine to speak , or give an output (that’s where the ‘cout’ comes in ) of “Hello , world”.

And then you’re done! You just forced your computer into saying “Hello , world” . You finally have the upper hand…. YOU ARE NOW IN COMMAND! YOU CAN FINALLY GET SOMEONE TO SAY “I LOVE YOU”!….provided that someone is a heartless ,cold and calculating machine.*cough cough*

Now , to help us count the annoyingly large stacks of money you have , we need to assign variable to it. Which is pretty simple.. all you have to do , is say “Hey , see this shiz here…It’s ‘a’ .. an integer imma use later…and this shiz is b ,another integer ”

However that is written like this

‘ int a , b; ‘

And if you wanna , say add these two up , you need to introduce a third variable called ‘sum’

so just write down

‘int a,b,sum; ‘

(Oh , bt-dubs .. the ‘;’ and ‘{‘s’ and ‘<<‘s are annoying but important , since without them , the computer won’t understand crap..don’t blame me , blame the people who made c++ a case sensitive language…stupid butts )

And , well , you gotta ask computer to add them up….. NAY , YOU GOTTA COMMAND THE COMPUTER TO ADD THEM UP!


which is done by the operator ‘+’

And , to feed value into ‘a’ and ‘b’ , you need to type something called ‘cin’ . Where the ‘in’ stands for ‘input’.

AND NOW , let’s look at what your homemade calculator looks like in code


1. #include<iostream.h>
2. #include<conio.h>
3. void main()
4. {
5. int a,b, sum;
6. cout<<”Enter two numbers:\n”;
7. cin>>a>>b;
8. sum=a+b;
9. cout<<”Sum is\n”<<answer;

And you’ve done it!


PS: This program won’t work. There’s stuff to cover. There are things called ‘bugs’ that mess your code up or give you not quite what you wanted . Also, much more syntax! So, stay tuned for the next part.

I encourage you to download code::blocks and keep trying new things and see what comes out! Here is  a link

– Charmi Jagwani