Control Statements

Part 2 of “The Basics of Coding in C++”

Control Statements

Control statements control the flow of the program. What do you mean by flow, you ask? Well, there’s a simple way to define it:

Flow Chart

In this chart, called a flow chart, the program is depicted as a series of instructions, all executed depending on the arrows shown. These arrows show the direction of flow.

Types of Control Statements

There are actually three main types of control statements; namely:

  • Selection Statements
  • Iteration Statements (Loops)
  • Jump Statements

We will be discussing each of them one by one.

if selection statement

As we have discussed this earlier, the if selection statement goes like this:

if(statement)
{
//some code
}

It just checks if the statement is true or false, and then just runs the code inside the braces according to the condition (that we’ve called “statement” in this code block)

Nested if statement

As there is no rule to not do so, we can use two if statements inside one another. This is what’s called Nested if statement.

So this goes like:

if(statement)
{
// some code
	if(another_statement)
	{
		//some more code
	}
}

As you can see, there are two if statements, one nested inside the other. Therefore, this is called nested if

if ... else statement

An if ... else statement is a statement that allows for two cases:

  1. If the statement is true.
  2. If the statement is anything but.

Therefore, this is the syntax:

if(statement)
{
	//some code
}
else
{
	//some more code
}

if ... else ... if statement

An if ... else ... if statement is a statement, like if ... else, allows for more than one condition, but this allows for as many conditions as you want.

The syntax of this is:

if(statement_1)
{
	//some code
}
else if(statement_2)
{
	//some more code
}
else if(statement_3)
{
	//yet more code
}
else if(statement_4)
{
 	//more code
}
// and so on
else
{
	//last bit of code
}

So this statement basically allows you to make shorter programs when there are loads of conditions to use.

switch statement

On the other hand, if there’s a variable that is changing in value, the switch statement can be pretty useful.

Here’s the Syntax:

swtich ( variable)
{
	case case_name: //code_comes_here
	case case_name_2: //code_comes_here
	case case_name_3: //code_comes_here
	//some more cases
	case case_name_N: //code_here
}

This may be pretty tough to understand, therefore I’ll be taking up a problem to make it clear.

Let us suppose that there is a variable var that can take up values from 1 to 3. We need to find different things in each case, namely: Case 1: if var is 1 Output the number to the mod 2. Case 2: if var is 2 Output the number to the power 5. (There’s a reason I’ve taken 2) Case 3: if var is 3 Output if the number is a power of two or not.

First, let’s make the code for the individual cases:

A Simple Program with selection statements

Case 1: The modulus of a number (say, a) with respect to another number (say, b) is just the remainder that we happen to get when we divide one by the other (namely, the remainder of a/b). There’s an operator in C++ which does this very thing. It’s called the modulus operator (%). Therefore, we just need to find var%2! Simple stuff!

Case 2: The power of a number can be found in a variety of ways. You can either do cout<<2*2*2*2*2;, but hey, who wants the easy stuff? We are here to learn new things. Therefore, this is the new thing you’re gonna learn.

The C++ stream insertion operator? Well, it also has another job! You might have heard of Binary. Or at least might have heard of computers only being able to understand 0’s and 1’s right? That’s Binary. A computer can really only understand 0’s and 1’s.

How do we write two, then? You might have read about the Number system in Elementary School. The idea of a tens place and a Hundreds place must have been interesting. You could suddenly write however large a number you wanted to write! Any ideas how 2 would be written in Binary with this simple idea? No? Okay, then! This is how it is written. Just as 10 = 1 * 10^0 + 1 * 10^1, 2 = 0 * 2^0 + 1* 2^1. Or, for short, it is 10. Now, you might be asking: Why is this important to my C++ program? It does have importance. You see, the stream insertion operator («) can be used in this way too. Writing cout<< 2<<1; gives 4 as the answer. That’s because 4 = 0*2^0 + 0*2^1 + 1*2^2 = 100 That basically means that («), also called the left shift for this very reason, shifts the place value of the number by whatever number of places that you want it to be. This is going to prove to be useful later to calculate some (quite large) numbers. So the program for case 2 is just: cout<< 2 << 5;

Case 3: This one’s a pretty straightforward one.

The program is just:

 /* The == is used to compare the equality of two variables or a variable and a constant. In contrast, = is used to assign one variable to another variable/constant */
if(var%2==0)
{
	cout<<"This is a power of two!";
}
else cout<<"Not a power of two!";

The Final Program

#include<iostream>
using namespace std;
int main()
{
int var;
cout<<"Enter the variable:"<<endl;
cin>>var;
switch(var)
	{
		case 1: cout<<var%2<<endl;
			break; //This part is to ensure that the program doesn't continue into other cases.
		case 2: cout<< 2 << 5;
			break;
		case 3: if(var%2==0)
			{
				cout<<"This is a power of two!";
			}
			else cout<<"Not a power of two!"; //writing a break; in here is pretty much optional
	}
}

Iteration Statements

These are the “loops”, statements that keep on repeating until a particular condition is achieved.

while loop

This probably could be called the mother of all loops, as, I will show you later, all loops can be written as a while loop. It is truly awesome.

Syntax:

while (condition)
{
	//some code
}

As you can see, the code will keep executing itself until the starting condition isn’t fulfilled (i.e., condition becomes false).

The do ... while loop

This loop is a bit different from the while loop, in the sense that the code enclosed within braces will get executed at least once, even if the starting condition isn’t fulfilled.

Syntax:

do
{
	//some code
}while(condition);

Keep the semicolon-at-the-end in mind.

Equivalent Syntax using only while loops:

//code
while(condition)
{
	//same code
}

As you can see, the code would be executed at least once, regardless of the condition.

The for loop

This is the loop that everyone that codes at any company likes, and it isn’t unwarranted. A for loop does three things in a single line, and people like it.

Syntax:

for(initialisation; stopping_condition; extra_condition)
{
	//some code
}

What’s so special about this loop is that it does three things at once, namely:

Initialisation: It initialises a (normally declared) variable, like setting an int apple to zero Stopping condition: The loop will run until this condition is satisfied. Extra Condition: The loop will keep execute this condition at the end of each run through the loop, or as we call it, each iteration.

Therefore, the equivalent while loop syntax would be:

while(stopping_condition)
{
	// initialisation
	//some code
	//extra_condition
}

Jump Statements

There are two jump statements, namely:

  • break and
  • goto

break statement

This statement basically breaks the program from the loop, as seen in the example.

goto statement

A goto statement is essentially used for going to a certain marked part of the program. It can essentially be used as an alternative for a while loop (although this isn’t encouraged).

Syntax:

// In some early part of the program
marked_area: //code
//more code
// some more code
goto marked area;

This makes the program go back to the marked area. Usage as a while loop:

marked_area:
if(condition)
{
	//code
	goto marked_area;
}

This was my tutorial on Control Statements. Hope you enjoyed it!