Flow Control
Normal code is executed from top to bottom, starting from the main function, Flow control in C and C++ refers to the mechanisms that control the sequence in which statements are executed in a program. This includes decisions, loops, and jumps, allowing the program to behave differently based on conditions or to repeat certain actions.
To change the flow you can use a number of keywords:
- if, else
- Ternary
- Switch
- while, do-while
- For
- Range-based
- Try, catch, and throw
- Jump Statements
these keywords work with logical expressions:
- (x > 0)
- (y-2 < b)
and you have to use parentheses to format the condition.
If-else
int x = 7;
if (x > 10) {
printf("x is greater than 10\n");
} else if (x > 5) {
printf("x is greater than 5 but less than or equal to 10\n");
} else {
printf("x is 5 or less\n");
}
Ternary
It is a concise way to perform a simple if-else
statement. It is available in both C and C++, but it’s often used more frequently in C++ due to the language’s emphasis on concise and expressive code.
// condition ? expression_if_true : expression_if_false;
int a = 10;
int b = 20;
// Using ternary operator
int max = (a > b) ? a : b;
int z = 15;
// Finding the maximum value using nested ternary operators
int max = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);
switch case
int in = 0;
switch(in) {
case 0:
printf("Zero");
break;
case 1:
printf("One");
break;
default:
print("unknow");
}
while loop
It is repeats a block of code as long as a condition is true.
while(keepgoing)
{
// ...
break;
continue;
if (answer == 0)
{
keepgoing = false;
}
}
do-while
Similar to while
, but the condition is checked after the loop’s body is executed, ensuring the loop runs at least once.
int i = 0;
do {
printf("i = %d\n", i);
i++;
} while (i < 5);
for loop
Traditional for loop has three parts:
- initialiser
- continue condition
- incremented
for (int loop = 0; loop < 10; loop++ )
{
// ...
}
for range (C++11)
int arr[] = {1, 2, 3, 4};
for (int x : arr)
{
cout << x ; // will print 1234
}
Jump Statements
Jump statements allow the program to abruptly change the flow of execution.
break
statement
Exits a loop or switch
statement prematurely.
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("i = %d\n", i);
}
continue
statement
Skips the remaining code in the current iteration of a loop and moves to the next iteration.
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
printf("i = %d\n", i);
}
goto
statement
Jumps to a labeled statement. Its use is generally discouraged as it can lead to unstructured and hard-to-read code.
int x = 5;
if (x == 5) {
goto label;
}
printf("This won't be printed if x is 5\n");
label:
printf("Jumped to label\n");
try
, catch
, and throw
(C++ only)
C++ provides a structured way to handle errors and exceptions using try
, catch
, and throw
blocks.
#include
#include
int main() {
try {
int x = -1;
if (x < 0) {
throw std::invalid_argument("Negative value not allowed");
}
std::cout << "Value of x: " << x << std::endl;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
return
statement
Exits a function and optionally returns a value.
int add(int a, int b) {
return a + b;
}