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.

1] SEQUENTIAL.

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.

2] CONDITIONAL

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.

if(condition)

statement1;

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 :

if(condition)

statement1;

else

statement2;

Getting this thing real.

if(2>1)

cout<<“2 is greater than 1”;

else

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

statement1;

else //inner else

statement2;

}

else if(condition 3) //outer else

Statement3;

else

Statement4;

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!

3]LOOPING

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.

Syntax:

for (initialization; condition; increase or decrease)

statement;

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:

do

{

statements;

}

while ( condition );

for example:

x=0;

do {

cout<<“Hello, world!” ;

x++;

} 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

Email: captainknowledge1@gmail.com

DOWNLOAD OUR EXCLUSIVE FREE ANDROID APP  HERE : http://goo.gl/Fs9nbl

LIKE US ON FACEBOOK OR WE WILL KILL PUPPIES  : http://goo.gl/ch9cg1

AND TWITTER : http://goo.gl/pw0hWr

AND QUORA : http://goo.gl/XndilW

And umm… wherever else you can find us.