IF ELSE

IF ELSE STATEMENT

if...else is a branching statement. It is used to take an action based on some condition. For example - if user inputs valid account number and pin, then allow money withdrawal.
If statement works like "If condition is met, then execute the task". It is used to compare things and take some action based on the comparison. Relational and logical operators supports this comparison.
C language supports three variants of if statement.
  • Simple if statement
  • If else if statement
  • Nested if else statement
As a programmer you must have a good control on program execution flow. In this exercise we will focus to control program flow using if...else statements.

Syntax of if statement

if(boolean_expression)
{
    // body of if
}

Example program of if statement

Let us write our first program based on conditions. Write a program to input user age and check if he is eligible to vote in India or not. A person in India is eligible to vote if he is 18+
/** * C program to check if a person is eligible to vote or not. */ #include <stdio.h> int main() { /* Variable declaration to store age */ int age; /* Input age from user */ printf("Enter your age: "); scanf("%d", &age); /* Use relational operator to check age */ if(age >= 18) { /* If age is greater than or equal 18 years */ printf("You are eligible to vote in India."); } return 0; }

If…else and if…else…if statement

Simple if works as "if some condition is true then do some tasks". It doesn't specifies what to do if condition is false. A good program must think both ways. For example - if user inputs correct account number and pin, then allow money withdrawal otherwise show error message to user.
if...else statement is an extension of simple if. It works as "If some condition is true then, do some task otherwise do some other task".

Syntax of if...else statement

if(boolean_expression)
{
    // Body of if
    // If expression is true then execute this
}
else
{
    // Body of else
    // If expression is false then execute this
}
In above syntax if the given Boolean expression is true then, execute body of if part otherwise execute body of else part. In any case either body if or body of else is executed. In no case both the blocks will execute.

Example of if...else statement

Let us write program based on if...else statement. Write a program to input two numbers from user. Print maximum between both the given numbers.
/**
 * C program to find maximum between two numbers
 */

#include <stdio.h>

int main()
{
    /* Declare two integer variables */
    int num1, num2;

    /* Input two number from user */
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);

    /* Compare both number using relational operator */
    if(num1 > num2)
    {
        /* If first number is greater than second */
        printf("First number is maximum.");
    }
    else
    {
        /* If first number is not greater than second */
        printf("Second number is maximum.");
    }

    return 0;
}
Output of the above program
Enter two numbers: 10
20
Second number is maximum



0 Comments:

Post a Comment