If-else

Notes Zone
0

Control Statement / Decision making Statement/ Conditional Statement

The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs.

They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making statements in programming languages decide the direction of the flow of program execution.


Need of Conditional Statements

There come situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code. For example, in C if x occurs then execute y else execute z. There can also be multiple conditions like in C if x occurs then execute p, else if condition y occurs execute q, else execute r. This condition of C else-if is one of the many ways of importing multiple conditions. 

These decision-making Statement Programming language decide the direction of the flow of program executed.

Types of Control Statement:

  1. if Statement
  2. if else Statement
  3. Nested if Statement
  4. if-else-if Ladder
  5. Switch Statement


1. Simple if Statement:

The if in C is the most simple decision-making statement. It consists of the test condition and if block or body. If the given condition is true only then the if block will be executed.

The if Statement in C is a Control Structure that evaluates a Condition. If the Condition is true, it run the associated Code, if false the Code is ignored.


Syntax of if Statement in C

if(condition) 
{
    // if body
    // Statements to execute if condition is true
}


The working of the if statement in C

STEP 1: When the program control comes to the if statement, the test expression is evaluated.

STEP 2: If the condition is true, the statements inside the if block are executed.

STEP 3: If the expression is false, the statements inside the if body are not executed.

STEP 4: Program control moves out of the if block and the code after the if block is executed.


Advantages of if Statement

Following are the main advantages of the if statement in C:

  • It is the simplest decision-making statement.
  • It is easy to use and understand.
  • It can evaluate expressions of all types such as int, char, bool, etc.

Disadvantages of if Statement

The main limitations of if block is listed below:

  • It contains only a single block. In case when there are multiply related if blocks, all the blocks will be tested even when the matching if block is found at the start
  • When there are a large number of expressions, the code of the if block gets complex and unreadable.
  • It is slower for a large number of conditions.


2. If-else Statement:

The if-else statement in C is a flow control statement used for decision-making in the C program. It is one of the core concepts of C programming. It is an extension of the if in C that includes an else block along with the already existing if block.

The if-else statement is a decision-making statement that is used to decide whether the part of the code will be executed or not based on the specified condition (test expression). If the given condition is true, then the code inside the if block is executed, otherwise the code inside the else block is executed.

Syntax of if-else

if (condition) {
    // code executed when the condition is true
}
else {
    // code executed when the condition is false
}
The working of the if-else statement in C

STEP 1: When the program control first comes to the if-else block, the test condition is checked.

STEP 2: If the test condition is true:

             The if block is executed.

STEP 3: If the test condition is false:

              The else block is executed

STEP4: After that, the program control continues to the statements below the if-else statement.


Advantages of if-else Statement

  • The if-else statement enables the user to execute different statements based on different conditions.
  • It can evaluate test expressions of type int, char, boolean, and more.
  • It helps in modifying the flow of the program.
  • It is simple, efficient, and easier to read when there is less number of conditions.

Disadvantages of if-else Statement

  • If there are a lot of if statements present, the code becomes unreadable and complex.
  • It also becomes slower compared to the switch statement.


3. If-else-if ladder statement:

The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.


Syntax of if-else-if ladder

// any if-else ladder starts with an if statement only
if(condition) {
        
}
else if(condition) { 
 // this else if will be executed when condition in if is false and
 // the condition of this else if is true
}
.... // once if-else ladder can have multiple else if 
else { // at the end we put else 
        
}                                                              


Working Flow of the if-else-if ladder:


1.The flow of the program falls into the if block.
2. The flow jumps to 1st Condition
3. 1st Condition is tested respectively:
      If the following Condition yields true, go to Step 4.
      If the following Condition yields false, go to Step 5.
4. The present block is executed. Goto Step 7.
5. The flow jumps to Condition 2.
      If the following  Condition yields true, go to step 4.
      If the following Condition yields false, go to Step 6.
6. The flow jumps to Condition 3. 
      If the following Condition yields true, go to step 4.
      If the following Condition yields false, execute the else block. Goto Step 7.
7. Exits the if-else-if ladder.

4. Nested If else Statement:

Nested If Else Statements are a fundamental concept in programming. They allow us to create more complex decision-making structures by placing one if else statement inside another. In this article, we will discuss the Nested if else statement.

Syntax Nested if else Statement

if (condition1)

 {  

   /* code to be executed if condition1 is true */  
   if (condition2)
 {  
      /* code to be executed if condition2 is true */  
   }
 else 
{  
      /* code to be executed if condition2 is false */  
  }  
}
 else 
{  
   /* code to be executed if condition1 is false */  
}


5. Switch Statement:

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possible values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.

Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases).

Syntax of switch Statement in C

switch(expression)
{
case value1: statement_1;
             break;
case value2: statement_2;
             break;
.
.
.
case value_n: statement_n;
              break;
default: default_statement;
}


Rules of the switch case statement

Following are some of the rules that we need to follow while using the switch statement:

  • In a switch statement, the “case value” must be of “char” and “int” type.
  • There can be one or N number of cases.
  • The values in the case must be unique.
  • Each statement of the case can have a break statement. It is optional.
  • The default Statement is also optional.


How switch Statement Work?

The working of the switch statement in C is as follows:

Step 1: The switch variable is evaluated.

Step 2: The evaluated value is matched against all the present cases.

Step 3: If the matching case value is found, the associated code is executed.

Step 4: If the matching code is not found, then the default case is executed if present.

Step 5: If the break keyword is present in the case, then program control breaks out of the switch statement.

Step 6: If the break keyword is not present, then all the cases after the matching case are executed.

Step 7: Statements after the switch statement are executed.


Advantages of C switch Statement

  • Easier to read than if else if.
  • Easier to debug and maintain for a large number of conditions.
  • Faster execution speed.

Disadvantages of C switch Statement

  • Switch case can only evaluate int or char type.
  • No support for logical expressions.
  • Have to keep in mind to add a break in every case.

Tags

Post a Comment

0Comments

Post a Comment (0)