if is a c++ condition statement.
Syntax:
if (<<condition-expression>>) { <<evaluate-statement-1>>; <<evaluate-statement-2>>; // ... more statements } else { <<evaluate-statement-1>>; <<evaluate-statement-2>>; // ... more statements }
c++ example:
int x; x = 4; if (x == 4) { std::cout << "x is 4" << std::endl; } else { std::cout << "x is not 4" << std::endl; }
Because x is 4,
so x == 4 is true, so
will be executed, std::cout <<
"x is 4" <<
std::endl;
will be not executed.
std::cout
<< "x is not
4" << std::endl;
switch is another c++ condition statement, switch is a more powerful if
Syntax:
switch (<<value-expression>>) { case <<value1>>: <<evaluate-statement-1>>; break; case <<value2>>: <<evaluate-statement-2>>; break; // more case ... // more case ... default: <<evaluate-statement-N>>; break; }
<<value-expression>>
is equal to <<value1>>
,
it will execute <<evaluate-statement-1>>;
<<value-expression>>
is equal to <<value2>>
,
it will execute <<evaluate-statement-2>>;
<<evaluate-statement-N>>;
"inside" th default will be
executed.
break
means "break and quit the switch immediately", otherwise another
case might match the condition, and an unexpected statement will be executed,
so you'd better add a break "inside"
every case.
three-op-operator
:?
is also another c++ if-condition. It
is more short for value judgement.
?:
Syntax:
auto result = <<condition>> ? <<expression-1>> : <<expression-2>> ;
<<condition>>
is true, the <<expression-1>>
will be executed, and the result is <<expression-1>>
<<condition>>
is false, the <<expression-2>>
will be executed, and the result is <<expression-2>>
c++ example:
bool x = 3; std::string result = (x==3)?"true":"false";
c++ for loop
Syntax:
// for-loop: for (<<init-statement>>; <<judge-condition>>; <<change-expression>>) { // loop-body begins <<evaluate-statement-1>>; <<evaluate-statement-2>>; // ... more statement // ... more statement } // loop-body ends
for-loop
will execute the loop-body again and again,
...
<<init-statement>>
is used to set the initial status, it is used to initialize a variable's
value commonly.
<<judge-condition>>
is an expression that can be judged to true
or false, if it is true,
the loop-body will be executed again;
if it is false, the for-loop
will quit.
<<change-expression>>
is used to change the status of something.
You don't want to execute the same loop-body
commonly, so <<change-expression>>
can be used to change the status.
Infinite
loop
- If the <<judge-condition>>
is always true, the for-loop will run forever
until some external thing interrupts it. This is commonly used in a game-rendering
loop, a user click might trigger an event to interrupt the loop.
{}
can be omitted, if it has only one <<evaluate-statement>>
;
otherwise the brackets can not be omitted.
Run orders:
break;
statement can be inserted inside the loop-body
.
c++ example:
for (int i=0; i<5; ++i) { std::cout << "i is " << i << std::endl; }
c++ range-based for loop can be also called c++ for each.
Syntax:
for (<<declare-a-variable>>: <<initializing-value-list>>) { // loop-body begins <<evaluate-statement-1>>; <<evaluate-statement-2>>; // ... more statement // ... more statement } // loop-body ends
break;
statement can be inserted inside the loop-body
.
c++ example:
for (std::string str: {"c++", "world", "hello world"}) { std::cout << "str is " << str << std::endl; }
c++ while loop
Syntax:
while (<<judge-condition>>) { // loop-body begins <<evaluate-statement-1>>; <<evaluate-statement-1>>; // ... more statement // ... more statement } // loop-body ends
Infinite
loop
- If the <<judge-condition>>
is always true, the while-loop will run forever
until some external thing interrupts it. This is commonly used in a game-rendering
loop, a user click might trigger an event to interrupt the loop.
break;
statement can be inserted inside the loop-body
.
c++ example:
int x = 0; while (x < 10) { std:cout << "x is " << x << std::endl; ++x; // change-statement }
c++ do-while loop
Syntax:
do { <<evaluate-statement-1>>; <<evaluate-statement-2>>; // ... more statement // ... more statement } while (<<judge-condition>>);
;
should be placed at the end of do-while-loop
.
c++ example:
int i = 0; do { std::cout << "i is " << i << std::endl; ++i; } while (i<10);
Written on Dec 06, 2024
c++ std::exception:
std::cout.write(err.data(), err.size());
std::cout << std::endl;
caught:
=================================== # The c++ programming language. # # # # Join c++ Discord: yZcauUAUyC # # Deck # ===================================