C if-else Statements (JNNC Technologies)


If else statements in C is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if the expression is evaluated to true, otherwise executes else statement code block.
The basic format of if else statement is:
Syntax:
if(test_expression)
{
   //execute your code
}
else
{
   //execute your code
}
Figure – Flowchart of if-else Statement:
c-if-else

Example of a C Program to Demonstrate if-else Statement

 Example:
#include<stdio.h>

main()
{
  int a, b;

  printf("Please enter the value for a:");
  scanf("%d", & amp; a);

  printf("\nPlease the value for b:");
  scanf("%d", & amp; b);

  if (a & gt; b) {  
    printf("\n a is greater");
  } else {  
    printf("\n b is greater");
  }
}
Program Output:
c-if-else statement 1
Example:
#include<stdio.h>

main() {
  int num;
  printf("Enter the number:");
  scanf("%d", num);

  /* check whether the number is negative number */
  if (num < 0)
    printf("The number is negative.");
  else
    printf("The number is positive."); 
}
Program Output:
c-if-else-statement 2


Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();