From 5d76ab904bc9da3746122192655f7db32851e19f Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Fri, 12 Sep 2025 20:08:53 +0200 Subject: [PATCH 1/7] Control Flow Article First partial draft --- wiki/cpp-tutorial/control-flow.md | 130 ++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index f41efc1..3971136 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -6,8 +6,138 @@ wip: true ## What is Control Flow? +Control flow is the order in which instructions are executed in a program. You can specify the control flow of a program +by using control flow statements. Without these statements program will execute every instruction one after another +(sequentially). + +The 3 main control flows are + +- Sequential +- Branching (Conditional) +- Looping (Iterative) + ## Branches +Branching in programming means executing different sets instructions (branches) based on some condition. This condition +is any expression that evaluates to a boolean. + +### If Statement + +If statement runs a block of code if the condition is `true`. + +```cpp +if(condition) { + // block of code executed if condition is true +} +``` + +This code updates a `price` of a product where only loyal customers are eligible for a 15% discount (normal customers +pay the regular price): + +```cpp +if(eligibleForDiscount) { + price *= 0.75; // apply discount of 15% +} +``` + +### If Statement With an Else Branch + +Else statement runs a block of code if condition is `false`. Else statement always goes after the if statement. + +```cpp +if(condition) { + // block of code executed if condition is true (if block) +} else { + // block of code executed if condition is false (else block) +} +``` + +Example of a code that takes user input and prints if the number is odd or even: + +```cpp +#include + +int main() { + int number; + std::cout << "Enter a number: "; + std::cin >> number; + + if(number % 2 == 0) { // if the remainder of division by 2 is equal to 0 + std::cout << "Number is even"; + } else { // number % 2 == 1 + std::cout << "Number is odd"; + } +} +``` + +Braces are optional. You don't have to use `{...}` when writing if-else statements but it is highly recommended. + +```cpp +if(condition) /* if block */; +else /* else block */; +``` + +Example of confusing indentation: + +```cpp +if(condition) + std::cout << "This expression executed only if condition is true."; + std::cout << "This expression is always executed."; +``` + +### Nested If Statements + +You can nest if statements. Most often you will see them nested using the `else if` expression. + +```cpp +if(condition_1) { + // 1st block + // condition_1 is true +} else if(condition_2) { + // 2nd block + // condition_1 is false + // condition_2 is true +} else { + // 3rd block + // condition_1 is false + // condition_2 is false +} +``` + +this is equivalent to: + +```cpp +if(condition_1) { + // 1st block +} else { + if(condition_2) { + // 2nd block + } else { + // 3rd block + } +} +``` + +Example of code that prints if a number is positive, negative or a zero: + +```cpp +#include + +int main() { + int number; + std::cout << "Enter a number: "; + std::cin >> number; + + if(number > 0) { + std::cout << "Positive"; + } else if(number < 0) { + std::cout << "Negative"; + } else { + std::cout << "Zero"; + } +} +``` + ## Looping ## Switches From c55bd80eef1f9b1d310a36637d75a3c8952e35bc Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Sun, 14 Sep 2025 23:42:43 +0200 Subject: [PATCH 2/7] Second partial draft --- wiki/cpp-tutorial/control-flow.md | 179 +++++++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 6 deletions(-) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index 3971136..21660e2 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -10,11 +10,12 @@ Control flow is the order in which instructions are executed in a program. You c by using control flow statements. Without these statements program will execute every instruction one after another (sequentially). -The 3 main control flows are - -- Sequential -- Branching (Conditional) -- Looping (Iterative) +| Type of control flow | Type of statement | +| ----------------------- | --------------------- | +| Sequential | (By default) | +| Jump | goto, break, continue | +| Branching (Conditional) | if-else, switch | +| Looping (Iterative) | for-loop, while-loop | ## Branches @@ -23,7 +24,8 @@ is any expression that evaluates to a boolean. ### If Statement -If statement runs a block of code if the condition is `true`. +If statement runs a block of code if the condition is `true` (execution jumps to the end of the if-statement if +condition is `false`). ```cpp if(condition) { @@ -140,6 +142,171 @@ int main() { ## Looping +Example of code that sums up user inputted numbers until user inputs `0`: + +```cpp +#include +int main() { + int num; + int sum = 0; + do { + std::cin >> num; + sum += num; + } while(num != 0); + std::cout << "The sum is: " << num; +} +``` + +### While Loop + +While loop keeps executing a block of code while condition is `true`. Condition gets checked at the start of the while +statement and jumps to the end if condition is `false`. + +```cpp +while(condition) { + // loop body +} +``` + +:::info While loop is equivalent to + +```cpp +LOOP_START: +{ + if (condition) { + // loop body + goto LOOP_START; + } +} +``` + +::: + +Example of printing numbers from 0 to 100 using a while loop: + +```cpp +#include + +int main() { + int n = 0; + while(n <= 100) { + std::cout << n << '\n'; + ++n; + } +} +``` + +Calculating a factorial of a number: + +```cpp +#include + +int main() { + int number; + std::cin >> number; + + int factorial = number; + while(--number) { + factorial *= number; + } + + std::cout << "The factorial of " << number << "is " << factorial; +} +``` + +### Do While Loop + +Do-while loop keeps executing a block of code while condition is `true`. Condition gets checked at the end of the +do-while statement and the execution jumps to the start if condition is `true`. + +```cpp +do { + // loop body (executed at least once) +} while(condition); +``` + +:::info Do-While loop is equivalent to: + +```cpp +LOOP_START: +{ + // loop body + if (condition) { + goto LOOP_START; + } +} +``` + +::: + +### For Loop + +For loop also keeps executing a block of code while condition is `true`. Init statement evaluates exactly once at the +start. Expression gets always evaluated after the loop body. + +```cpp +for(/* init-statement */; /* condition */; /* expression */) { + // loop body +} +``` + +:::info For loop is equivalent to: + +```cpp +{ + /* init-statement */ + while (condition) { + /* loop body */ + /* expression */ ; + } +} +``` + +::: + +This is a valid for loop that loops infinitely: + +```cpp +for(;;) { + // loop body +} +``` + +Example of printing numbers from 0 to 100 using a for loop: + +```cpp +#include + +int main() { + // declaration ; condition ; increment + for(int i = 0; i <= 100; ++i) { + std::cout << i << '\n'; + } +} +``` + +Example combining if-else statement and a for loop to play [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) game for +the first 100 numbers. + +```cpp +#include + +int main() { + for(int num = 1; num <= 100; ++num) { + if(num % 15 == 0) { + std::cout << "FizzBuzz"; + } else if(num % 3 == 0) { + std::cout << "Fizz"; + } else if(num % 5 == 0) { + std::cout << "Buzz"; + } else { + std::cout << num; + } + std::cout << '\n'; + } +} +``` + ## Switches ## Conditional Operator From 93c84732ec03e4ad3f6bc768232893efc37e7569 Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Wed, 17 Sep 2025 06:11:17 +0200 Subject: [PATCH 3/7] Update control-flow.md --- wiki/cpp-tutorial/control-flow.md | 209 ++++++++++++++++-------------- 1 file changed, 115 insertions(+), 94 deletions(-) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index 21660e2..c30be28 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -4,32 +4,35 @@ wip: true # Control Flow -## What is Control Flow? - Control flow is the order in which instructions are executed in a program. You can specify the control flow of a program by using control flow statements. Without these statements program will execute every instruction one after another (sequentially). -| Type of control flow | Type of statement | -| ----------------------- | --------------------- | -| Sequential | (By default) | -| Jump | goto, break, continue | -| Branching (Conditional) | if-else, switch | -| Looping (Iterative) | for-loop, while-loop | +| Type of control flow statements | Statements | +| ------------------------------- | --------------------- | +| Conditional | if-else, switch | +| Loop (Iteration) | for-loop, while-loop | +| Jump | goto, break, continue | + +:::info Block / Statement + +Block `{...}` groups a sequence of statements into a single statement, creating a block scope. + +::: -## Branches +## Conditional Statements -Branching in programming means executing different sets instructions (branches) based on some condition. This condition -is any expression that evaluates to a boolean. +Conditional statements (also known as branching statements or selection statements) allow you to execute different sets +instructions (branches of code) based on some condition. This condition is any expression that evaluates to a boolean +(`true` or `false`). ### If Statement -If statement runs a block of code if the condition is `true` (execution jumps to the end of the if-statement if -condition is `false`). +If statement executes a substatement if the condition is `true`. ```cpp if(condition) { - // block of code executed if condition is true + /* block executed if condition is true */ } ``` @@ -38,19 +41,19 @@ pay the regular price): ```cpp if(eligibleForDiscount) { - price *= 0.75; // apply discount of 15% + price *= 0.75; /* apply discount of 15% */ } ``` ### If Statement With an Else Branch -Else statement runs a block of code if condition is `false`. Else statement always goes after the if statement. +Else statement executes a substatement if condition is `false`. Else statement always goes after the if statement. ```cpp if(condition) { - // block of code executed if condition is true (if block) + /* block executed if condition is true */ } else { - // block of code executed if condition is false (else block) + /* block executed if condition is false */ } ``` @@ -64,19 +67,20 @@ int main() { std::cout << "Enter a number: "; std::cin >> number; - if(number % 2 == 0) { // if the remainder of division by 2 is equal to 0 + /* if the remainder of division by 2 is equal to 0 */ + if(number % 2 == 0) { std::cout << "Number is even"; - } else { // number % 2 == 1 + } else { /* number % 2 == 1 */ std::cout << "Number is odd"; } } ``` -Braces are optional. You don't have to use `{...}` when writing if-else statements but it is highly recommended. +Braces are optional. You don't have to use `{...}` (block) when writing if-else statements but it is highly recommended. ```cpp -if(condition) /* if block */; -else /* else block */; +if(condition) /* if statement */; +else /* else statement */; ``` Example of confusing indentation: @@ -93,16 +97,16 @@ You can nest if statements. Most often you will see them nested using the `else ```cpp if(condition_1) { - // 1st block - // condition_1 is true + /* 1st block */ + /* condition_1 is true */ } else if(condition_2) { - // 2nd block - // condition_1 is false - // condition_2 is true + /* 2nd block */ + /* condition_1 is false */ + /* condition_2 is true */ } else { - // 3rd block - // condition_1 is false - // condition_2 is false + /* 3rd block */ + /* condition_1 is false */ + /* condition_2 is false */ } ``` @@ -110,12 +114,12 @@ this is equivalent to: ```cpp if(condition_1) { - // 1st block + /* 1st block */ } else { if(condition_2) { - // 2nd block + /* 2nd block */ } else { - // 3rd block + /* 3rd block */ } } ``` @@ -140,135 +144,145 @@ int main() { } ``` -## Looping +## Loop Statements -Example of code that sums up user inputted numbers until user inputs `0`: - -```cpp -#include -int main() { - int num; - int sum = 0; - do { - std::cin >> num; - sum += num; - } while(num != 0); - std::cout << "The sum is: " << num; -} -``` +The loop statements allow you to execute the same instructions multiple times. ### While Loop -While loop keeps executing a block of code while condition is `true`. Condition gets checked at the start of the while -statement and jumps to the end if condition is `false`. +While loop keeps executing a statement while condition is `true`. ```cpp while(condition) { - // loop body + /* block */ } ``` -:::info While loop is equivalent to +Example of printing numbers from 0 to 100 using a while loop: ```cpp -LOOP_START: -{ - if (condition) { - // loop body - goto LOOP_START; +#include + +int main() { + int n = 0; + while(n <= 100) { + std::cout << n << '\n'; + ++n; } } ``` -::: - -Example of printing numbers from 0 to 100 using a while loop: +Calculating factorial of a number: ```cpp #include int main() { - int n = 0; - while(n <= 100) { - std::cout << n << '\n'; - ++n; + int fac = 1, num; + std::cout << "Enter a number: "; + std::cin >> num; + + while(num) { + fac *= num; + --num; } + + std::cout << "The factorial of the number is: " << b; } ``` -Calculating a factorial of a number: +Calculating nth Fibonacci number: ```cpp #include int main() { - int number; - std::cin >> number; + int a = 0, b = 1, n; + std::cout << "Enter a number: "; + std::cin >> n; - int factorial = number; - while(--number) { - factorial *= number; + /* F(n + 2) = F(n + 1) + F(n) */ + while(--n) { + int swap = b; + b += a; + a = swap; } - std::cout << "The factorial of " << number << "is " << factorial; + std::cout << "The nth Fibonacci number is: " << b; } ``` ### Do While Loop -Do-while loop keeps executing a block of code while condition is `true`. Condition gets checked at the end of the -do-while statement and the execution jumps to the start if condition is `true`. +Do-while loop keeps executing a substatement while condition is `true`. Do-while loop is similar to while loop but it +guarantees that the substatement gets executed at least once. ```cpp do { - // loop body (executed at least once) + // block (executed at least once) } while(condition); ``` -:::info Do-While loop is equivalent to: +Example of code that prints out digits of of a number: ```cpp -LOOP_START: -{ - // loop body - if (condition) { - goto LOOP_START; - } +#include + +int main() { + int num; + std::cout << "Enter a number: "; + std::cin >> num; + + do { + std::cout << num % 10 << '\n'; + num /= 10; + } while(num != 0); } ``` -::: +Example of code that sums up user inputted numbers until user inputs `0`: + +```cpp +#include +int main() { + int num; + int sum = 0; + do { + std::cin >> num; + sum += num; + } while(num != 0); + std::cout << "The sum is: " << num; +} +``` ### For Loop For loop also keeps executing a block of code while condition is `true`. Init statement evaluates exactly once at the -start. Expression gets always evaluated after the loop body. +start. Expression gets always evaluated after the statement. ```cpp for(/* init-statement */; /* condition */; /* expression */) { - // loop body + /* block */ } ``` -:::info For loop is equivalent to: +For loop is equivalent to: ```cpp { /* init-statement */ while (condition) { - /* loop body */ + /* statement */ /* expression */ ; } } ``` -::: - This is a valid for loop that loops infinitely: ```cpp for(;;) { - // loop body + /* block */ } ``` @@ -278,15 +292,14 @@ Example of printing numbers from 0 to 100 using a for loop: #include int main() { - // declaration ; condition ; increment for(int i = 0; i <= 100; ++i) { std::cout << i << '\n'; } } ``` -Example combining if-else statement and a for loop to play [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) game for -the first 100 numbers. +Example combining if-else statement and a for loop to play a word game called +[FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) for the first 100 numbers. ```cpp #include @@ -307,6 +320,14 @@ int main() { } ``` -## Switches +## Jump statements + +### Break Statement + +### Continue Statement + +### Goto Statement + +## Switch Statements ## Conditional Operator From fc3f04e172f1dcc12eb689aac71ad24394ecf9b5 Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Mon, 22 Sep 2025 03:26:19 +0200 Subject: [PATCH 4/7] First working draft --- wiki/cpp-tutorial/control-flow.md | 165 +++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 5 deletions(-) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index c30be28..26af8e9 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -14,7 +14,7 @@ by using control flow statements. Without these statements program will execute | Loop (Iteration) | for-loop, while-loop | | Jump | goto, break, continue | -:::info Block / Statement +:::info Block (Compound Statement) Block `{...}` groups a sequence of statements into a single statement, creating a block scope. @@ -151,10 +151,11 @@ The loop statements allow you to execute the same instructions multiple times. ### While Loop While loop keeps executing a statement while condition is `true`. +Condition gets re-evaluated every iteration (this is true for every type of the loop statement). ```cpp while(condition) { - /* block */ + /* block (loop body) */ } ``` @@ -219,7 +220,7 @@ guarantees that the substatement gets executed at least once. ```cpp do { - // block (executed at least once) + // block (loop body) executed at least once } while(condition); ``` @@ -262,7 +263,7 @@ start. Expression gets always evaluated after the statement. ```cpp for(/* init-statement */; /* condition */; /* expression */) { - /* block */ + /* block (loop body) */ } ``` @@ -282,7 +283,7 @@ This is a valid for loop that loops infinitely: ```cpp for(;;) { - /* block */ + /* block (loop body) */ } ``` @@ -324,10 +325,164 @@ int main() { ### Break Statement +The break statement allows you to stop a loop early. +Here is an example of a simple guessing game: + +```cpp +#include + +int main() { + int number = 1234; + int guess; + for(;;) { // infinite loop + std::cout << "Guess a number: "; + std::cin >> guess; + if(guess < number) { + std::cout << "Too low!\n"; + } else if (guess > number) { + std::cout << "Too high!\n"; + } else { + std::cout << "You guessed correctly!!!"; + break; + } + } +} +``` + ### Continue Statement +The continue statement allows you to skip the rest of the body. + +```cpp +#include + +int main() { + for(int i = 0; i < 100; ++i) { + if(i % 2 == 0) continue; + std::cout << i << '\n'; + } +} +``` + ### Goto Statement +Goto statement allows you to jump to a defined label. +You can only jump to a label within the same function. +Goto statements make code less readable and you should avoid using them. +Here is a code equivalent of a while loop: + +```cpp +LOOP_LABEL: +if(condition) { + /* statement */ + goto LOOP_LABEL; +} +``` + +Jumping into an if statement: +```cpp +goto LABEL; +if(false) { + LABEL: + std::cout << "This code gets executed!"; +} else { + std::cout << "This one doesn't!"; +} +``` + +Jump also cannot bypass variable initialization. +This code is invalid and compiler should give you a compilation error: + +```cpp +int main() { + goto LABEL; + int x = 0; + LABEL: + ++x; +} +``` + ## Switch Statements +Switch statements are very similar to if else statements. +Condition gets evaluated once and execution jumps to the appropriate label. +Break statement allows you to jump to the end of the switch statement. +There are two labels `case` and `default`. +Case label needs to be followed by a constant expression. +There can only be one default label. +If none of the cases apply execution jumps to the end of the switch statement or the default label if specified. + +```cpp +switch (condition) +{ +case 1: + std::cout << "One"; + break; +case 2: + std::cout << "Two"; + [[fallthrough]]; /* Attribute (optional - disables warnings) */ +case 3: + std::cout << "Three"; + break; +} +default: + std::cout << "Too big!" +``` + +Example of a minimal calculator that takes in two numbers separated by one of `+-*/` operators. + +```cpp +#include +#include // C++23 feature + +int main() { + int x, y; + char op; + std::cout << "Enter number operator number"; + std::cin >> x >> op >> y; + + switch(op) { + case '+': + std::println("Sum: {} + {} = {}", x, y, x + y); + break; + case '-': + std::println("Difference: {} - {} = {}", x, y, x - y); + break; + case '*': + std::println("Product: {} * {} = {}", x, y, x * y); + break; + case '/': + std::println("Quotient (rounded down): {} / {} = {}", x, y, x / y); + break; + default: + std::println("Invalid operator!!!"); + } +} +``` + ## Conditional Operator + +Conditional operators are ternary operators (operators taking in 3 operands). +If second and third operands are of the same type, the result is of that type. + +```cpp +(condition) ? /* condition true */ : /* condition false */; +``` + +Here is an example of conditional operator usages: + +```cpp +int main() { + int x, y; + std::cin >> x >> y; + int max = (x > y) ? x : y; + std::cout << "The maximum is: " << max << '\n'; + std::cout << "The ordering is: " << ((x == y) ? "Equal to" : (x < y) ? "Less than" : "Greater than"); +} +``` + +::: info + +Stream insertion operator has precedence `<<` over the ternary operator `?:` + +::: From d6dba9fa12b4e9fc6c41f8c3f48d7e62d250d006 Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Mon, 22 Sep 2025 03:34:28 +0200 Subject: [PATCH 5/7] Formatting issues --- wiki/cpp-tutorial/control-flow.md | 32 +++++++++++++------------------ 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index 26af8e9..d2aa984 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -150,8 +150,8 @@ The loop statements allow you to execute the same instructions multiple times. ### While Loop -While loop keeps executing a statement while condition is `true`. -Condition gets re-evaluated every iteration (this is true for every type of the loop statement). +While loop keeps executing a statement while condition is `true`. Condition gets re-evaluated every iteration (this is +true for every type of the loop statement). ```cpp while(condition) { @@ -325,8 +325,7 @@ int main() { ### Break Statement -The break statement allows you to stop a loop early. -Here is an example of a simple guessing game: +The break statement allows you to stop a loop early. Here is an example of a simple guessing game: ```cpp #include @@ -366,10 +365,8 @@ int main() { ### Goto Statement -Goto statement allows you to jump to a defined label. -You can only jump to a label within the same function. -Goto statements make code less readable and you should avoid using them. -Here is a code equivalent of a while loop: +Goto statement allows you to jump to a defined label. You can only jump to a label within the same function. Goto +statements make code less readable and you should avoid using them. Here is a code equivalent of a while loop: ```cpp LOOP_LABEL: @@ -380,6 +377,7 @@ if(condition) { ``` Jumping into an if statement: + ```cpp goto LABEL; if(false) { @@ -390,8 +388,7 @@ if(false) { } ``` -Jump also cannot bypass variable initialization. -This code is invalid and compiler should give you a compilation error: +Jump also cannot bypass variable initialization. This code is invalid and compiler should give you a compilation error: ```cpp int main() { @@ -404,13 +401,10 @@ int main() { ## Switch Statements -Switch statements are very similar to if else statements. -Condition gets evaluated once and execution jumps to the appropriate label. -Break statement allows you to jump to the end of the switch statement. -There are two labels `case` and `default`. -Case label needs to be followed by a constant expression. -There can only be one default label. -If none of the cases apply execution jumps to the end of the switch statement or the default label if specified. +Switch statements are very similar to if else statements. Condition gets evaluated once and execution jumps to the +appropriate label. Break statement allows you to jump to the end of the switch statement. There are two labels `case` +and `default`. Case label needs to be followed by a constant expression. There can only be one default label. If none of +the cases apply execution jumps to the end of the switch statement or the default label if specified. ```cpp switch (condition) @@ -462,8 +456,8 @@ int main() { ## Conditional Operator -Conditional operators are ternary operators (operators taking in 3 operands). -If second and third operands are of the same type, the result is of that type. +Conditional operators are ternary operators (operators taking in 3 operands). If second and third operands are of the +same type, the result is of that type. ```cpp (condition) ? /* condition true */ : /* condition false */; From 86becc9f5f38ddf414190f3624d903d59f079f98 Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Thu, 2 Oct 2025 22:25:54 +0200 Subject: [PATCH 6/7] Update control-flow.md --- wiki/cpp-tutorial/control-flow.md | 70 +++++++++++++++---------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index d2aa984..69d28f5 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -1,7 +1,3 @@ ---- -wip: true ---- - # Control Flow Control flow is the order in which instructions are executed in a program. You can specify the control flow of a program @@ -23,12 +19,12 @@ Block `{...}` groups a sequence of statements into a single statement, creating ## Conditional Statements Conditional statements (also known as branching statements or selection statements) allow you to execute different sets -instructions (branches of code) based on some condition. This condition is any expression that evaluates to a boolean +of instructions (branches of code) based on some condition. This condition is any expression that evaluates to a boolean (`true` or `false`). -### If Statement +### If-Statement -If statement executes a substatement if the condition is `true`. +If-statement executes a substatement if the condition is `true`. ```cpp if(condition) { @@ -45,9 +41,9 @@ if(eligibleForDiscount) { } ``` -### If Statement With an Else Branch +### If-Statement With an Else Branch -Else statement executes a substatement if condition is `false`. Else statement always goes after the if statement. +Else statement executes a substatement if condition is `false`. Else statement always goes after the if-statement. ```cpp if(condition) { @@ -79,7 +75,7 @@ int main() { Braces are optional. You don't have to use `{...}` (block) when writing if-else statements but it is highly recommended. ```cpp -if(condition) /* if statement */; +if(condition) /* if-statement */; else /* else statement */; ``` @@ -87,13 +83,13 @@ Example of confusing indentation: ```cpp if(condition) - std::cout << "This expression executed only if condition is true."; + std::cout << "This expression is executed only if the condition is true."; std::cout << "This expression is always executed."; ``` -### Nested If Statements +### Nested If-Statements -You can nest if statements. Most often you will see them nested using the `else if` expression. +You can nest if-statements. Most often you will see them nested using the `else if` expression. ```cpp if(condition_1) { @@ -110,7 +106,7 @@ if(condition_1) { } ``` -this is equivalent to: +this code is equivalent to: ```cpp if(condition_1) { @@ -148,9 +144,9 @@ int main() { The loop statements allow you to execute the same instructions multiple times. -### While Loop +### while-loop -While loop keeps executing a statement while condition is `true`. Condition gets re-evaluated every iteration (this is +while-loop keeps executing a statement while condition is `true`. Condition gets re-evaluated every iteration (this is true for every type of the loop statement). ```cpp @@ -159,7 +155,7 @@ while(condition) { } ``` -Example of printing numbers from 0 to 100 using a while loop: +Example of printing numbers from 0 to 100 using a while-loop: ```cpp #include @@ -213,9 +209,9 @@ int main() { } ``` -### Do While Loop +### Do while-loop -Do-while loop keeps executing a substatement while condition is `true`. Do-while loop is similar to while loop but it +Do-while-loop keeps executing a substatement while condition is `true`. Do-while-loop is similar to while-loop but it guarantees that the substatement gets executed at least once. ```cpp @@ -256,9 +252,9 @@ int main() { } ``` -### For Loop +### For-Loop -For loop also keeps executing a block of code while condition is `true`. Init statement evaluates exactly once at the +For-loop also keeps executing a block of code while condition is `true`. Init statement evaluates exactly once at the start. Expression gets always evaluated after the statement. ```cpp @@ -267,7 +263,7 @@ for(/* init-statement */; /* condition */; /* expression */) { } ``` -For loop is equivalent to: +For-loop is equivalent to: ```cpp { @@ -279,7 +275,7 @@ For loop is equivalent to: } ``` -This is a valid for loop that loops infinitely: +This is a valid for-loop that loops infinitely: ```cpp for(;;) { @@ -287,7 +283,7 @@ for(;;) { } ``` -Example of printing numbers from 0 to 100 using a for loop: +Example of printing numbers from 0 to 100 using a for-loop: ```cpp #include @@ -299,7 +295,7 @@ int main() { } ``` -Example combining if-else statement and a for loop to play a word game called +Example combining if-else statement and a for-loop to play a word game called [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) for the first 100 numbers. ```cpp @@ -307,7 +303,7 @@ Example combining if-else statement and a for loop to play a word game called int main() { for(int num = 1; num <= 100; ++num) { - if(num % 15 == 0) { + if(num % 3 == 0 && num % 5 == 0) { std::cout << "FizzBuzz"; } else if(num % 3 == 0) { std::cout << "Fizz"; @@ -365,8 +361,8 @@ int main() { ### Goto Statement -Goto statement allows you to jump to a defined label. You can only jump to a label within the same function. Goto -statements make code less readable and you should avoid using them. Here is a code equivalent of a while loop: +Goto statement allows you to jump to a defined label. You can only jump to a label within the same function. Here is a +code equivalent of a while-loop: ```cpp LOOP_LABEL: @@ -376,7 +372,7 @@ if(condition) { } ``` -Jumping into an if statement: +Jumping into an if-statement: ```cpp goto LABEL; @@ -399,16 +395,18 @@ int main() { } ``` +:::info Goto statements are useful. However they make the code less readable and it requires additional knowledge to use +them properly so it might be a good idea to avoid them for now. ::: + ## Switch Statements -Switch statements are very similar to if else statements. Condition gets evaluated once and execution jumps to the +Switch statements are very similar to if-else statements. Condition gets evaluated once and execution jumps to the appropriate label. Break statement allows you to jump to the end of the switch statement. There are two labels `case` and `default`. Case label needs to be followed by a constant expression. There can only be one default label. If none of the cases apply execution jumps to the end of the switch statement or the default label if specified. ```cpp -switch (condition) -{ +switch (condition) { case 1: std::cout << "One"; break; @@ -418,9 +416,9 @@ case 2: case 3: std::cout << "Three"; break; -} default: - std::cout << "Too big!" + std::cout << "Default" +} ``` Example of a minimal calculator that takes in two numbers separated by one of `+-*/` operators. @@ -477,6 +475,8 @@ int main() { ::: info -Stream insertion operator has precedence `<<` over the ternary operator `?:` +Stream insertion operator has precedence `<<` over the ternary operator `?:`. See +[article](https://en.cppreference.com/w/cpp/language/operator_precedence.html) for more information about operator +precedence. ::: From 12c88b3257d021fdabb85dd85ed31e018b4b70ab Mon Sep 17 00:00:00 2001 From: ProfessionalMenace Date: Thu, 9 Oct 2025 23:58:12 +0200 Subject: [PATCH 7/7] Editorial updates --- wiki/cpp-tutorial/control-flow.md | 91 +++++++++++++++---------------- 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/wiki/cpp-tutorial/control-flow.md b/wiki/cpp-tutorial/control-flow.md index 69d28f5..fe56471 100644 --- a/wiki/cpp-tutorial/control-flow.md +++ b/wiki/cpp-tutorial/control-flow.md @@ -10,7 +10,7 @@ by using control flow statements. Without these statements program will execute | Loop (Iteration) | for-loop, while-loop | | Jump | goto, break, continue | -:::info Block (Compound Statement) +:::info Block Statement (Compound Statement) Block `{...}` groups a sequence of statements into a single statement, creating a block scope. @@ -53,7 +53,7 @@ if(condition) { } ``` -Example of a code that takes user input and prints if the number is odd or even: +Example: Implementation of a program that takes user input and prints if the number is odd or even: ```cpp #include @@ -72,11 +72,12 @@ int main() { } ``` -Braces are optional. You don't have to use `{...}` (block) when writing if-else statements but it is highly recommended. +Braces are optional. You don't have to use `{...}` (block) when writing if-else statements but it is highly recommended +(for better code readability). ```cpp if(condition) /* if-statement */; -else /* else statement */; +else /* else-statement */; ``` Example of confusing indentation: @@ -120,7 +121,8 @@ if(condition_1) { } ``` -Example of code that prints if a number is positive, negative or a zero: +Example: Implementation of a program that prompts user to enter a number, takes user input (number), prints if a number +is positive, negative or a zero: ```cpp #include @@ -142,7 +144,10 @@ int main() { ## Loop Statements -The loop statements allow you to execute the same instructions multiple times. +The loop statements allow you to execute the same instructions multiple times (while condition is true). + +::info Loop Body The body of a loop is a common way of referring to a block statement `{...}` that executed by the loop. +:: ### while-loop @@ -151,11 +156,11 @@ true for every type of the loop statement). ```cpp while(condition) { - /* block (loop body) */ + /* loop body */ } ``` -Example of printing numbers from 0 to 100 using a while-loop: +Implementation of a program printing numbers from 0 to 100 using a while-loop: ```cpp #include @@ -169,7 +174,7 @@ int main() { } ``` -Calculating factorial of a number: +Implementation of a program calculating factorial of a number: ```cpp #include @@ -188,7 +193,7 @@ int main() { } ``` -Calculating nth Fibonacci number: +Implementation of a program calculating nth [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_sequence): ```cpp #include @@ -211,16 +216,16 @@ int main() { ### Do while-loop -Do-while-loop keeps executing a substatement while condition is `true`. Do-while-loop is similar to while-loop but it -guarantees that the substatement gets executed at least once. +Do-while-loop keeps executing a loop body while condition is `true`. Do-while-loop is similar to while-loop but it +guarantees that the loop body gets executed at least once. ```cpp do { - // block (loop body) executed at least once + // loop body executed at least once } while(condition); ``` -Example of code that prints out digits of of a number: +Implementation of a program that prints out digits of of a number: ```cpp #include @@ -237,7 +242,7 @@ int main() { } ``` -Example of code that sums up user inputted numbers until user inputs `0`: +Implementation of a program that sums up user inputted numbers until user inputs `0`: ```cpp #include @@ -254,16 +259,16 @@ int main() { ### For-Loop -For-loop also keeps executing a block of code while condition is `true`. Init statement evaluates exactly once at the -start. Expression gets always evaluated after the statement. +For-loop also keeps executing the loop body while condition is `true`. Init statement evaluates exactly once at the +start. Expression gets always evaluated after loop body. ```cpp for(/* init-statement */; /* condition */; /* expression */) { - /* block (loop body) */ + /* loop body */ } ``` -For-loop is equivalent to: +For-loop is a nicer way of writing a while-loop and is equivalent to this code: ```cpp { @@ -275,15 +280,15 @@ For-loop is equivalent to: } ``` -This is a valid for-loop that loops infinitely: +This is a valid for-loop that loops indefinitely: ```cpp for(;;) { - /* block (loop body) */ + /* loop body */ } ``` -Example of printing numbers from 0 to 100 using a for-loop: +Implementation of a program printing numbers from 0 to 100 using a for-loop: ```cpp #include @@ -295,8 +300,8 @@ int main() { } ``` -Example combining if-else statement and a for-loop to play a word game called -[FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) for the first 100 numbers. +Implementation of a program combining if-else statement and a for-loop to play a word game called +[FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) for the first 100 numbers (1 to 100). ```cpp #include @@ -321,7 +326,8 @@ int main() { ### Break Statement -The break statement allows you to stop a loop early. Here is an example of a simple guessing game: +The break statement allows you to stop enclosing loop early. Here is an example of an implementation of a simple +guessing game: ```cpp #include @@ -346,7 +352,7 @@ int main() { ### Continue Statement -The continue statement allows you to skip the rest of the body. +The continue statement allows you to skip the rest of the loop body. ```cpp #include @@ -372,19 +378,7 @@ if(condition) { } ``` -Jumping into an if-statement: - -```cpp -goto LABEL; -if(false) { - LABEL: - std::cout << "This code gets executed!"; -} else { - std::cout << "This one doesn't!"; -} -``` - -Jump also cannot bypass variable initialization. This code is invalid and compiler should give you a compilation error: +Jump cannot bypass variable initialization. This code is invalid and compiler should give you compilation error: ```cpp int main() { @@ -395,15 +389,20 @@ int main() { } ``` -:::info Goto statements are useful. However they make the code less readable and it requires additional knowledge to use -them properly so it might be a good idea to avoid them for now. ::: +:::info + +Goto statements are useful. However they make the code less readable and it requires additional knowledge to use +them properly so it might be a good idea to avoid them for now. + +::: ## Switch Statements Switch statements are very similar to if-else statements. Condition gets evaluated once and execution jumps to the -appropriate label. Break statement allows you to jump to the end of the switch statement. There are two labels `case` -and `default`. Case label needs to be followed by a constant expression. There can only be one default label. If none of -the cases apply execution jumps to the end of the switch statement or the default label if specified. +appropriate label. Break statement allows you to jump to the end of the switch statement. There are two labels you can +define `case` and `default`. Case label needs to be followed by a constant expression (this restriction allows switch +statements to be optimized by the compiler). There can only be one default label. If none of the cases apply execution +jumps to the end of the switch statement or the default label if specified. ```cpp switch (condition) { @@ -412,7 +411,7 @@ case 1: break; case 2: std::cout << "Two"; - [[fallthrough]]; /* Attribute (optional - disables warnings) */ + [[fallthrough]]; /* Attribute (optional - disables warnings on fallthrough) */ case 3: std::cout << "Three"; break; @@ -421,7 +420,7 @@ default: } ``` -Example of a minimal calculator that takes in two numbers separated by one of `+-*/` operators. +Implementation of a minimal calculator that takes in two numbers separated by one of `+-*/` operators. ```cpp #include