C Programming Cheat Sheet - 3
Tags: c
Published: 2021-04-06 08:39:26

## 3.The Decision Control Structure

C has three major decision making instructions—the `if` statement, the `if-else` statement, and the `switch` statement.

###3.1 The If Statement
C Uses The keyword `if` to implement the decision control instruction. The general form of `if` statement looks like this:
```
//for single statement
if(condition) 
    statement;
  
//for multiple statement
if(condition) 
{ 
    block of statement; 
}
```
The more general form is as follow:
```
//for single statement
if(expression) 
    statement;
  
//for multiple statement
if(expression) 
{ 
    block of statement; 
}
```
Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in the if statement. For example all the following if statements are valid

```
if (3 + 2 % 5) 
    printf("This works");
```

The expression `(3 + 2 % 5)` evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the `printf("This works");` gets executed.

### 3.2 The If-Else Statement

The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated as

```
if (expression)
{ 
    block of statement;
}
else
    statement;
```
####**Note**
* The group of statements after the `if`upto and not including the `else` is called an `if block`. Similarly, the statement after the `else` form the `else block`.
* Notice that the `else` is written exactly below the `if`. The statements in the `if block` and those in the `else block` have been indented to the right.
* Had there been only one statement to be executed in the `if block` and only one statement in the `else block` we could have dropped the pair of braces.
* As with the `if` statement, the default scope of `else` is also the statement immediately after the `else`. To override this default scope a pair of braces as shown in the above *"Multiple Statements within if"* must be used.

###3.3 Nested if-else

If we write an entire `if-else` construct within either the body of the `if` statement or the body of an `else` statement. This is called **nesting of `ifs`**. This is demonstrated as -

```
if (expression1)
    statement;
else 
{
    if (expression2)
        statement;  
    else
    { 
        block of statement;
    }
}
```
### 3.4 The if-else Ladder/else-if Clause

The `else-if` is the most general way of writing a multi-way decision.

```
if(expression1) 
    statement; 
else if(expression2) 
    statement; 
else if(expression3) 
    statement; 
else if(expression4) 
{ 
    block of statement; 
} 
else 
    statement;
```
The expressions are evaluated in order; if an expressions true, the _"statement"_ or _"block of statement"_ associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces. The last `else` part handles the *"none of the above"* or *default case* where none of the other conditions is satisfied.

####3.5 Switch Statements or Control Statements

The `switch` statement is a multi-way decision that tests whether an expression matches one of a number of `constant` integer values, and branches accordingly. The `switch` statement that allows us to make a decision from the number of choices is called a `switch`, or more correctly s`witch-case-default`, since these three keywords go together to make up the `switch` statement.
```
switch (expression) 
{
    case constant-expression: 
        statement1;
        statement2;
        break;
    case constant-expression: 
        statement;
        break;
    ...
    default: 
        statement;
}
```
* In `switch…case` command, each `case` acts like a simple label. A label determines a point in program which execution must continue from there. `Switch` statement will choose one of `case` sections or labels from where the execution of the program will continue. The program will continue execution until it reaches `break` command.
  * `break` statements have vital rule in `switch` structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end of switch block will be executed (while most of the time we just want one case section to be run).
 * `default section` will be executed if none of the `case` sections match `switch` comparison.

###3.6 switch Versus if-else Ladder

There are some things that you simply cannot do with a `switch`.
These are:

    *  A float expression cannot be tested using a switch
    * Cases can never have variable expressions (for example it is wrong to say `case a +3` )
    * Multiple cases cannot use same expressions.