If Else Statement – C Programming Language

In C Programming Language sometimes we need evaluate conditions to start run a program or a piece of code. In ‘C’ we use de if else statement, to decide if we run a program or a piece of code based on a condition that we have defined.

If Else Syntax:

if(condition)
   <instruction>;
else
   <instruction>;

 

Example:

#include <stdio.h>
int main(int argc, char** argv) {

    int num = 1;

    if(num==1){
       printf("Number equal to 1");
    }else{
       printf("Number diferent of 1");
    }

    return (EXIT_SUCCESS);
}

Code Explained:

  • In line4, we set the value of the variable num to 1;
  • In line6, we check if the variable num is equal to 1, bellow the logical operator == ;
    • if is equal to 1, it will print “Number equal to 1”.
  • In line8, we say if is not equal to 1, print “Number diferent of 1”.

 

When we use the if else statement 

  1. we set a condition that we want verify;
  2. then if the condition is true (1), we run the program between keys of if statement;
  3. else if the condition is false (0), we run the program between keys of else statement.

Else If Statement

We also can use the else if statement to verify multiple conditions.

Sintax:

if(condition)
   <intruction>;
else if(condition)
   <intruction>;
else
   <intruction>;
#include <stdio.h>
int main(int argc, char** argv) {
    //we set variable num value to 5
    int num = 5;

    if(num==1){
       //we check if num is equal to 1 and print
       printf("Number equal to 1");
    }else if(num==5){
       //we check if num is equal to 5 and print
       printf("Number equal to 5");
    }else{
       //else we print:
       printf("Number diferent of 5");
    }

    return (EXIT_SUCCESS);
}

Logical Operators

Operator Comparisation
== Equal to
>= Greater than or Equal to
<= Less than or Equal to
> Bigger than
< Smaller than
!= Different than

Example Programs

Verify a integer is positive

/*
 *This example we say to user input a integer
 *Then we verify if is positive 
 */
#include <stdio.h>
int main(int argc, char** argv) {

    int num;
    printf("Enter an Integer:\n");
    //to read an integer we use %d, and to store we use &, and store the value on variable that we have defined
    scanf("%d", &num);
    //Verify if the integer is positive
    if(num>0){
       //if is poisitive print:
       printf("Positive Number");
    }else{
       //if isn't positive print:
       printf("Negative Number");
    }

    return (EXIT_SUCCESS);
}

 

Ask for user input two number, and verify which is the largest

/*
 *This example we say to user input a integer
 *Then we 
 */
#include <stdio.h>
int main(int argc, char** argv) {
    //set the variables before use it
    float num1, num2, bigger;

    printf("Enter a Number:\n");
    //to read an integer we use %f, and to store we use &, and store the value on variable that we have defined
    scanf("%f", &num1);

    printf("Enter a Another Number:\n");
    //to read an integer we use %d, and to store we use &, and store the value on variable that we have defined
    scanf("%f", &num2);

    //Verify which number is the largest, and store him one variable 'bigger'
    if(num1>=num2){
       bigger = num1;
    }else{
       bigger = num2;
    }
    
    //Then we print the largest number,
    // in c  we especify the local that we want put the number bellow %f, and that will take the value of variable 'bigger'
    printf("The largest number is %f", bigger);

    return (EXIT_SUCCESS);
}

Leave a Reply

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