C Programming if, if..else and Nested if...else Statement

Sunday, May 10, 2015

The if, if...else and nested if...else statement are used to make one-time decisions in C Programming, that is, to execute some code/s and ignore some code/s depending upon the test expression.

C if Statement

if (test expression) {
       statement/s to be executed if test expression is true;
}
The if statement checks whether the text expression inside parenthesis () is true or not. If the test expression is true, statement/s inside the body of if statement is executed but if test is false, statement/s inside body of if is ignored.

Flowchart of if statement


Example 1: C if statement

Write a C program to print the number entered by user only if the number entered is negative.

#include <stdio.h>
      int main(){
      int num;
      printf("Enter a number to check.\n");
      scanf("%d",&num);
      if(num<0) {      /* checking whether number is less than 0 or not. */ 
            printf("Number = %d\n",num); 
      }  
/*If test condition is true, statement above will be executed, otherwise it will not be executed */
      printf("The if statement in C programming is easy.");
return 0;
}

1 comments:

felisha green said...

Are you willing to know who your spouse really is, if your spouse is cheating just contact cybergoldenhacker he is good at hacking into cell phones,changing school grades and many more this great hacker has also worked for me and i got results of spouse whats-app messages,call logs, text messages, viber,kik, Facebook, emails. deleted text messages and many more this hacker is very fast cheap and affordable he has never disappointed me for once contact him if you have any form of hacking problem am sure he will help you THANK YOU.
contact: cybergoldenhacker at gmail dot com

Post a Comment