If Statement in C
If Statement in C
Simple if Syntax
statement;
➢ If the condition is true, immediate statement following if is executed
If multiple statements are to be executed after if, we must include them in curly braces
if (condition)
{
statement 1;
statement 2;
…………..
…………..
.
.
.
statement n;
}
➢ If the condition is true, block of statements (called compound statement) inside if is executed
➢ If the condition is false, block of statements (called compound statement) inside if is not executed
TRUE
condition
FALSE
In C/C++,
1. ZERO represents FALSE condition
2. Non-zero represents TRUE condition
Examples of non-zero values 5, -5.1, 100, -206 etc
Note that every operator in C++ must return some value. For example, + operator returns sum of two
numbers, * operator return multiplication of two numbers etc.
Practice Programs
(i)
#include<stdio.h>
void main()
{
system("color fc");
// e = = light yellow = Output window background color
//c = = Light red = Output window text color
int x = 5;
if(x)
printf("EngineersTutor.com")
}
(ii)
#include<stdio.h>
void main()
{
system("color ec");
// e = = light yellow = Output window background color
//c = = Light red = Output window text color
int x = 5;
if(x>10)
printf("EngineersTutor.com");
}
int x = 5;
if(x == 10)
printf("EngineersTutor.com");
}
(iv)
#include<stdio.h>
int main()
{
system("color ec");
// e = = light yellow = Output window background color
//c = = Light red = Output window text color
int x = 5, y = 10;
if(x+y)
printf("EngineersTutor.com");
}
(v)
#include<stdio.h>
void main()
{
system("color ec");
// e = = light yellow = Output window background color
//c = = Light red = Output window text color
int x = 5, y = 10;
if( (x+y)>30 )
printf("EngineersTutor.com");
}
void main()
{
system("color ec");
// e = = light yellow = Output window background color
//c = = Light red = Output window text color
int x = 5, y = 10;
if( (x+y)>30 )
{
printf("EngineersTutor.com");
printf("Teach Easy");
}
}
(vii)
#include<stdio.h>
void main()
{
system("color ec");
// e = = light yellow = Output window background color
//c = = Light red = Output window text color
int x = 5, y = 10;
if( (x+y)<30 )
printf("EngineersTutor.com\n");
printf("Teach Easy\n");
printf("Albert\n");
printf("Stephen");
}
void main()
{
int year;
printf("enter year \n");
scanf("%d", &year);
if((year%400==0)||((year%4==0)&&(year%100!=0)))
printf("given year is leap year \n");
else
printf("not leap year \n");
}
Program explanation
Program 1
Program 2
Program 4
Program 5
Program 7