Statements
Description
Statements are the basic building blocks of a program. A statement is an instruction for the computer to execute to perform a specific task. A program can alter the flow of control by using statements such as selection statements, iteration statements, and jump statements to change the order in which statements are executed.
Selection Statements
Selection statements are used to control whether or not a block of code is executed based on a condition.
If Statement
The most basic selection statement is the if
statement. The if
statement evaluates a condition, and if the condition is true, the block of code inside the if
statement is executed. If the condition is false, the block of code inside the if
statement is skipped and the next statement is executed. If there is an else if
following the if
, the condition is evaluated and its block of code is executed if the condition is true. This process repeats for each else if
statement. If there is an else
following the if
or else if
and the conditions are all false, the block of code inside the else
statement is executed.
Sometimes you will see an if
statement with just a single variable without any comparison operators. This checks if the variable is not equal to zero (i.e. != 0
). This is because the if
statement checks if the condition is true, and any non-zero value is considered true. Since false
is equal to zero, this is also a way to check if a boolean variable is true
or false
.
Switch Statement
The switch
statement solves the same problem as the if-else-if
statement, but it is easier to read and write. The switch
statement evaluates an expression, and if the expression matches one of the cases, the block of code inside the case is executed. If the expression does not match any of the cases, the block of code inside the default
case is executed. The break
statement is used to exit each case in the switch
statement.
Iteration Statements
Iteration statements are used to repeat a block of code a certain number of times or until a condition is met. Using iteration statements can reduce the amount of code you need to write.
While Statement
The while
statement is used to repeat a block of code until a condition is met. The condition is evaluated before the block of code is executed. If the condition is true, the block of code is executed. If the condition is false, the block of code is skipped and the next statement is executed.
Do-While Statement
The do-while
statement is similar to the while
statement, but the condition is evaluated after the block of code is executed. This means that the block of code is always executed at least once.
For Statement
The for
statement is used to repeat a block of code a certain number of times. The for
statement contains three parts: the iterator variable initialization, the condition, and the iterator variable update. The iterator variable initialization is executed before the block of code is executed, and the condition is evaluated before the block of code is executed. If the condition is true, the block of code is executed. If the condition is false, the block of code is skipped and the next statement is executed. After the block of code is executed, the iterator variable update is executed, and the condition is evaluated again. This process repeats until the condition is false.
Jump Statements
Jump statements are used to change the order in which statements are executed. The break
statement is used to exit a loop, and the continue
statement is used to skip the rest of the current iteration of a loop.