From 121e549be25e2faee9d2bba22985538d784116f3 Mon Sep 17 00:00:00 2001 From: Yashdeep Singh <107052354+Yash5204@users.noreply.github.com> Date: Wed, 29 Jun 2022 12:26:53 +0530 Subject: [PATCH] Update conditionals.md I have added some more notes to make them more productive and easy to understand --- C++/Notes/conditionals.md | 54 +++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/C++/Notes/conditionals.md b/C++/Notes/conditionals.md index 341b46c..60c7025 100644 --- a/C++/Notes/conditionals.md +++ b/C++/Notes/conditionals.md @@ -1,21 +1,45 @@ -### **Conditionals** +# Conditional Statements ---- +1. Conditional statements, also known as selection statements, are used to make decisions based on a given condition. +2.If the condition evaluates to True, a set of statements is executed, otherwise another set of statements is executed. +3. **“if-else”** and **“switch”** both are selection statements. +4. However, there are some differences in their operations. -Conditionals refer to if-else and switch -these are used to choose between multiple options, just like in real life we many times have to choose between multiple options, based on the condition we are in -if-else and switch do the similar thing but in code -Some examples of such problems can be program to check eligibility to vote based on age, FizzBuzz problem -FizzBuzz is a problem in which we print Fizz if number is divisible by 3 and print Buzz if its divisible by 5 +# if Statement : -**If-else Syntax** +- The if statement selects and executes the statement(s) based on a given condition. +- If the condition evaluates to True then a given set of statement(s) is executed. +- However, if the condition evaluates to False, then the given set of statements is skipped and the program control passes to the statement following the if statement. + +# if-else Statement : +- The if – else statement causes one of the two possible statement( s) to execute, depending upon the outcome of the condition. + +**if-else Syntax :** `if (Condition){ ` `----Thing to do----` `} else { ` `--Else what to do--` `} ` -**Switch Syntax** +# Nested if-else Statement : +- A nested if-else statement contains one or more if-else statements. +- The if else can be nested in three different ways :- +1. **if–else** statement nested within the if part. +2. **if-else** statement is nested within the else part. +3. **if-else** statement is nested within both the if and the else parts. + +# if-else-if ladder : +- Also known as the if-else-if staircase, has an if-else statement within the outermost else statement. +- The inner else statement can further have other if-else statements. + +# Switch Statement : + +- A switch statement can only work for quality of comparisons. +- No two case labels in the same switch can have identical values. +- If character constants are used in the switch statement, they are automatically converted to their equivalent ASCII codes. +- The switch statement is more efficient choice than if in a situation that supports the nature of the switch operation. + +**Switch Syntax :** `switch(expression){` `case const1: ` `---things to do----` @@ -27,4 +51,14 @@ FizzBuzz is a problem in which we print Fizz if number is divisible by 3 and pri ` break; ` `} ` -switch case is more cleaner, but mostly people prefer using if-else \ No newline at end of file +# Key Differences Between if-else and switch + +1. Expression inside **if statement** decide whether to execute the statements inside if block or under else block. On the other hand, expression inside **switch statement** decide which case to execute. + +2. An **if-else statement** uses multiple statements for multiple choices. On other hand, **switch statement** uses single expression for multiple choices. + +3. **If-esle statement** checks for equality as well as for logical expression. On the other hand, **switch** checks only for equality. + +4. The **if statement** evaluates integer, character, pointer or floating-point type or Boolean type. On the other hand, **switch statement** evaluates only character or a integer data type. + +5. The **if statement** is more flexible than **switch statement**, but the **switch statement** is more efficient than **if-else statement**.