Tutorial by Examples

Introduces a case label of a switch statement. The operand must be a constant expression and match the switch condition in type. When the switch statement is executed, it will jump to the case label with operand equal to the condition, if any. char c = getchar(); bool confirmed; switch (c) { c...
According to the C++ standard, The switch statement causes control to be transferred to one of several statements depending on the value of a condition. The keyword switch is followed by a parenthesized condition and a block, which may contain case labels and an optional default label. When t...
The catch keyword introduces an exception handler, that is, a block into which control will be transferred when an exception of compatible type is thrown. The catch keyword is followed by a parenthesized exception declaration, which is similar in form to a function parameter declaration: the paramet...
In a switch statement, introduces a label that will be jumped to if the condition's value is not equal to any of the case labels' values. char c = getchar(); bool confirmed; switch (c) { case 'y': confirmed = true; break; case 'n': confirmed = false; break; default: ...

if

Introduces an if statement. The keyword if must be followed by a parenthesized condition, which can be either an expression or a declaration. If the condition is truthy, the substatement after the condition will be executed. int x; std::cout << "Please enter a positive number." &lt...
The first substatement of an if statement may be followed by the keyword else. The substatement after the else keyword will be executed when the condition is falsey (that is, when the first substatement is not executed). int x; std::cin >> x; if (x%2 == 0) { std::cout << "Th...
Jumps to a labelled statement, which must be located in the current function. bool f(int arg) { bool result = false; hWidget widget = get_widget(arg); if (!g()) { // we can't continue, but must do cleanup still goto end; } // ... result = true; end...
Returns control from a function to its caller. If return has an operand, the operand is converted to the function's return type, and the converted value is returned to the caller. int f() { return 42; } int x = f(); // x is 42 int g() { return 3.14; } int y = g(); // y is 3 If re...
When throw occurs in an expression with an operand, its effect is to throw an exception, which is a copy of the operand. void print_asterisks(int count) { if (count < 0) { throw std::invalid_argument("count cannot be negative!"); } while (count--) { putchar(...

try

The keyword try is followed by a block, or by a constructor initializer list and then a block (see here). The try block is followed by one or more catch blocks. If an exception propagates out of the try block, each of the corresponding catch blocks after the try block has the opportunity to handle t...
if and else: it used to check whether the given expression returns true or false and acts as such: if (condition) statement the condition can be any valid C++ expression that returns something that be checked against truth/falsehood for example: if (true) { /* code here */ } // evaluate that ...
The break instruction: Using break we can leave 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 syntax is break; Example: we often use break in switch cases,ie once a case i switch is satisfied...

Page 1 of 1