-
Statements and flow control
A simple C++
statementis each of the individual instructions of a program, always end with a semicolon(😉, and are executed in the same order in which they appear in a program.-
Selection Statements: if and else
The if keyword is used to execute a statement or block, if, and only if, a condition is fulfilled. Its syntax is :
if (x == 100) cout << "x is 100"; if (x == 100) { cout << "x is "; cout << x; }Selection statements with if can also specify what happens when the condition is not fulfilled, by using the else keyword to introduce an alternative statement. Its syntax is :
if (x == 100) cout << "x is 100"; else cout << "x is not 100";Several
if + elsestructures can be concatenated with the intention of checking a range of values.if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0"; -
Iteration Statements (loops)
Loops repeat a statement a certain number of times, or while a condition is fulfilled. They are introduced by the keywords
while,do, andfor.-
The while loop
The simplest kind of loop is the while-loop. Its syntax is:
while (expression) statementThe
while-loopsimply repeatsstatementwhileexpressionistrue. If, after any execution of statement, expression is no longer true, the loop ends, and the program continues right after the loop.A thing to consider with while-loops is that the loop should end at some point, and thus the statement shall alter values checked in the condition in some way, so as to force it to become false at some point.
-
The do-while loop
A very similar loop is the do-while loop, whose syntax is :
do statement while (condition);It behaves like a
while-loop, except that condition is evaluated after the execution ofstatementinstead of before, guaranteeing at least one execution of statement, even if condition is never fulfilled. -
The for loop
The for loop is designed to iterate a number of times. Its syntax is:
for (initialization; condition; increase) statement;Like the
while-loop, this loop repeatsstatementwhileconditionis true. But, in addition, thefor loopprovides specific locations to contain aninitializationand anincreaseexpression, executed before the loop begins the first time, and after each iteration, respectively.Therefore, it is eapecially useful to use counter variables as
condition.It may be useful to execute more than a single expression as any of
initialization,condition, orstatement.Range-based for loop
The for-loop has another syntax, which is used exclusively with ranges:
for (declaration : range) statement;This kind of
for loopiterates over all the elements in range, where declaration declares some variable able to take the value of an element in thisrange.Rangesare sequences of elements, including arrays, containers, and any other type supporting the functions begin and end. -
Jump statements
-
The break statement
break leaves a loop, even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.
-
The continue statement
The continue statement causes the program to skip the rest of the loop in the current iteration, as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.
-
The goto statement
goto allows to make an absolute jump to another point in the program. This unconditional jump ignores nesting levels, and does not cause any automatic stack unwinding(堆栈解退).
The destination point is identified by a label, which is then used as an argument for the goto statement.
A
-
-
-
C++学习系列三:Statements and Flow Control||Functions||Overloads and Templates||Name Visibility
最新推荐文章于 2023-01-29 11:02:07 发布

最低0.47元/天 解锁文章
1396

被折叠的 条评论
为什么被折叠?



