C Language

Decision and Control Statements

In C Language by Baqir AliLeave a Comment

Decision or control-flow statements specify the order in which computations are performed.

There are two principle control statements:

  • if-else
  • switch

If Statement

The if statement is used to carry out a logical test and then take one of two possible actions, depending on whether the outcome of the test is true or false and here The else portion of the statement is optional.

The simplest possible if-else statement takes the form:

if(expression)
    statement; 

The expression is evaluated:

  • If expression has a nonzero value (i.e., if expression if true), statement is executed.
  • If expression has a value of zero (i.e., if expression is false) then the statement will be ignored.

If-else Statement

The if-else statement is an extension of if used in situations where there are two alternatives:

if(expression)        
     statement1;    
else    
     statement2; 

The expression is evaluated:

  • If expression has a nonzero value (i.e., if expression is true), statement1 is executed.
  • If the expression has a value of zero (i.e., if expression is false) and there is an else part, statement2 is executed.

Example 1:

 /* Find the minimum of two values */
 #include <stdio.h>
 void main(void)
 {
     int x, y, min;
     printf(“Input two integers: “);
     scanf(“%d%d”, &amp;x, &amp;y);
     if (x < y)
        min = x;
     else
        min = y;
     printf(“The minimum value is %d\n”, min);
 } 

Get The Coding Examples From C Language Code Example Page

Leave a Comment