Conditionals & loops- if, else-if, else, switch, for, while in C++

if, else,else-if, switch are the conditional statements in C++. The looping keywords are for, while, and do-while loops.

In this article, I will give you an overview of the conditionals and looping in C++. Conditional statements usually have a condition that requires to be met in order to execute a certain set of statements.

if, else if, else conditionals

Firstly, I discuss here about if statement. When a particular condition is met then the if is executed. The syntax is as follows.

if(condition)
{
      //lines of code
}

condition includes a logical conditions in mathematics. The condition result should evaluate to true or false. When it turns to be true the lines of code inside the if statement will be executed.

int conditionals()
{
	int a = 7; 
	int b = 9; 
	bool resultAdd = false;
	if (a + b == 16)
	{
		resultAdd = true; 
	}
	if (resultAdd == false)
	{
		cout << " The sum is not 16" << endl;
	}
}

In the example, we have added the numbers and check the result to be 16. If this evaluates to true then resultAdd will be set to true.

Secondly, ‘else’ comes into the picture when an ‘if’ condition does not return to be true. In the above example, we can replace one of the if statements with else. After replacing the if with an else the program looks as follows.

int conditionals()
{
	int a = 7; 
	int b = 9; 
	bool resultAdd = false;
	if (a + b == 16)
	{
		resultAdd = true; 
	}
	else
	{
		cout << " The sum is not 16" << endl;
	}
}

Thirdly, there is one more thing called else if. else if will be the next to be evaluated after if statement. else if is valid only when there is an if statement.

int conditionals()
{
	int input; 
	cout << " Enter a number between 1 to 4"<<endl;
	cin >> input;
	if (input == 1)
	{
		cout << "User entered 1" << endl; 
	}
	else if ((input == 2) || (input == 3))
	{
		cout << "User entered a number \> 1 and \<4"<< endl;
	}
	else if(input ==4)
	{
		cout << "User entered 4" << endl;
	}
	else
	{
		cout << "User entered an invalid choice" << endl;
	}
}

Here I have asked the user to enter a number between a range. Based on the input I check if it is a 1, then I check if it is a 2 or a 3. After that, I check for 4. Lastly, I check for any other possibility through ‘else’.
Note: I have used || which indicates a Logical OR. When one of the conditions is true then the else if lines of code will be evaluated. Logical AND can be used as && in order for the statements to be executed all the conditions need to be true.

switch condition

switch is used when we want to select one code block out of multiple blocks. Many times multiple else if can be substituted to a switch. I have substituted the above code to a switch in the below example.

int conditionals()
{
	int input; 
	cout << " Enter a number between 1 to 4"<<endl;
	cin >> input;

	switch (input)
	{
	case 1: cout << "1: ONE" << endl;
		break; 
	case 2: cout << "2: TWO" << endl;
		break;
	case 3:cout << "3: THREE" << endl;
		break;
	default:
		cout << "DEFAULT CASE" << endl;
	}
}

There are 3 cases and a default case. Based on the option user enters the particular code block will be executed. When any other number is entered apart from 1-3 then it falls in the default case. Note that I have added a break in all of them. If I don’t add a break then after a particular case turns to be true all the subsequent cases will be executed. We do not want that to happen so we should add a break after every case.

for, while and do-while loops

What is a loop? A loop is where we execute a code block repeatedly until a particular condition holds good. There are various types of loops in C++, One is for and the other is while.

The general syntax of a for loops is as follows.

for(initialization; condition; increment)
{
      //code block
}

An example to print the sum of numbers from 1 to 10 using a for loop.

void loops()
{
	int total = 0;
	for (int i = 1; i <=10; i++)
	{
		total+=  i;  
	}
	cout << "The total is " << total << endl;
}
///********OUTPUT**********/
The total is 55

I use the for loop to initialize a variable called i to 1. It repeatedly checks if i is still less than or equal to 10. We increment at every step. When I reach 11 it comes out of the loop and prints the total.

We shall see how to use while loops.

While loops have a condition and keeps executing the block of code inside while until the condition is true.

Syntax

while(condition)
{
    //code block
}
void loops()
{
	int counter = 0; 
	while (counter < 5)
	{
		counter++;
		cout << counter << endl;
	}
}

Every time I check for the counter and it evaluates to true, the counter is incremented and cout will print the number.

We have to be careful of these kinds of loops when there is no breaking condition they can execute forever.

Do-while loop is similar to a while but it makes sure that the code block executes at least once irrespective of the condition.

Syntax:

do
{
}while(condition); 

Conclusion

We learned how to use loops in this tutorial. In the next one, I will talk about Pointers, Arrays, and dynamic arrays.

Your subscription could not be saved. Please try again.
Your subscription has been successful. The email is sent to you with the attachment!

Download your eBook on C++

Conditionals

Subscribe here and get your copy. You can download it on your local device.