From d5cf6f23ccc85ebcd3b6dd7a92b1ea0121e35dae Mon Sep 17 00:00:00 2001 From: Imdadul Rehmaan <164654367+irhmaan@users.noreply.github.com> Date: Tue, 25 Mar 2025 13:13:03 +0530 Subject: [PATCH] Update control-flow--statements@s5Gs4yF9TPh-psYmtPzks.md Added brief intro about C++ control statements. --- ...-flow--statements@s5Gs4yF9TPh-psYmtPzks.md | 94 ++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/src/data/roadmaps/cpp/content/control-flow--statements@s5Gs4yF9TPh-psYmtPzks.md b/src/data/roadmaps/cpp/content/control-flow--statements@s5Gs4yF9TPh-psYmtPzks.md index 8b772289d..a7619beab 100644 --- a/src/data/roadmaps/cpp/content/control-flow--statements@s5Gs4yF9TPh-psYmtPzks.md +++ b/src/data/roadmaps/cpp/content/control-flow--statements@s5Gs4yF9TPh-psYmtPzks.md @@ -1 +1,93 @@ -# Control Flow & Statements \ No newline at end of file +# Control Flow & Statements +Control flow & statements provides the ability to make decision which part of code should be executed or not based on certain conditions(also called decision control statements). In C++, the different types of control statements are: + + ## 1. if + Simplest way to check weather a conditon is true or false and make a decision. + +```cpp + //Syntax + + if(condition_to_be_evaluated){ + //code to be executed when condition evaluates to true. + // this section will only execute if specified condition is + // met. Else it won't. + } + +``` + ```cpp + int age = 19; + // check if age is greater than 19. + if(age > 18) { + std::cout << "You are eligible to vote" << std::endl; + } + +``` +Note: You can skip to write curly brasses if there is only one statement. +```cpp + int age = 19; + if (age > 18) + cout << "allowed to vote"; + return 0; +``` + + ## 2. if else: + When if condition is not evualuated true and you still want to execute some code, then if-else is used. + ```cpp + //Syntax + + if(condition_to_be_evaluated){ + //code to be executed when condition evaluates to true. + // this section will only execute if specified condition is + // met. Else it won't. + } else { + // code to be excuted only when if does not + // match to specified condition. +} + +``` + ```cpp + int age = 10; + // check if age is greater than 19. + if(age > 18) { + std::cout << "You are eligible to vote" << std::endl; + } else { + std::cout << "Not eligible to vote" << std::endl; + } +``` + + ## 3. Switch: + +```cpp + //Syntax + + switch(expression_to_be_compared){ + case 'Value_A': + std::cout << "Value is A" < b) ? a : b; +std::cout << max; +``` + +To read more in depth about control statements such as if else if ladder , Nested if, Jump, goto, break. Check this out: https://www.geeksforgeeks.org/cpp-decision-making/