This blog is under construction

Saturday, 21 April 2012

Control statements - if-else Statement

Syntax for if-else statement:

if (expression) {
     <IF BLOCK>
} else {
     <ELSE BLOCK>
}

If the truth value of the expression is evaluated to be true, then the statements present inside the IF BLOCK would get executed.  Otherwise, the statements present inside ELSE BLOCK would get executed.

Example program for if-else statement:
  #include <stdio.h>
  int main() {
        int data;
        printf("Enter a number:");
        scanf("%d", &data);
        // check whether the value of data is greater than 100
        if (data > 100) {
                // if block
                printf("Entered no is greater than 100\n");
        } else {
                // else block
                printf("Entered no is not greater than 100\n");
        }
        return 0;
  }


  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter a number:156
  Entered no is greater than 100



No comments:

Post a Comment