If  else statements in C simplified

If else statements in C simplified

In C program If, if else, switch statements are known as conditional statements.

Conditional statements: are also known as Boolean expressions programs that return true or false on events and carry out tasks based on these results. A real-life example of conditional simple transactions at the ATM can go like this;

Check if the customers have the requested money in their bank account, if they do proceed to give them their withdrawal, if not(else) tell them they have insufficient balance.

This is a simple conditional statement. Another example is that of you getting a new car;

If the price of a Lamborghini is higher than your budget /car savings then buy a jaguar only if you have enough money, but if you can’t afford any then wait till next year.

So conditional statements are been implemented in all our day-to-day activities even when we input out passwords. Going grocery shopping and so on.

If statement in C

An if statement in C is declared by using the "if" keyword and inputting the condition to be satisfied in brackets followed by the code to run if the condition is satisfied like this:

if (conditinal_expression_to_be_satisfied)
    The code that will run when condition is satisfed

If you have more than a line of code then you need to surround it with curly braces like this:

if (conditinal_expression_to_be_satisfied){
    The code that will run when condition is satisfed 
}

Example of if statement:

#include <stdio.h>

int main() {
    int number = 10; /* initialize a variable called number*/

    /* Check if the number is greater than 5*/
    if (number > 5)
        printf("The number is greater than 5\n");
    /* if the number is greater then continue to print*/

    return 0;
}

output

The number is greater than 5

If else statements in C

An if-else statement unlike an if statement is a conditional statement that has an option or a code that runs if the If condition wasn’t satisfied. Let's say it’s an If statement with another block of code to run if the If condition is false. The block of code that runs for the else might have its own conditional expression and it might not. In this scenario, the else block runs only if the If condition is false.

Flowchart for an if-else statement:

The syntax for the If else statement without conditions:

if (conditinal_expression_to_be_satisfied){
    The code that will run when condition is satisfed 
}else{
    run this code instead
}

The syntax for the If else statement with conditions:

if (conditinal_expression_to_be_satisfied){
    The code that will run when condition is satisfed 
}else if (another_conditinal_expression_to_be_satisfied){
    /The code that will run when the elseif condition is satisfed
}else{
    run this code instead
}

Example of if else statements

#include <stdio.h>

int main() {
    int number = 3; /* initialize a variable called number*/

    /* Check if the number is greater than 5*/
    if (number > 5){
        printf("The number is greater than 5\n");
    /* if the number is greater then continue to print*/
        }else {
        printf("The number is less than or equal to 5\n");
    /* else (if not)print the number is less than or equal to 5 */
    }

    return 0;
}

output

The number is less than or equal to 5

if...else if example

#include <stdio.h>

int main() {
    int number = 10;

    // Check if the number is greater than 10
    if (number > 10) {
        printf("The number is greater than 10\n");
    }
    // Check if the number is equal to 10
    else if (number == 10) {
        printf("The number is equal to 10\n");
    }
    // If the number is neither greater than 10 nor equal to 10, it must be less than 10
    else {
        printf("The number is less than 10\n");
    }

    return 0;
}
Output
The number is equal to 10\n

Switch case statement in C

If the conditions are numerous then it is preferable to use a switch case statement. It checks through multiple conditions and executes the exact code for the condition that is satisfied.

The syntax for switch case statement:

switch (expression) {
    case constant1:
/*code to be executed if expression is equal to constant1*/         
        break;
    case constant2:/*code to be executed if expression is equal to constant2*/ 
        break;
    ...
    default:
/*code to be executed if expression does not match any of the constants*/
        break;
}

Example of switch case

#include <stdio.h>

int main() {
    int number = 2;  /* Declare and initialize the variable number with the value 2*/

    switch (number) {  /* Start a switch statement using the value of number*/
        case 1:  /* If number is 1, execute the following code:*/
            printf("The number is 1\n");  /* Print the message "The number is 1" to the console*/
            break;  /* Exit the switch statement*/
        case 2:  /* If number is 2, execute the following code:
               Print the message "The number is 2" to the console*/
        printf("The number is 2\n");
            break;  /* Exit the switch statement*/
        case 3:  /* If number is 3, execute the following code:*/
            printf("The number is 3\n");  /* Print the message "The number is 3" to the console*/
            break;  /* Exit the switch statement*/
        default:  /* If number is not 1, 2, or 3, execute the following code:*/
            printf("The number is not 1, 2, or 3\n");  /* Print the message "The number is not 1, 2, or 3" to the console*/.
            break;  /* Exit the switch statement*/
    }

    return 0;  // Exit the program with a status code of 0
}

Output

The number is 2