W7Code » C Language » 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:

 

Example:

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:

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

 

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

Leave a Reply