If Statement in C Programming

There are several decision making statements supported by C Language. If Statement in C programming has three different forms, if Statement, the if-else statement, else-if statement. Also we’ll cover switch statement. The if keyword tells the compiler that it is decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. Let’s look at syntax of each form of if statement for implementation:

If Statement in C Programming

The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If not true, execute this instruction.

If Statement Syntax
if (TRUE)
    Execute this line of code

Let’s take a look at example code:

int main()
{
    int mynumber;
    scanf("%d",&mynumber); 

    if (mynumber == 10)
        printf("Entered Number is equal\n");

    return 0;
}

Here is an output from project:

Output of If Statement in C Program
Output of If Statement in C Program

In the example above the user can input a number. The number is stored in the variable mynumber. Now take a look at the “if statement”: if the number stored in the variable mynumber is equal to ten, then print “Entered Number is equal” on the screen. Simple, isn’t it. If the number is not equal to ten, then nothing gets printed.

Now take another look at the “if statement”: look at the placement of the semi-colon. As you can see there is no semi-colon behind the “if statement”. If there is just one instruction (if the statement is true), you can place it after the “if statement” (with an indentation). When multiple instructions necessary to execute then we will have to use curly brackets, like so (syntax):

if (TRUE) {
    /* between the braces is the body of the if statement */
    Execute all statements inside the body
}

I recommend always putting braces following if statements. If you do this, you never have to remember to put them in when you want more than one statement to be executed, and you make the body of the if statement more visually clear. The modified program will look something like this:

#include<stdio.h>
int main()
{
    int mynumber;
    scanf("%d",&mynumber);
    
    if (mynumber == 10){
         printf("Entered Number is equal\n");
         printf("closing program !\n")
    }
    return 0;
}

learn-embedded-system


If-Else Statement

Sometimes when the condition in if statement evaluates to false, it would be nice to execute some code. The “else” statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.

If-Else Syntax
if (TRUE){
   /* Execute these statements if TRUE */
}
else{
   /* Execute these statements if FALSE */
}

Let’s take a look at an example. We will continue to modify previous example program. Now we like to print something if the “if statement” is not equal. We could do this by adding another “if statement” but there is an easier/better way. Which is using so called “else statement” along with “if statement”. Here is an example:

Example Program: If-Else Statement
#include<stdio.h>

int main()
{
    int mynumber;
    scanf("%d",&mynumber);

    if ( mynumber == 10 )
    {
        printf("Entered Number is equal\n");
    }
    else
    {
        printf("Entered Number is Not equal\n");
    }
    return 0;
}

Else-If statement

Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement’s body to execute. You can use an “else if” statement following an if statement and its body; that way, if the first statement is true, the “else if” will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is to be executed.

Let’s look at a simple program for you to try out on your own.

#include <stdio.h>

int main()
{
    int age;
    printf( "Please enter your age " );
    scanf( "%d", &age );

    if ( age < 100 ) {
        printf ("You are pretty young!\n" );
    }
    else if ( age == 100 ) {
        printf( "You are old\n" );
    }
    else {
        printf( "You are really old\n" );
    }
}

Here is an output from project:

Output of else-if in C Program
Output of else-if in C Program

Switch Statement

The switch statement is almost the same as an “if statement”. The switch statement can have many conditions. You start the switch statement with a condition. If one of the variable equals the condition, the instructions are executed. It is also possible to add a default. If none of the variable equals the condition the default will be executed. Let’s have a look at example code:

Example Program: Switch Case
#include <stdio.h>

int main () {

   /* local variable definition */
   char grade;
   scanf("%c", &grade);

   switch(grade) {
      case 'a' :
         printf("Excellent!\n" );
         break;
      case 'b' :
      case 'c' :
         printf("Well done\n" );
         break;
      case 'd' :
         printf("You passed\n" );
         break;
      case 'e' :
         printf("Better try again\n" );
         break;
      default :
         printf("Invalid grade\n" );
   }

   printf("Your grade is  %c\n", grade );
   return 0;
}

Note: “break” is used to exit the switch case.

Output of Switch Case in C Program
Output of Switch Case in C Program

In the example above the user can input a character. The character is stored in the variable grade. Now take a look at the “switch statement”: if the character stored in the variable grade is equal to ‘a’, then print “Excellent!” on the screen. Simple, isn’t it. If the character is not equal to any ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ then default code get executed and print “Invalid grade” on screen.

We encourage you to make your own programs or to modify existing programs. This is the only way you can learn C Programming. This is all for If Statement in C Programming. In next post we will learn about loops in C Programming Language. I hope you will find this post educational. Feel free to leave a comment.

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?