From 0cb0b5c3b847d02eae923fe9fca78dae1bc806d8 Mon Sep 17 00:00:00 2001 From: Arnab Sarkar Date: Thu, 3 Apr 2025 12:45:41 +0530 Subject: [PATCH 1/4] added content to if-else/switch/goto under control flow & statements inside c++ roadmap --- ...lse--switch--goto@bjpFWxiCKGz28E-ukhZBp.md | 99 ++++++++++++++++++- 1 file changed, 98 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..53fd921fe 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,98 @@ -# if else / switch / goto \ No newline at end of file +# If-Else / Switch / Goto + +## If-Else Statement + +The `if-else` statement allows conditional execution based on boolean expressions. + +```cpp +#include + +int main() { + int num = 10; + + // Check if the number is positive, negative, or zero + if (num > 0) { + std::cout << "Number is positive" << std::endl; + } else if (num < 0) { + std::cout << "Number is negative" << std::endl; + } else { + std::cout << "Number is zero" << std::endl; + } + return 0; +} +``` + +### Explanation: +- If `num` is greater than zero, it prints "Number is positive." +- If `num` is less than zero, it prints "Number is negative." +- Otherwise, it prints "Number is zero." + +--- + +## Switch Statement + +The `switch` statement is used when multiple conditions depend on a single variable. + +```cpp +#include + +int main() { + int choice = 2; + + // Evaluating different cases based on the value of 'choice' + switch (choice) { + case 1: + std::cout << "You chose option 1" << std::endl; + break; // Exit switch after executing this case + case 2: + std::cout << "You chose option 2" << std::endl; + break; + case 3: + std::cout << "You chose option 3" << std::endl; + break; + default: + std::cout << "Invalid choice" << std::endl; + } + return 0; +} +``` + +### Explanation: +- Depending on the value of `choice`, a corresponding message is printed. +- The `break` statement ensures that the control exits after the matched case. +- If no cases match, the `default` case executes. + +--- + +## Goto Statement + +The `goto` statement allows jumping to a labeled statement within a function. + +```cpp +#include + +int main() { + int num = 5; + + // If condition is met, jump to 'label' + if (num < 10) + goto label; + + std::cout << "This will be skipped" << std::endl; + + label: + std::cout << "Jumped to label" << std::endl; + + return 0; +} +``` + +### Explanation: +- If `num` is less than 10, execution jumps to `label`. +- The statement between `if` and `label:` is skipped. + +Visit the following resources to learn more: + +- [@video@if-else/The 'if-else' Statement in C++](https://www.youtube.com/watch?v=9-BjXs1vMSc) +- [@video@switch/Learn C++ With Me - Switch Statement](https://www.youtube.com/watch?v=uOlLs1OYSSI) +- [@video@goto/C++ Tutorial for Beginners - Break, Continue, and Goto](https://www.youtube.com/watch?v=ikGk4ZZ-uYE&t=14s) \ No newline at end of file From 53057fc1358fb27ee1ad32bb3362e71585f9129e Mon Sep 17 00:00:00 2001 From: Arnab Sarkar Date: Wed, 9 Apr 2025 14:21:15 +0530 Subject: [PATCH 2/4] added content to if-else/switch/goto under control flow & statements inside c++ roadmap --- ...lse--switch--goto@bjpFWxiCKGz28E-ukhZBp.md | 122 +++++++++--------- 1 file changed, 62 insertions(+), 60 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 53fd921fe..14abd5785 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,98 +1,100 @@ # If-Else / Switch / Goto -## If-Else Statement +C++ provides you with tools which helps you to control the way your program behaves (logic flows) based on how the user interact with your program. Here we will discuss about `if-else`, `switch` and `goto` are three common ways to guide the flow of logic in your code. -The `if-else` statement allows conditional execution based on boolean expressions. +## if-else -```cpp -#include +Used to execute a block of code only if a certain condition is met -int main() { - int num = 10; - - // Check if the number is positive, negative, or zero - if (num > 0) { - std::cout << "Number is positive" << std::endl; - } else if (num < 0) { - std::cout << "Number is negative" << std::endl; - } else { - std::cout << "Number is zero" << std::endl; - } - return 0; +**syntax** + +```cpp +if(/*condition*/){ +//block of code +} +// else is executed when if Statement is false adding else with if is optional +else{ +//block of code } ``` -### Explanation: -- If `num` is greater than zero, it prints "Number is positive." -- If `num` is less than zero, it prints "Number is negative." -- Otherwise, it prints "Number is zero." - ---- +if you want to check multiple conditions in a particular sequence you can use `else if` like this for example -## Switch Statement +```cpp +#include +int main (){ + std::cout << "Enter a number: "; + std::cin >> number; + + if (number > 0) { + std::cout << "The number is positive." << std::endl; + } else if (number < 0) { + std::cout << "The number is negative." << std::endl; + } else { + std::cout << "The number is zero." << std::endl; + } + + return 0; +} +``` -The `switch` statement is used when multiple conditions depend on a single variable. +## switch: +When we need to compare a single variable against multiple constant values, a switch statement can be used instead of multiple `if-else` blocks. +**syntax** ```cpp #include int main() { - int choice = 2; + int choice; + + std::cout << "Enter a number (1-3): "; + std::cin >> choice; - // Evaluating different cases based on the value of 'choice' switch (choice) { case 1: - std::cout << "You chose option 1" << std::endl; - break; // Exit switch after executing this case + std::cout << "You chose One." << std::endl; + break; case 2: - std::cout << "You chose option 2" << std::endl; + std::cout << "You chose Two." << std::endl; break; case 3: - std::cout << "You chose option 3" << std::endl; + std::cout << "You chose Three." << std::endl; break; default: - std::cout << "Invalid choice" << std::endl; + std::cout << "Invalid choice." << std::endl; } + return 0; } ``` +## Goto +`goto` is a low-level control flow feature that allows unconditional jumps to a labeled part of the code. While it can be useful in specific scenarios, especially for breaking out of deeply nested loops or handling errors in C, it often leads to confusing and hard-to-maintain code if not used carefully. -### Explanation: -- Depending on the value of `choice`, a corresponding message is printed. -- The `break` statement ensures that the control exits after the matched case. -- If no cases match, the `default` case executes. +**syntax** +```cpp +#include ---- +int main() { + std::cout << "Before goto\n"; -## Goto Statement + goto skip; -The `goto` statement allows jumping to a labeled statement within a function. + std::cout << "This will be skipped\n"; -```cpp -#include +skip: + std::cout << "After label\n"; -int main() { - int num = 5; - - // If condition is met, jump to 'label' - if (num < 10) - goto label; - - std::cout << "This will be skipped" << std::endl; - - label: - std::cout << "Jumped to label" << std::endl; - return 0; } +/* + Output: + Before goto + After label +*/ ``` - -### Explanation: -- If `num` is less than 10, execution jumps to `label`. -- The statement between `if` and `label:` is skipped. - Visit the following resources to learn more: -- [@video@if-else/The 'if-else' Statement in C++](https://www.youtube.com/watch?v=9-BjXs1vMSc) -- [@video@switch/Learn C++ With Me - Switch Statement](https://www.youtube.com/watch?v=uOlLs1OYSSI) -- [@video@goto/C++ Tutorial for Beginners - Break, Continue, and Goto](https://www.youtube.com/watch?v=ikGk4ZZ-uYE&t=14s) \ No newline at end of file +- [@video@The 'if-else' Statement in C++](https://www.youtube.com/watch?v=9-BjXs1vMSc) +- [@video@Learn C++ With Me - Switch Statement](https://www.youtube.com/watch?v=uOlLs1OYSSI) +- [@video@ why is it illegal to use "goto"?](https://youtu.be/AKJhThyTmQw?si=gjEqAsDZVMDGVAT2) \ No newline at end of file From bb0f96cf26fba1289927671619eb8bf8a3d89be3 Mon Sep 17 00:00:00 2001 From: Arnab Sarkar Date: Wed, 9 Apr 2025 16:52:05 +0530 Subject: [PATCH 3/4] added content to control flow in cpp roadmap fixed format error --- ...lse--switch--goto@bjpFWxiCKGz28E-ukhZBp.md | 93 +------------------ 1 file changed, 1 insertion(+), 92 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 14abd5785..6fee36203 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 @@ -2,99 +2,8 @@ C++ provides you with tools which helps you to control the way your program behaves (logic flows) based on how the user interact with your program. Here we will discuss about `if-else`, `switch` and `goto` are three common ways to guide the flow of logic in your code. -## if-else - -Used to execute a block of code only if a certain condition is met - -**syntax** - -```cpp -if(/*condition*/){ -//block of code -} -// else is executed when if Statement is false adding else with if is optional -else{ -//block of code -} -``` - -if you want to check multiple conditions in a particular sequence you can use `else if` like this for example - -```cpp -#include -int main (){ - std::cout << "Enter a number: "; - std::cin >> number; - - if (number > 0) { - std::cout << "The number is positive." << std::endl; - } else if (number < 0) { - std::cout << "The number is negative." << std::endl; - } else { - std::cout << "The number is zero." << std::endl; - } - - return 0; -} -``` - -## switch: -When we need to compare a single variable against multiple constant values, a switch statement can be used instead of multiple `if-else` blocks. - -**syntax** -```cpp -#include - -int main() { - int choice; - - std::cout << "Enter a number (1-3): "; - std::cin >> choice; - - switch (choice) { - case 1: - std::cout << "You chose One." << std::endl; - break; - case 2: - std::cout << "You chose Two." << std::endl; - break; - case 3: - std::cout << "You chose Three." << std::endl; - break; - default: - std::cout << "Invalid choice." << std::endl; - } - - return 0; -} -``` -## Goto -`goto` is a low-level control flow feature that allows unconditional jumps to a labeled part of the code. While it can be useful in specific scenarios, especially for breaking out of deeply nested loops or handling errors in C, it often leads to confusing and hard-to-maintain code if not used carefully. - -**syntax** -```cpp -#include - -int main() { - std::cout << "Before goto\n"; - - goto skip; - - std::cout << "This will be skipped\n"; - -skip: - std::cout << "After label\n"; - - return 0; -} -/* - Output: - Before goto - After label -*/ -``` Visit the following resources to learn more: - [@video@The 'if-else' Statement in C++](https://www.youtube.com/watch?v=9-BjXs1vMSc) - [@video@Learn C++ With Me - Switch Statement](https://www.youtube.com/watch?v=uOlLs1OYSSI) -- [@video@ why is it illegal to use "goto"?](https://youtu.be/AKJhThyTmQw?si=gjEqAsDZVMDGVAT2) \ No newline at end of file +- [@video@Why is it illegal to use "goto"?](https://youtu.be/AKJhThyTmQw?si=gjEqAsDZVMDGVAT2) \ No newline at end of file From 85666e7a85d4bde5203ebb4e73784440af26543f Mon Sep 17 00:00:00 2001 From: 28arnab <68094959+28arnab@users.noreply.github.com> Date: Thu, 10 Apr 2025 01:15:52 +0000 Subject: [PATCH 4/4] chore: update roadmap content json --- public/roadmap-content/cpp.json | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/public/roadmap-content/cpp.json b/public/roadmap-content/cpp.json index b2da28a9e..5f8437147 100644 --- a/public/roadmap-content/cpp.json +++ b/public/roadmap-content/cpp.json @@ -141,8 +141,24 @@ }, "bjpFWxiCKGz28E-ukhZBp": { "title": "if else / switch / goto", - "description": "", - "links": [] + "description": "C++ provides you with tools which helps you to control the way your program behaves (logic flows) based on how the user interact with your program. Here we will discuss about `if-else`, `switch` and `goto` are three common ways to guide the flow of logic in your code.\n\nVisit the following resources to learn more:", + "links": [ + { + "title": "The 'if-else' Statement in C++", + "url": "https://www.youtube.com/watch?v=9-BjXs1vMSc", + "type": "video" + }, + { + "title": "Learn C++ With Me - Switch Statement", + "url": "https://www.youtube.com/watch?v=uOlLs1OYSSI", + "type": "video" + }, + { + "title": "Why is it illegal to use \"goto\"?", + "url": "https://youtu.be/AKJhThyTmQw?si=gjEqAsDZVMDGVAT2", + "type": "video" + } + ] }, "oYi3YOc1GC2Nfp71VOkJt": { "title": "Functions",