Statements are instructions of program, either in a form of a variable declaration or action, and end with a semicolon ;. When executing statements, the program usually follows first to last order sequence.

{
	statement1;
	statement2;
	statement3;
	...
}

Or in a single line, { statement1; statement2; statement3; }

Selection statements: if and else

Selection statements

Statements are executed if the conditions specified inside the parenthesis () are met or true

The if statement allows the program to execute the code statement or block inside if the condition parameters are met (aka true). Otherwise, the statement will not execute. The syntax does as follows:

if (condition) statement

Example

The following snippet executes a print message user is an adult if the given integer variable age is over 17

if (age > 17)
	cout << "user is an adult";

Additionally, in some point of your code where more than one coding statement is necessary, you must use braces {} to form a block:

if (age > 17) {
	cout << "user is an adult";
	cout << "he or she can apply for a credit card independently";
}

Indentation and line breaks generally maintain the same functionality as if it was in a multi-line code block.

if (age > 17) { cout << "user is an adult"; cout << "he or she can apply for a credit card independently";}


Alternative failover using else

Using the else keyword in the if statement creates an alternative statement for conditions that are otherwise not met aka false. The new syntax is as follows:

if (condition) statement1 else statement2

Using the previous example and adding an additional lines of code, let’s assume that the reassigned age variable is 17 :

if (age > 17) {
	cout << "user is now an adult";
	cout << "he or she can apply for a credit card independently";
} else
	cout << "user is ineligible for credit card sign-ups independently";

In this new example, the code will print user is ineligible for credit card sign-ups because the else statement is used as a catch-all.

There are some instances where you may want to use multiple statements but for different conditions. Multiple if + else structures creates concatenation of them (chain of conditions) with the first condition to check then descends down until another condition has been meet to execute a corresponding statement.

For instance:

if (age > 17) {
	cout << "user is now an adult";
	cout << "he or she can apply for a credit card independently";
} else if (age >= 13)
	cout << "user is only eligible to become an AU on their parents' cc";
else
	cout << "user is ineligible for credit card sign-ups independently";

Iteration statements (loops)

Loops iterates (repeats) the statement a certain number of times until the condition is no longer true. There are three iteration keywords, each serving similar purposes:

  • while loops the statement until the condition is no longer met
  • do while (similar to while) where statement is executed before checking the condition
  • for loops the statement as long as the condition is true

while loop

Syntax: while (expression) statement

In a while loop, the script checks to see if the expression is still valid before executing the statement. It will keep repeating until the expression is no longer valid.

Indefinite loop possible

You must consider adding a statement that will eventually force the expression inside the while loop to no longer become true. Otherwise, the loop will persist infinitely. The purpose of adding a statement --n is to have the while loop iterate 10 times, not go beyond that amount.

#include <iostream>
using namespace std;
 
int main() {
	int n = 10; // Initialize an integer variable n with a value of 10
				// n represents the number of iterations
	
	while (n > 0) { // Wrap the statement with a while loop that has an expression of n must be greater than zero
		// For every statement that passes, print a message
		cout << n << ", ";
		// Reduce the number of times by decreasing the variable itself
		--n;
	}
	
	cout << "Blast off!";
	return 0;
}

After executing the program, it should output:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Blast off!

do while loop

Syntax: do statement while (condition)

Similar to [while loop](2. Program structure/Statements and flow control > while loop), the statement is executed before checking the validation of the condition

For example,

#include <iostream>
using namespace std;
 
int main() {
	string str;
	do {
		cout << "Repeat after me: " <<
	} while ()
	return 0;
}

for loop

Syntax: for (initialization; condition; increase) statement;

Similar to the while and do loops, the loop repeats until the condition is no longer true. For this type of loop, it is used as a counter loop similar to previous examples of the different loops.

  • initialization - initializes an integer variable with a value (e.g. int n = 0)
  • condition - creates a limitation of the loop by checking its condition before running the statement.
  • increase - for every loop, the initialized variable will increase through the use of 1. Basics of C++/Operators > Increment and decrement (++, —)

Here’s an example:

#include <iostream>
using namespace std
 
int main() {
	for (int n = 0; n<10; n++) {
		count << "Looped " << n << " time(s)" << endl;
	}
	
	cout << "Loop is done";
}

In the example, we created the loop by initializing the variable, creating the condition to limit the number of iterations possible, and set an increment after finishing a loop.

Indefinite loop possible

If the for loop doesn’t specify a condition, the loop will continue iterating infinitely without stopping. Setting a condition helps to limit the number of iterations.

Alternatively, we could have initialized n as 10 , change the condition to n>0 or n!=0, and set the increase expression to n-- (similar to increase but decrease by one).

Multiple expressions

It is possible to have more than one expression for each initialization and increase. To have more than one expression, use the comma operator , to separate every expression. For example,

int n, i;
for (n=0, i=50; n != i; n++, i--) {
	// code
}

For every iteration, increase variable n by one and decrease the variable i by one. Until some time later, variable n matches with variable i, which as a result ends the loop because the condition is no longer true.

for ( n=0, i=50 ; n!=i ; n++ , i— )
Initialization
Condition
Increase

Multiple conditions

When setting in for more than one condition, you use the logical operators (&& or ||) to check for multiple conditions. You could create two groups of conditions by wrapping each group in parentheses like(condition1 && condition2) || (condition3 && condition4)

For instance,

int n, i;
for (n=0, i=50; n != i && i != 40; n++, i--) {
	// code
}

This loop stops at the 10th iteration because the loop has a second condition i != 40, even if n will not match i.

Jump Statements

continue keyword

Using the keyword continue skips the rest of the code block in the current iteration. This doesn’t end the future loops until the condition is no longer true.

for (n=0; n<10; n++) {
	// first part of code
	if (n == 8) continue; // ignores second part of the code if n == 8 but continues the loop until the condition is false
	// second part of the code
}

break keyword

Using the keyword break stops the current loop and future loops regardless of the condition.

for (n=0; n<10; n++) {
	// first part of code
	cout << n << ", ";
	if (n == 5) break; // Stops the rest of the loop code below and future loops
}

Switch statement

To be continued