0% found this document useful (0 votes)
49 views11 pages

Conditional Statements in C

C program

Uploaded by

ARX Gaming
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)
49 views11 pages

Conditional Statements in C

C program

Uploaded by

ARX Gaming
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

DECISION MAKING & BRANCHING

(if-else)

Md. Mazbaur Rashid


Lecturer,
Department of Software Engineering
Introduction
A c program is a set of statements which are normally
executed sequentially. But in practice, we may have a
number of situations where we may have to change the
order of execution of statements depending on certain
conditions, or repeat a group of statements until certain
specific condition are met. Here the decisions making
comes where the computer can be directed to execute a
certain statements based on a particular condition.
Decision-Making Statements

C language supports following statements

if statement
switch statement
Conditional operator statement
goto statement
if statement

if statement is used to control the flow of execution of


statements. If statement can be used in different forms.

Simple if statements
if-else statements
Nested if-else statements
else-if ladder
Different forms of if statements
if statements Nested if-else statements else-if ladder
if(condition) if(condition) if(condition-1)
{ { {
Statements; if(condition) Statements;
} Statements; }
else if(condition-2)
else {
if-else statements Statements; Statements;
} }
if(condition) else if(condition-3)
else
{ {
{
Statements; Statements;
Statements;
} }
}
else else
{ {
Statements; Statements;
}
}
Relational and Logical Operators (Recap)
Relational Operator Logical Operator
Meaning Operator Details

Equal to == (P == Q) is not true


Meaning Operator
Not equal to != (P != Q) is true
Logical AND &&
Less than < (P < Q) is not true

Greater than > (P > Q) is true Logical OR ||


Less than or equal to <= (P <= Q) is not true
Logical NOT !
Greater than or equal to >= (P >=Q) is true
Examples
if Statements

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int mark = 85; int number = 5;

if(mark >= 80) if(number > 0)


{ {
printf("This is a Positive Number!\n");
printf("Grade: A+\n");
}
}
return 0;
return 0;
}
}
Examples
if-else Statements

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int mark = 74; int number = -5;

if(mark >= 80) if(number > 0)


{ {
printf("Grade: A+\n"); printf("This is a Positive Number!\n");
} }
else else
{ {
printf("Not 'A+'\n"); printf("This is not a Positive Number!\n");
} }
return 0; return 0;
} }
Examples
Nested if-else Statements
#include<stdio.h>
int main()
{
int number = 9;

if(number > 0)
{
if(number % 2 == 0)
printf("This number is Positive and also Even\n");
else
printf("This number is Positive but Odd\n");
}
else
printf("This is neither Positive nor Even number!\n");
return 0;
}
Examples
else-if ladder
#include<stdio.h> else if(day == 4)
printf("Tuesday");
int main() else if(day == 5)
{ printf("Wednesday");
int day = 7; else if(day == 6)
} printf("Thursday");
if(day == 1) else if(day == 7)
printf("Saturday"); printf("Friday");
else if(day == 2) else
printf("Sunday"); printf("Invalid");
else if(day == 3)
printf("Monday"); return 0;
}
Thank You

You might also like