There are some moments in life when we have to make decisions, and according to these decisions, we decide how things will go next. Similarly, in computer programming, we need to make some decisions in our code flow based on a certain set of inputs, and these decisions tell us which code block to execute next and which code block to skip.

  1. Getting Started with ” if ” conditions
  2. Switch Statement in C++
  3. Jump Statements in C++

C++ Conditions

Getting Started with ” if ” conditions

This is the simplest decision-making statement that can be used to decide which block of code will be executed next. This decision is based on whether a certain condition evaluates to FALSE or TRUE.

Example:

if(some_condition)
	statement;

In this example, if the variable named “some_condition” does not evaluate to TRUE, the code block that follows will not be executed.

Example:

if(some_condition)
	statement_1;
	statement_2;

In this example, similarly, if “some_condition” evaluates to TRUE, “statement_1” is executed. However, in this case, since curly braces were not explicitly used following the if condition, only the statement that follows the condition will be executed. Meaning in this case, if “some_condition” evaluates to FALSE, only “statement_2” will be executed and “statement_1” will be skipped since it is the only statement associated with the if condition.

Example:

if(some_condition)
{
	statement_1;
	statement_2;
}

Here curly braces are used to associate one or more statements to the if condition.

Example program to:

  • evaluate the input variable “x”
  • if “x” is greater than 10, set it to 20
#include <iostream>
using namespace std;

int main()
{
	int x = 5;
	if(x > 10)
	{
		x = 20;
	}
	cout << "x equals: " << x << endl;
}

Output:

x equals: 5

Here in this example, the value “x” is not greater than 10, then its value remains unchanged.

if-else” statements

The if condition only executes the following block of code if the condition evaluates to TRUE. Now, what happens if you want to execute a separate block of code in the case of TRUE and another block of code in the case of FALSE? In this case, we use the “else” statement.

Example program to:

  • evaluate the input variable “x”
  • if “x” is greater than 10, set it to 20
  • if “x” is less than or equal 10, set it 0
#include <iostream>
using namespace std;

int main()
{
	int x = 5;
	if(x > 10)
	{
		x = 20;
	}
	else
	{
		x = 0;
	}
	cout << "x equals: " << x <<endl;
}

Output:

x equals: 0

Here the condition whether “x” is greater than 10 evaluates to FALSE. Therefore, the block of code that follows the “else” statement is executed setting “x” to zero.

if, else-if and else” statements

Now imagine you have a variable that you want to check against a certain value, and in case your check does not evaluate to TRUE, you would like to check it against another value. Here comes the work of the “else-if” statement.

Example program to:

  • check the input variable “x”
  • if “x ” is less than 10, set it to 0
  • if “x” is less than 20, set it to 10
  • otherwise, leave it unchanged
#include <iostream>
using namespace std;

int main()
{
	int x = 15;
	if(x < 10)
	{
		x = 0;
	}
	else if(x < 20)
	{
		x = 10;
	}
	
	cout << "x equals: << x << endl;
}

Output:

x equals: 10

Here the value “x” has not satisfied the first condition yet satisfied the second. Therefore, the value “x” is set to 10.

Nested “if and if-else” statements

What if you need to check a variable against a certain value, and if your check is satisfied, you need to check it against another value? One way to do this is that you can use “if” statements inside of other “if” statements.

Example program to:

  • check input variables “x” and “y”
  • if both, “x” and “y” are greater than 10, set “y” equals “x”
#include <iostream>
using namespace std;

int main()
{
	int x = 15;
	int y = 20;

	if(x > 10)
	{
		if(y > 10)
		{
			y = x;
		}
	}

	cout << "x equals: << x << endl;
	cout << "y equals: << y << endl;
}

Output:

x equals: 15
y equals: 15

For sure there is another way to implement this example using the “&&” operator as in the following code snippet.

#include <iostream>
using namespace std;

int main()
{
	int x = 15;
	int y = 20;

	if( (x > 10) && (y > 10) )
	{
		y = x;
	}

	cout << "x equals: << x << endl;
	cout << "y equals: << y << endl;
}

Output:

x equals: 15
y equals: 15

In addition to the “&&” operator, you can also use the “||” operator that represents logic or operation, OR the exclamation mark “!” that represents the logic NOT operation.

Switch Statement in C++

Switch case statements are mainly used to replace long if statements and are specifically utilized if you need to check your variable against a whole lot of possible values. Just like if conditions, switch cases allow you to change your path of execution, favoring branches over branches in an easy and readable way.

Usage and syntax:

switch (var)
{
	case a: // execute if var = a
		break;
	case b: // execute if var = b
		break;
	default: // execute if var is not equal a or b
}

The default case can be compared to the “else” statement, where this block of code is executed if and only if all previous conditions were not satisfied.

Example program to:

  • evaluate the input variable “x”
  • if “x” equals 30, set it to 20
  • if “x” equals 20, set it to 10
  • otherwise, set it to 0
#include <iostream>
using namespace std;

int main()
{
	int x = 20;
	switch(x)
	{
		case 30: 
			x = 20;
			break;
		case 20: 
			x = 10;
			break;
		default: 
			x = 0;
	}
	
	cout << "x equals: << x << endl;
}

Output:

x equals: 10

Note that the “break” statement tells the compiler to stop executing the switch case statement.

Some points to consider when writing switch case statements

  • The expression passed to the switch statement needs to evaluate to a constant value or else it will not be valid.
  • You can not use duplicated cases. For example, if “x” is checked to be equal “a” in a case, you can not repeat this same check again in a different case.
  • The default case is optional, which means you can just not write it if you want. However, it is still recommended that you write your default scenario.
  • The break statement is optional, if not present, the code will continue executing and evaluate the next case.
  • Nesting of switch statements is allowed, which implies using a switch case inside a specific case for example. On the other hand, it is not recommended because it makes your code not readable and complex to debug.

Jump Statements in C++

C++ break

The “break” statement is typically used to terminate a loop even if the loop continuity condition is not satisfied yet.

Example program to:

  • initialize integer variable “x”
  • loop from 0 to 10
  • if 5 equals loop iterator, break from the loop (use if conditions)
  • otherwise, set “x” equals loop iterator
#include <iostream>
using namespace std;

int main()
{
	int x, i;
	for(i=0; i<=10; i++)
	{
		if(i == 5)
			break;
		else
			x = i;
	}
	cout << "x value equals: << x << endl;
}

Output:

iterator equals: 4

In this example, you can see how the value of “x” is 4 because the loop was stopped before assigning the iterator’s value to “x”.

C++ Continue

Unlike the break statement, “continue” forces the loop to go to the next iteration. On the other hand, it is used when you do not want the code block associated with the loop to be executed for this specific iteration.

Example program to:

  • initialize integer variable “x”
  • loop from 0 to 10
  • if 5 equals loop iterator, continue loop execution (use if conditions)
  • otherwise, set “x” equals loop iterator
#include <iostream>
using namespace std;

int main()
{
	int x, i;
	for(i=0; i<=10; i++)
	{
		if(i == 5)
			continue;
		else
			x = i;

		cout << "x value equals: << x << endl;
	}
}

Output:

x value equals: 0
x value equals: 1
x value equals: 2
x value equals: 3
x value equals: 4
x value equals: 6
x value equals: 7
x value equals: 8
x value equals: 9
x value equals: 10

Notice how assigning the value 5 to “x” was skipped using the continue statement.