Statement Scope

里的只有里用,外的可以往里用

Simple Statements

  • Most statements in C++ end with a semicolon. (null statement: ;)

Compound Statements (Blocks)

A compound statement, usually referred to as a block.

  • An empty block is equivalent to a null statement

Conditional Statements

The if Statement

The switch Statement

  • The expression is converted to integral type. (计算switch后面的expr)
  • case labels must be integral constant expr

⚠️ Defining an empty default section indicates to subsequent readers that the case was considered.

Variable Definitions inside the Body of a switch

没怎么听懂,但大概意思是被初始化的变量不能用,没被初始化的可以用,因为在一个scope中,跳出去用会没初始化

再来:不能从out of scope到in scope without initializing

Iterative Statements

The while Statement

⚠️ Variables defined in a while condition or while body are created and destroyed on each iteration.

Traditional for Statement

for (init; cond; expr)

  • 如果是init是init-statement,那么初始化这一步只做一次。(其实就是想说这里的scope是整个循环,而不是每次循环。区别于 while)
  • 只有每次循环结束了,才会 evaluate the expression
  • init-statement can define several objects.

Range for Statement

like the for in Python

for(/*declaration*/ : /*expression*/)
	/*statement*/
  • expression is an object of a type that represents a sequence
  • declaration defines the variable that we’ll use to access the underlying elements in the sequence
  • On each iteration, the variable in declaration is initialized from the value of the next element in expression. means not the element itself, but a new variable have the value of the element.

Example

==easy to use auto:==

for(auto c : str)

to Change the Elements in Sequence?

use reference, because reference is actually the element:

for(auto &c : str)

The do while Statement

⚠️ 就算保底会运行一次,但是还是遵循着 “cond用不了state中的init”

Jump Statements

The break Statement

The continue Statement

  • while and do while will continue to evaluate the condition.
  • traditional for will continue at the expr. (这里我实验,发现是expr之后再cond,其实不冲突,实际上就是去expr,然后进入下一个iterate,而进入下一个iterates的第一步就是cond)
  • range for will continue by init the control variable from the next element

The goto Statement

  • jump in the same function.
  • ⚠️ 跟switch类似,不能跳init直接用
  • ⚠️ 但可以跳回init,destroy then construct again

try Blocks and Exception Handling

🔗return Statement