0% found this document useful (0 votes)
10 views8 pages

If Statement in C

Uploaded by

Madhuri Desai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

If Statement in C

Uploaded by

Madhuri Desai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Simple if statement in C

Simple if Syntax

The form of an if statement is as follows:

if(condition) No semicolon after if

statement;
➢ If the condition is true, immediate statement following if is executed

➢ If the condition is false, the statement after if is not 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

1|Page Youtube.com/EngineersTutor www.EngineersTutor.com


if(5)

5 is non-zero and represents TRUE


condion
❖ > (greater than) is relational operator. Note that relational operators return either
true or false
Operator Meaning Example
== Equality 5 == 5 // returns True
!= Not Equal to 5 != 5 // returns False
< Less Than 5 < 5.5 // returns True
<= Less Than or Equal 5 <= 5 // returns True
> Greater Than 5 > 5.5 // returns True
>= Greater Than or Equal 6.3 >= 5 // returns True
Relational operators

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");
}

2|Page Youtube.com/EngineersTutor www.EngineersTutor.com


(iii)
#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");
}

(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");
}

3|Page Youtube.com/EngineersTutor www.EngineersTutor.com


(vi)
#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");
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");
}

4|Page Youtube.com/EngineersTutor www.EngineersTutor.com


(vii) Testing for Leap year
#include<stdio.h>

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

5|Page Youtube.com/EngineersTutor www.EngineersTutor.com


Program 3

Program 4

Program 5

6|Page Youtube.com/EngineersTutor www.EngineersTutor.com


Program 6

Program 7

7|Page Youtube.com/EngineersTutor www.EngineersTutor.com


Program 8

8|Page Youtube.com/EngineersTutor www.EngineersTutor.com

You might also like