Page Content

Tutorials

What Are The Decision Making Statements In C?

Decision Making Statements in C

Decision making is a basic idea in C programming that enables your program to select between various execution pathways depending on predetermined criteria. A program’s statements typically run one after the other in a sequential order. It is necessary to modify this normal flow, which is a component of control flow or program control, in order to execute distinct operations based on whether a condition is true or not. Branching is another term for decision making. The if, if-else, and switch statements are among the statements that C offers for this.

An expression that can be true or untrue is a condition. C considers non-zero values true and zero values false.

The if Statement

The simplest statement for making decisions is the if statement. A statement or block of statements is only executed if a predetermined condition is met.

The if statement may be expressed generally as follows: if (condition) statement; if the condition included in parenthesis evaluates to true (non-zero), the statement that follows is executed. The if statement is bypassed if the condition is false (zero), and the program proceeds with the code that follows.

You must use curly brackets {} to collect statements that need to be executed when the condition is true into a code block.
A block of statements used in syntax: if (condition) { statement1; statement2;}

Syntax: if (condition) { statement1; statement2;}

If statements, for example, do not terminate with a semicolon following the condition enclosed in parenthesis. On the other hand, the if block’s sentences usually terminate with a semicolon.

Code Sample for an if statement: Determining whether a given number is positive

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number); // Use scanf to read an integer [10, 11]
    // Check if the number is positive
    if (number > 0) {
        printf("The number is positive.\n");
    }
    printf("This line is always printed.\n");
    return 0;

Only when the value entered for the number is greater than 0 does the example print the message “The number is positive.” Regardless of the circumstance, the final printf is executed.

The if-else Statement

The choice is made in both directions via the if-else expression. It enables you to run a separate block of code in the event that the condition is false and another block of code in the event that the condition is true.

Syntax: if (condition) statement1; else statement2;

It is assessed. The execution of statement 1 occurs if it is true. The code in the else section, statement 2, is run if the condition is false. The else block will never be invoked in addition to the if block. Otherwise, it is optional.

To include multiple statements in the if or else component, use brackets {}, similar to the if statement.

Example if-else code: To determine if an integer is even or odd:

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    // Check if the number is even or odd using the modulus operator (%)
    if (number % 2 == 0) {
        printf("The number is even.\n");
    } else {
        printf("The number is odd.\n");
    }
    return 0;
}

The software in this example determines whether the residue of dividing an integer by two is zero. In the event that the requirement is met, the number is even. If not, the number is odd (condition is false).

Nested if-else Structures

The if or else component of another if-otherwise statement can contain if or if-else statements. Nesting is the term for this. You can test several conditions in succession by nesting them.

An inner if or if-else’s execution is contingent upon the assessment of the outer condition or conditions. An if statement enclosed in an else block, for example, will only be examined if the outer if condition is false.

Nested if-else Code Example: For Determining the Greatest of Three Numbers:

#include <stdio.h>
int main() {
    int num1, num2, num3;
    printf("Enter three integers: ");
    scanf("%d %d %d", &num1, &num2, &num3);
    // Outer if: Check if num1 is greater than num2
    if (num1 > num2) {
        // Inner if: If num1 > num2, check if num1 is also greater than num3
        if (num1 > num3) {
            printf("The greatest number is: %d\n", num1);
        } else { // If num1 > num2 but num1 is NOT > num3, then num3 must be the greatest
            printf("The greatest number is: %d\n", num3);
        }
    } else { // Outer else: If num1 is NOT greater than num2 (meaning num2 >= num1)
        // Inner if: If num2 >= num1, check if num2 is greater than num3
        if (num2 > num3) {
            printf("The greatest number is: %d\n", num2);
        } else { // If num2 >= num1 but num2 is NOT > num3 (meaning num3 >= num2), then num3 must be the greatest
            printf("The greatest number is: %d\n", num3);
        }
    }
    return 0;
}

By comparing three values in phases, this program shows how to utilise nested if-else statements to find the largest of them. The first thing it does is see if num1 is greater than num2. It then makes another comparison to determine the maximum among the remaining options based on that outcome.

Even for single lines, using braces {} for code blocks is frequently advised to enhance readability and prevent potential problems such as the “dangling else” problem, in which an else could unintentionally associate with a different if than intended. Deep nesting may not be as readable as alternatives like the switch statement or the if else ladder for complex decision trees with several conditions.

Click to know about the C Operator Precedence And Associativity.

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index