C Programming if Statement

C Programming IF Statement

C Programming if Statement

Table of Contents

Conditional statements are often used in programming to make decisions. In the chapter you will learn how to makes decisions using if conditional statements in C programming.

C Programming IF Statement

There are three types if conditional statement in C programming:

          1.   if-if                                                                                                                                  2.   if…else-if…else                                                                                                                3.   if…elseif…else-if…elseif…else

C IF Statement Syntax

				
					if (testExpression)
{
 // This code will execute.
}
				
			

Here if first evaluetes test Expression.

If test Expression is True (not null) then if the statement/statements inside the block will be executed.

If test Expression is False (null), if the statement/statements within the block will be skipped.

Wen the value of test Expression is True and when it is False is discussed in our relational and logical operators page.

Example: C if Statement

				
					// Program to display positive numbers on screen when user inputs them.
//User input negative number will not be displayed on the screen.

#include <stdio.h>
int main()
{
 int testNumber;

 printf("Enter an unsigned integer: ");
 scanf("%d", &testNumber);

 //If the value of testNumber is greater than 0 then the Test expression will be True.
 if (testNumber > 0)
 {
 printf("You have entered %d.\n",testNumber);
 }

 printf("The if statement is easy in C programming.");

 return 0;
}
				
			
Output 1
				
					Enter an unsigned integer: 5
You entered 5.
The if statement is easy in C programming.
				
			

(testNumber > 0) The test expression return True when the user enters 5. S your entered 5 will be displayed.

Output 2
				
					Enter an integer: -6
The if statement is easy in C programming.

				
			
c programming continue statement

(testNumber < 0) The test expression returns False when the user enters -6. So the compiler if skips the statement inside it.

Here is another example to better understand the if statement:

				
					#include <stdio.h>

int main()
{
 int i;

 /* this is always true */
 if (1) {
 printf ("This should be printed.\n");
 }
 /* This is never true */
 if (0) {
 printf ("This should not be printed.\n");
 }
 /* we can also use "else" */
 if (0) {
 printf ("This should not be printed.\n");
 }
 else {
 printf ("This 'else' part should be printed\n");
 }

 i = 1;

 /* One can optionally not use { and } when there is only one statement. */

 if (i == 1){
 printf ("This is a branch without the braces { and }.\n");
 }


 /* be careful about the equal(=) sign? */

 if (i = 100){
 printf ("Beware of equal signs in if statements.\n");
 }


 return 0;
}
				
			
output
				
					This should be printed.
This 'else' part should be printed.
This is a branch without the braces { and }.
Beware of equals signs in if statements.

				
			

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *