Using if-else statements for decision-making

if-else

When it comes to programming, decision-making is a crucial aspect. It involves determining the path the program should follow based on certain conditions. One of the most fundamental tools for decision-making in programming is the “if-else” statement. If-Else statements are a fundamental concept in many programming languages, including Java, Python, C++, and JavaScript. They allow developers to control the flow of their programs based on conditions. With if-else statements, a program can take different paths depending on whether a condition evaluates to true or false.

What is if-else statements?

An if-else statement is a conditional statement that executes a block of code if a given condition is true. If the condition is false, an alternate block of code can be executed, specified by the “else” keyword.

Syntax: –

if (condition) {

    // Code block to be executed if the condition is true

} else {

    // Code block to be executed if the condition is false

}

How it’s work?

The program evaluates the condition within the parentheses of the if statement. If the condition is true, the code inside the if block executes. If the condition is false, the code inside the else block (if present) executes.

Write a program to check that given number is even or odd by using if-else statement?
#include <stdio.h>
#include <conio.h>

void main() {
    int num;
          clrscr();
    // Input the number from the user
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Check if the number is even or odd using if-else statements
    if (num % 2 == 0) {
        // if the reminder comes 0 that indicates that the given number is even.
        printf("%d is an even number.\n", num);
    } else {
        // otherwise the given number is odd.
        printf("%d is an odd number.\n", num);
    }

    getch();
}
Output: - 
Enter an integer: 34
34 is an even number.
[program terminated] 
Write a program to check that given number is a multiple of 3 by using if-else statement?
#include <stdio.h>
#include <conio.h>

void main() {
    int number;
    clrscr();

    // Input the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check if the number is a multiple of 3
    if (number % 3 == 0) {
        printf("%d is a multiple of 3.\n", number);
    } else {
        printf("%d is not a multiple of 3.\n", number);
    }

    getch();
}
Output: -
Enter a number: 153
153 is a multiple of 3.
[program terminated] 
Write a program to check that given number is greater than 10 and smaller than 100 by using if-else statement?
#include <stdio.h>
#include <conio.h>

void main() {
    int number;
    clrscr();
    // Input the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check if the number is greater than 10 and smaller than 100
    if (number > 10 && number < 100) {
        printf("%d is greater than 10 and smaller than 100.\n", number);
    } else {
        printf("%d is not greater than 10 and smaller than 100.\n", number);
    }

    getch();
}
Output: -
Enter a number: 5	
5 is not greater than 10 and smaller than 100.
[program terminated] 
Explanation: –
  • We include the standard input/output library stdio.h for input/output operations.
  • We declare an integer variable number to store the user input.
  • We prompt the user to enter a number and read it using scanf.
  • We check if the number satisfies both conditions, number > 10 and number < 100, using the logical AND operator &&. If the condition is true, the number is both greater than 10 and smaller than 100, and we print the appropriate message. Otherwise, we print that it doesn’t meet both conditions.

Nested if-else

Nested if-else statements are conditional statements in programming that are placed inside other if or else blocks. This allows for more complex decision-making by creating a hierarchy of conditions to be evaluated. In other words, a new if-else statement is placed inside the body of an existing if or else block.

The general syntax of a nested if-else statement looks like this:

if (condition1) {

    // Code block to be executed if condition1 is true

    if (condition2) {

        // Code block to be executed if condition2 is true

    } else {

        // Code block to be executed if condition2 is false

    }

} else {

    // Code block to be executed if condition1 is false

}

Here’s a step-by-step explanation of how nested if-else statements work:

  • The program first evaluates the outer if condition (condition1).
  • If condition1 is true, the code inside the first if block is executed.
  • Inside the first if block, there is another if-else statement (nested if-else) with its own condition (condition2).
  • If condition2 is true, the code inside the nested if block is executed.
  • If condition2 is false, the code inside the nested else block is executed.
  • If condition1 is false, the code inside the else block of the outer if-else statement is executed.
nested if flowchart

Nested if-else statements allow for more specific and detailed decision-making. You can use them to handle multiple conditions and take different actions based on the combinations of these conditions. However, it’s important to be cautious with nesting too deeply, as it can make the code more difficult to read and maintain. In some cases, it may be better to use other control structures, like switch statements or separate functions, to handle complex conditions in a more organized manner.

Here are three C programs that use nested if statements to demonstrate different scenarios:

Write a program to check that given year is leap year or not?
#include <stdio.h>
#include <conio.h>

void main() {
    int year;
    clrscr();
    // Input the year from the user
    printf("Enter a year: ");
    scanf("%d", &year);

    // Check if the year is a leap year
    if (year % 4 == 0) {
        if (year % 100 != 0 || year % 400 == 0) {
            printf("%d is a leap year.\n", year);
        } else {
            printf("%d is not a leap year.\n", year);
        }
    } else {
        printf("%d is not a leap year.\n", year);
    }

    getch();
}
Output: -
Enter a year: 2012
2012 is a leap year.

[program terminated] 
Write a program to check given numbers is positive or negative by using nested if statement?
#include <stdio.h>
#include <conio.h>
void main()
clrscr();
int x;
int y;
printf(“Enter the value of x and y: ”);
scanf(“%d %d”, &x, &y);
if (x > 0) {
    if (y > 0) {
        printf("Both x and y are positive.\n");
    } else {
        printf("x is positive, but y is not positive.\n");
    }
} else {
    printf("x is not positive.\n");
}

    getch();
}

Output: -

Enter the value of x and y: 32 -3
x is positive, but y is not positive.

[program terminated] 

else if

In C programming, the “else if” statement is used as an extension to the basic “if” statement to handle multiple conditions in a structured and organized manner. It provides an alternative condition that is evaluated only if the preceding “if” condition is false.

The syntax of the “else if” statement is as follows:

if (condition1) {

    // Code block to be executed if condition1 is true

} else if (condition2) {

    // Code block to be executed if condition2 is true and condition1 is false

} else {

    // Code block to be executed if both condition1 and condition2 are false

}

Here’s how the “else if” statement works:

  • The program first evaluates the initial “if” condition (condition1).
  • If condition1 is true, the code inside the corresponding “if” block is executed, and the program skips the “else if” and “else” parts, proceeding to the next statement after the entire “if-else” construct.
  • If condition1 is false, the program moves to the next “else if” condition (condition2) and evaluates it.
  • If condition2 is true, the code inside the corresponding “else if” block is executed, and the program skips the “else” part, proceeding to the next statement after the entire “if-else” construct.
  • If condition2 is also false, the program executes the code inside the “else” block (if present), as it serves as the default option when both condition1 and condition2 are false.

The “else if” statement is particularly useful when you have multiple conditions to check, and each condition is exclusive of the others. It provides a clean and readable way to handle such situations without nesting multiple “if” statements, which can lead to code clutter and reduced readability.

else if flowchart
Write a program to check to Determining the Time of the Day by using else-if statement?
#include <stdio.h>
#include <conio.h>

void main() {
    int hour;
    clrscr();
    // Input the hour from the user
    printf("Enter the hour (in 24-hour format): ");
    scanf("%d", &hour);

    // Determine the time of the day
    if (hour >= 0 && hour < 12) {
        printf("Good morning!\n");
    } else if (hour >= 12 && hour < 18) {
        printf("Good afternoon!\n");
    } else if (hour >= 18 && hour < 24) {
        printf("Good evening!\n");
    } else {
        printf("Invalid hour entered!\n");
    }

    getch();
}

Output: -
Enter the hour (in 24-hour format): 5
Good morning!

[program terminated] 
Write a program to calculate tax by using else-if statement?
#include <stdio.h>
#include <conio.h>
void main() {
    float income, tax;
    clrscr();
    // Input the income from the user
    printf("Enter your income: ");
    scanf("%f", &income);

    // Calculate the tax based on income
    if (income <= 250000) {
        tax = 0;
	printf(“Tax = %f”, tax);
    } else if (income <= 500000) {
        tax = 0.05 * (income - 250000);
		printf(“Tax = %f”, tax);
    } else if (income <= 1000000) {
        tax = 0.2 * (income - 500000) + 12500;
		printf(“Tax = %f”, tax);
    } else {
        tax = 0.3 * (income - 1000000) + 112500;
		printf(“Tax = %f”, tax);
    }
// Display the calculated tax
    printf("Tax amount: %.2f\n", tax);

    getch();
}
Output: -
Enter your income: 700000
Tax = 22500.000000
[program terminated] 
Write a program to calculate BMI (Body Mass Index) by using else-if statement?
#include <stdio.h>
#include <conio.h>

void main() {
    float weight, height, bmi;
    clrscr();
    // Input weight and height from the user
    printf("Enter your weight (in kilograms): ");
    scanf("%f", &weight);
    printf("\nEnter your height (in meters): ");
    scanf("%f", &height);

    // Calculate BMI (Body Mass Index)
    bmi = weight / (height * height);

    // Categorize BMI
    if (bmi < 18.5) {
        printf("Underweight\n");
    } else if (bmi < 25) {
        printf("Normal weight\n");
    } else if (bmi < 30) {
        printf("Overweight\n");
    } else {
        printf("Obese\n");
    }

    getch();
}
Output: -

Enter your weight (in kilograms): 45.4
Enter your height (in meters): 1.76
Underweight
[program terminated] 

Read more about c programming: –

Data type and Operator in c: – https://xcoode.co.in/c_programming/data-type-and-operators-in-c/

Follow us on: –

Facebook page: – https://www.facebook.com/groups/294849166389180
Quora : – https://xcoode404.quora.com/

By Admin

2 thoughts on “If-else statement in c”

Leave a Reply

Your email address will not be published. Required fields are marked *