The if statement in C Programming Language



Here, if is the keyword in C.
Syntax:
if (condition is true){
/*block of statements to be executed*/
}
The keyword if tells the compiler that what follows is decision control statement. The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if statement using an example.

Example:

int a = 10;
if (a == 10){
printf(“You are in if block\n”);
printf(“The value of a is %d\n”, a);
}
printf(“You are out of if block”);

In the above example we have assigned value 10 to a. In the next line, there is an if statement. It is read as “If a is equal to 10 then perform the block of statements enclosed within the braces”. After the execution of this block normal sequence flow of the program is executed.
The output of the above example is:
You are in if block
The value of a is 10
You are out of if block


Lets have a look at different expressions that can be used in if statement.

a == b read as a is equal to b
a!=b read as a is not equal to b
a < b read as a is less than b a > b read as a is greater than b
a<=b read as a is less than or equal to b a>=b read as a is greater than or equal to b

Example:

/*Demonstration of if statement*/
int marks;
printf(“Enter the marks:”);
scanf(“%d”, &marks);
if(marks>=35){
printf(“Congrats!!!”);
}

In the above program, it will prompt you to enter the marks. If you enter marks greater than or equal to 35, it will display Congrats!!! on the screen else it will do nothing.


Responses

0 Respones to "The if statement in C Programming Language"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy