Control statements are elements in the source code that control the flow of program execution. They include blocks using {
and }
square brackets, loops using for, while, and do while, and decisions using if and switch, as well as goto. There are two types of control statements: conditional statements and unconditional statements. (Related recommendations: "C Tutorial")
Conditional statements in c
Sometimes, a program needs to be executed based on specific conditions. Conditional statements are executed when one or more conditions are met. The most common of these conditional statements is the if statement, which has the form:
if (condition) { statement(s); }
This statement will be executed as long as the condition is true.
c uses many other conditional statements, including:
if-else
: The if-else statement operates on an either-or basis. If the condition is true, one statement is executed; if the condition is false, another statement is executed.
if-else if-else
: This statement selects an available statement based on conditions. If no condition is true, the else statement at the end is executed.
while
: while repeats the given statement as long as it is true.
do while
: The do while statement is similar to the while statement except that the condition is checked at the end of the statement.
for
: The for statement will repeat a statement as long as the condition is met.
Unconditional control statements
Unconditional control statements do not need to meet any conditions. They instantly move control from one part of the program to another. Unconditional statements in
c include:
goto
: The goto statement points control to another part of the program.
break
: The break statement terminates the loop (repeating structure)
continue
: The continue statement is used in the loop to repeat the loop for the next value. The method is Transfers control back to the beginning of the loop and ignores subsequent statements.
The above is the detailed content of Use of C++ control statements. For more information, please follow other related articles on the PHP Chinese website!