The if statement

The if statement is used to specify conditional execution of program statement, or a group of statements enclosed in braces. The general format of the statement is

if (condition)
{
   program statements;
}

When an if statement is encountered, condition is evaluated and if its value is true .the statement is executed. The if statement execution can be understood by going through the following program

Consider the problem of finding out the biggest among two numbers. The problem decision, that is, the tasks include

  • Getting the input (the two numbers)
  • Reading their values
  • Assuming that first number is the biggest. at the first stage
  • If the first number is not bigger, then the second one should be selected as the biggest.
  • Displaying the outcome of the decision

The following chart illustrates the working of the if construct.

Example 2.2.

The following example demonstrates how the greatest among (wo numbers is determined using the if-construct.

#include <stdio.h>
int main()
{
    int value1, value2, big;
    printf ( "Enter the number values");
    scanf ("%d%d ", &value1, &value2);
    big = value1; 
    if (valuel1< value2) 
       big = value2; 
       printf ( "The bigger number is %d"" , big) ;
}