Understanding C++ Control Statements
C++ Control Statements
Introduction
C++ control statements are used to make decisions and execute instructions on the basis of those decisions. They provide the looping construct which helps you repeat certain instructions until a given condition is true. C++ control statements include the if, switch, for, while, and do-while statements.
if Statement
The most basic and generally used control statement is the if statement. It allows you to execute a statement or block of statements only if the condition is true. It has the following syntax:
if (condition)
{
/* code to be executed if condition is true */
}
The condition can be any valid boolean expression. If the condition evaluates to true then the statement or block of statements enclosed within the if statement will be executed. Otherwise, the statement or block of statements will not be executed.
switch Statement
The switch statement is similar to the if statement, but it is used when you have multiple conditions that need to be tested. It can also be used to speed up your code by verifying one condition and executing different parts of the code depending on the condition. The switch statement has the following syntax:
switch (expression)
{
case value1 :
/* code to be executed when expression = value1 */
break;
case value2 :
/* code to be executed when expression = value2 */
break;
/* you can have any number of case statements */
default :
/* code to be executed when expression does not match any of the cases */
}
for Loop
The for loop is used to iterate through a block of code multiple times. It has the following syntax:
for (initialization; condition; increment)
{
/* code to be executed */
}
The initialization statement is executed only once when the loop is first entered. The condition is evaluated until it is false, at which point the loop is exited. The increment statement is executed every time the loop is re-entered.
while Loop
The while loop is similar to the for loop but it is used when you don't know in advance how many times the loop will execute. It has the following syntax:
while (condition)
{
/* code to be executed */
}
The code within the while loop is executed as long as the condition is true. Once the condition evaluates to false the loop is exited.
do-while Loop
The do-while loop is similar to the while loop but it differs in that the code within the loop is executed at least once before the condition is tested. It has the following syntax:
do
{
/* code to be executed */
}
while (condition);
The code within the do-while loop is always executed at least once. After the code has been executed the condition is tested and if it is true then the loop is re-entered. If the condition evaluates to false then the loop is exited.
Conclusion
C++ control statements provide an efficient way to make decisions and execute code based on those decisions. They are used extensively in modern programming languages and are a powerful tool for creating complex programs.