if statement in C
In the if statement, 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 of if statement in C:
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.
awesome tutorial
I don understand the if in block…..all the block thing……pls explain to me clearly…pls pls
That is fantastic!
wat is meant by ‘you are in if block’
The statement “you are in if Block” is just an indicator for your reference to indicate that the if condition evaluated to true and the control is inside the IF block.
plz tell me what is the problem in this program.it is not running.
#include
#include
void main()
{
char ch;
clrscr();
printf(“enter the character”);
scanf(“%ch”,&ch);
if (ch==’a’||ch==’e’||ch==’i’||ch||’o’||ch==’u’)
printf(“character is vowel”);
else
printf(“character is consonent”);
getch();
}
@anuj: try this code
#include
#include
void main()
{
char ch;
clrscr();
printf(“enter the character”);
scanf(“%c”,&ch);
if (ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’)
printf(“character is vowel”);
else
printf(“character is consonent”);
getch();
}
i am learning C for the very first time and i don’t know where to programme C,I mean can i run the statements in DOS…please reply me as early as possible.plz plz plz
No. you have to install turbo c on your computer.
please tell me this is fi else statement exap.. or if statement
y dint u include iostream.h and conio.h in anuj’s program?? and y just wrote #include??
plz let me knw d reason
Hello… That is due to some error on this website… the page while rendering on the browser took as a tag and dint display it
Excellent place for a person who has no idea what programming is. Well done!
I love dis site
pls tel me wats is the use of scanf
The use of scanf is to take the input from the programmer
(ch== ‘a’ || ch== ‘e’ || ch== ‘o’|| ch== ‘u’)
use space
and#incude
Awesssome work !!! (y)
Give me reason, why its printing Equal…
main()
{
int a=10;
char b=10;
if(a==b)
printf(“Equal”);
else
printf(“not Equal”);
}
s00rav because u have given the same value in a and b try b=11 then it will not print equal
this pgm is not working
main()
{
char d;
printf(“Enter the character”);
scanf(“%c”,&d);
if(d>=65&&d=97&&d=48&&d<=57)
{
printf("Digits");
}
else
{
printf("Special symbols");
}
}