From cbe7d19740eb18750ffc2e9671d34b7b47316bb4 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Sat, 5 Apr 2025 17:14:32 +0530 Subject: [PATCH 1/2] Update if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md --- ...lse--switch--goto@bjpFWxiCKGz28E-ukhZBp.md | 109 +++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md b/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md index 508114a8b..d74fc136d 100644 --- a/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md +++ b/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md @@ -1 +1,108 @@ -# if else / switch / goto \ No newline at end of file +# Control Flow Statements + +Control flow statements allow you to execute certain parts of your code if specific conditions are met. In C++, there are three main control flow statements: `if-else`, `switch`, and `goto`. + +## if-else +The `if-else` statement is a fundamental control flow statement that allows you to execute one of two blocks of code depending on whether a condition is satisfied. This statement is useful for complex comparisons. The syntax for the `if-else` statement is: + +```cpp +if (condition) { + // Block of code executed if the condition is true +} else { + // Block of code executed if the condition is false +} +``` + +For example: + +```cpp +#include +using namespace std; + +int main() { + int a = 10, b = 5; + if (a > b) { + cout << "a is greater than b"; + } else { + cout << "b is greater than a"; + } +} +``` + +## switch +The `switch` statement allows you to execute different blocks of code based on the value of a `control variable`. It uses `cases` to define code blocks that run when a specific value matches. A `default` case is executed if none of the other cases match. Each `case` must end with a `break` statement; otherwise, all following cases will execute. `Switch` is useful when you want to compare `constant values`. The syntax for the `switch` statement is: + +```cpp +switch (control_variable) { + case 1: { + // Code to be executed for case 1 + break; + } + case 2: { + // Code to be executed for case 2 + break; + } + default: { + // Code to be executed if no cases match + break; + } +} +``` + +For example: + +```cpp +#include +using namespace std; + +int main() { + int ch; + cout << "Select a language: 1. English 2. French"; + cin >> ch; + switch (ch) { + case 1: { + cout << "Good morning!"; + break; + } + case 2: { + cout << "Bonjour!"; + break; + } + default: { + cout << "The option does not exist :)"; + break; + } + } +} +``` + +## goto +The `goto` statement allows you to jump to different parts of your program using `labels` as location points. The syntax for `goto` is: + +```cpp +goto label_name; +``` + +For example: + +```cpp +#include +using namespace std; + +int main() { + cout << "This is line 1"; + goto line_3; + + cout << "This line will be skipped"; + + line_3: + cout << "This is line 3"; +} +``` + + +Learn more from the following resources: + +- [@article@C++ if else](https://www.w3schools.com/cpp/cpp_conditions.asp) +- [@article@C++ switch](https://www.w3schools.com/cpp/cpp_switch.asp) +- [@article@C++ goto](https://www.programiz.com/cpp-programming/goto) From 5704a7f78be86c3733c3bbb7a1e640ac90bfa9c5 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Sat, 5 Apr 2025 17:53:04 +0530 Subject: [PATCH 2/2] Update if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md Fixed terminologies --- ...lse--switch--goto@bjpFWxiCKGz28E-ukhZBp.md | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md b/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md index d74fc136d..c85d47b8e 100644 --- a/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md +++ b/src/data/roadmaps/cpp/content/if-else--switch--goto@bjpFWxiCKGz28E-ukhZBp.md @@ -1,9 +1,9 @@ -# Control Flow Statements +# Branching Statements -Control flow statements allow you to execute certain parts of your code if specific conditions are met. In C++, there are three main control flow statements: `if-else`, `switch`, and `goto`. +Branching statements allow you to conditionally or unconditionally execute different parts of your code. The branching statements that will be covered are `if-else`, `switch`, and `goto`. ## if-else -The `if-else` statement is a fundamental control flow statement that allows you to execute one of two blocks of code depending on whether a condition is satisfied. This statement is useful for complex comparisons. The syntax for the `if-else` statement is: +The `if-else` statement is a conditional branching statement that allows you to execute one of two blocks of code depending on whether a condition is satisfied. This statement is useful for making decisions based on a boolean expression. The syntax for the `if-else` statement is: ```cpp if (condition) { @@ -30,7 +30,7 @@ int main() { ``` ## switch -The `switch` statement allows you to execute different blocks of code based on the value of a `control variable`. It uses `cases` to define code blocks that run when a specific value matches. A `default` case is executed if none of the other cases match. Each `case` must end with a `break` statement; otherwise, all following cases will execute. `Switch` is useful when you want to compare `constant values`. The syntax for the `switch` statement is: +The `switch` statement allows you to execute different blocks of code based on the value of a `control variable`. It uses `cases` to define code blocks that run when a specific value matches. A `default` case is executed if none of the other cases match. Each `case` must end with a `break` statement; otherwise, all following cases will execute. `switch` is useful when you want to compare a `control variable` to multiple constant values. The syntax for the `switch` statement is: ```cpp switch (control_variable) { @@ -77,13 +77,13 @@ int main() { ``` ## goto -The `goto` statement allows you to jump to different parts of your program using `labels` as location points. The syntax for `goto` is: +The `goto` statement allows you to unconditionally jump to different parts of your program using `labels` as location points. The syntax for `goto` is: ```cpp goto label_name; ``` -For example: +For example: ```cpp #include @@ -93,16 +93,14 @@ int main() { cout << "This is line 1"; goto line_3; + // This line will be skipped cout << "This line will be skipped"; - line_3: - cout << "This is line 3"; +line_3: + cout << "This is line 3"; } ``` - -Learn more from the following resources: - - [@article@C++ if else](https://www.w3schools.com/cpp/cpp_conditions.asp) - [@article@C++ switch](https://www.w3schools.com/cpp/cpp_switch.asp) - [@article@C++ goto](https://www.programiz.com/cpp-programming/goto)