}

Learn programmign: How to write If else statement in C Program

Created:

Introduction

Knowning how to program in C is a good desicion. C is a very old lenguage but it is still used. One of the first things you may need to known is how to code an if else statement in C.

We will explain how to program an if else with modern C.

Learn if and else syntax

If else syntax is widely standarized and it should be similar to this example:

if ( boolean_expresion ) {
   printf("Tutorials! boolean expression was TRUE");
}
else  {
   printf("Tutorials! boolean expression was False");
}

When boolean_expresionn is TRUE, the program will print "Tutorials! boolean expression was TRUE".
If given condition is FALSE (not TRUE), the program will print "Tutorials! boolean expression was FALSE"

Example!

In the next example we will ask the user to enter the age. If the age is bigger than 21 it will print the message allowing them to buy beer! clrscr is used to clear the screen. scanf is used to get the input from the user. %d refer to read the input type integer. Scanf required a point (&x) to the variable to write what the user typed.

#include <stdio.h>
int main (){
    clrscr();
    int x;
    printf("Enter the age :");
    scanf("%d",&x);
    if( x > 21 ){
        printf("You are older than 21, you can buy beer!");
    }
    else {
        printf("You are not allowed to buy any alcoholic drinks");
    }
    return 0;
}

Finally we return 0 since program execution was successfully.

Another example: how to tell is the number is odd or even using C

First we clear the screen with clrscr, then we use scanf to ask the user for the input. In the if statement use the modulo function (%) which will return 0 when the number is divisible by 2. For example n % 3 will return 0 when n is 6,12 or n is divisible by 3. Modulo function returns the remainder of the division.

#include <stdio.h>
void main()
{
    int n;
    clrscr();
    printf(“Enter the number:”);
    scanf(“%d”,&n);
    if (n%2==0) {
        printf(“the number is even”);
    } else {
        printf(“the number is odd”);
    }
    getch();
}

Finally getch() is used to get a character from the console but does not echo to the screen.

Now to give a more complex example we will iterate on the first n numbers and print which ones are odd or even. The variable n will be a user input. As always we first clear the screen with clrscr(), then we ask the user for the input.

void main()
{
    int i,n;
    clrscr();
    printf("Enter the level\n");
    scanf("%d",&n);
    for(i=0; i<n; i++) {
        if(i % 2==0) {
            printf("even number=%d \n",i);
        } else {
            printf("odd number=%d \n",i);
        }
    }
    getch();
}

You will find that the body of the for is almost equal to the previous example. The for will just execute the if statement n times in the the condition i < n is met.

Advices when using if statement

A common error with if statement is when conditions overlap or there are gaps in the cases.

Let's see an example that should print the correct grade. The code should print your grade with the following logic:

  • more then 75 you are grade-A.
  • between 60 and less then 75 grade-B.
  • between 50 and less then 60 grade-C.
  • between 40 and less then 50 grade-E.
  • less than 40 grade-E.
#include #include
void main(){
    int i;
    clrscr();
    printf(“Enter your total marks”);
    scanf(“%d”,&i);
    if(i>=75){ 
        printf(“Grade-A”);
    }
    if(i>=60){
        printf(“Grade-B”);
    }
    if(i=50){
        printf(“Grade-C”);}
    if(i=40){
        printf(“Grade-D”);
    }
    if(i<40){
        printf("Fail");
    }
    getch();
}

The code has the following problems:

  • For score bigger than 75 it will print "Grade-A" and "Grade-B".
  • For several score it will not print anything, for example with 65.

The proper code is the next one:

#include #include
void main(){
    int i;
    clrscr();
    printf(“Enter your total marks”);
    scanf(“%d”,&i);
    if(i>=75){ 
        printf(“Grade-A”);
    }
    if(i>=60 && i<75){
        printf(“Grade-B”);
    }
    if(i>=50 && i<60){
        printf(“Grade-C”);}
    if(i>=40 && i<50){
        printf(“Grade-D”);
    }
    if(i<40){
        printf("Grade-E");
    }
    getch();
}

Another proper way to write the code above is to use elseif. When you use else if with C, you know that after the else the previous statement if false. Let's see the same example using else if

#include #include
void main(){
    int i;
    clrscr();
    printf(“Enter your total marks”);
    scanf(“%d”,&i);
    if(i>=75){ 
        printf(“Grade-A”);
    }
    else if(i>=60){ //since we use else we know that i<75
        printf(“Grade-B”);
    }
    else if(i>=50){ 
        printf(“Grade-C”);}
    else if(i>=40){
        printf(“Grade-D”);
    }
    else { //we don't need to write the condition since all above is false
        printf("Grade-E");
    }
    getch();
}

As you can see the conditions on the else if are simple since we know that the previous conditions are false. The last condition is not neccesary since all previous are false.

Remeber to always test your code!