Decision Making Statements in C with Examples

This Decision Making in C Programmers must define one or more conditions that will be evaluated or tested by the program.

A statement or statements that will be run if the condition is true, and optionally, further statements that will be executed if the condition is false.

Introduction Decision Making in C

We all make judgments every day, whether consciously or unconsciously, and every single one of them is the result of computations and comparisons.

The execution of our code blocks needs to be controlled and regulated, just like in programming.

What is a Decision Making Statement in C? 

Decision-making statements in C determine the program’s arc and flow.

It is because they provide conditions using Boolean expressions that are evaluated to a true or false Boolean value.

They are sometimes referred to as conditional statements.

A particular piece of code will run if the condition is true; if the condition is false, the block will not run.

The following categories of decision-making statements are offered by the C programming language.

StatementDescription
if statementA boolean expression is followed by one or more statements in an if statement.
if else statementAn optional else statement, which is executed if the Boolean expression is false, might come after an if statement.
nested if statementsAn if or else if statement can be used inside of another if or else if statement (s).
switch statementA switch statement enables the comparison of a variable’s value to a list of possible values.
nested switch statementsOne switch statement can be used inside another switch statement (s).

if Statement in C

The if statement in C is used to examine a given condition and perform actions based on whether or not that condition is true.

It’s most commonly utilized in scenarios where we need to perform many operations for various situations.

It is a very strong conditional statement. The If statement is in charge of changing a program’s execution flow.

A condition is always used with an if statement. Before any statement inside the body of If is executed, the condition is evaluated first.

In C programming, the if statement has the following syntax:

if (example expression) 
{
   // code to be performed
}

How if statement work?

The example expression inside the parentheses is evaluated by the if statement ().

  • If the example expression returns true, the statements in the if body are performed.
  • Statements inside the body of if are not performed if the example expression is evaluated to be false.

An example of Expression is True

int example = 5; 
if(example < 10) { //codes to be performed }

An example of Expression is False

int example = 5;
if(example > 10)
{
//codes to be performed
}

if Statement Example #1:

// Program to display a number if it is negative

#include <stdio.h>
int main(){
int num;

printf("Enter an integer: ");
scanf("%d", &num);

// true if number is less than 0
if (num < 0) {
printf("You entered %d.\n", num);
}

printf("The if statement is easy to learn.");

return 0;
}

Output 1 of Executed Code Above Example #1

Enter an integer: You entered -5.
The if statement is easy to learn.

The expression number 0 is evaluated to be true when the user enters -5. As a result, the number -5 that you entered appears on the screen.

You can test the above example here! ➡ C Online Compiler

If Else Statement using C

If Else Statement using C – An optional else block can be added to the if statement.

For a single condition, the if-else statement is used to conduct two operations.

The if-else statement is an expansion of the if statement that allows us to conduct two different operations, one for the condition’s correctness and the other for its incorrectness.

if (test-expression) {
    // run code if test expression is true
}
else {
    // run code if test expression is false
}

How if else statement works?

  • If the true value of the test expression is found.

  • The statements included within the if body are performed.

  • The execution of statements inside the body of else is skipped.

  • If the test expression yields a false result.

  • The statements inside the else body are executed.

  • The execution of statements within the if body is skipped.

We’ll set a value for a variable and develop a program to see if the number is less than 10 or larger than ten.

Let’s get this started.

Example of If Else Statement in C

#include<stdio.h>
int main()
{
    int number=45;
    if(number<10)
    {
        printf("The value is less than 10");
    }
    else
    {
        printf("The value is greater than 10");
    }
    return 0;
}

Output of Executed Code Above:

The value is greater than 10

You can test the above example here! ➡ C Online Compiler

Explanation of the illustration above

  • A variable with the value 19 has been created. Using a ‘C’ program, we must determine whether the number is greater than or less than ten. The if-else statement was utilized to do this.

  • We’ve used the number 10 as a condition since we need to compare our value to 10.

  • As you can see, the first block is always a true block, which implies that if the test-expression value is true, the first block, If, will be run.

  • An else block is the second block. If the value of the test-expression becomes false, the statements in this block will be executed.

  • Because the value of the number in our program is bigger than ten, the test condition is false, and the else block is run. As a result, our output will be “The value is bigger than 10” from an else block.

  • The program will terminate with a successful outcome after the if-else.

Nested If else statement in C

Nested If else statement in C – An if statement that is nested inside another if statement is known as a nested if statement.

When you need to test a number of situations before deciding on the best course of action, nested if statements come in handy.

Nested if else is when an if else statement appears within the body of another “if” or “else” statement.

In C, the syntax for a Nested If Else Statement is:

//check if the first condition holds
if (condition 1) {
    
    //if the second condition holds
    if (condition 2) {
        do something
    }
    //if the second condition does not hold
    else {
        do something else
    }
    
}
// if the first condition does not hold
else{
    
    //if the third condition holds
    if (condition 3) {
        do something
    }
    //if the third condition does not hold
    else {
        do something else
    }
    
}

Example #1 of Nested If Else Statement in C

#include <stdio.h>

int main() {


    int n;


    scanf("%d",&n);


    if (n % 2 == 0){


        printf("Even ");


        if (n % 4 == 0) {

            printf("and divisible by 4");
        } else {

            printf("and not divisible by 4");
        }
    } else {

        printf("Odd ");


        if(n % 3 == 0) {

            printf("and divisible by 3");
        } else {

            printf("and not divisible by 3");
        }

    }

    return 0;
}

Output of Executed Code Above Example #1

16 Even and divisible by 4

The above illustration uses an Nested if-else statement To determine whether the number is even or odd, and if it is even.

The user will input 6 and it is even but not divisible by 4.

9 Odd and divisible by 3

The above illustration uses an Nested if-else statement To determine whether the number is even or odd, and if it is even.

The user will input 9 and it is odd so it is divisible by 3.

You can test the above example here! ➡ C Online Compiler

Summary

In summary of Decision Making Statements in C with Examples, If else statements are used to make decisions in computer languages.

The C programming language allows you to nest if else statements to make better decisions.

Nesting is the process of putting many if else statements inside an if and else statement. The second if and else expression is considered to be nested within the first.

I hope you enjoy this post on Decision Making Statements in C with Examples, in which I attempt to illustrate the language’s core grammar.


Leave a Comment