0% found this document useful (0 votes)
5 views

Conditional Statements

Uploaded by

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

Conditional Statements

Uploaded by

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

Conditional

Statements
Using If and else
• The conditional statements (also known as decision
control structures) such as if, if else, switch, etc. are
used for decision-making purposes in C programs.
• Conditional statements are essential for creating
programs that can make decisions and execute different
code blocks based on specific conditions.
Need for conditional statements
• Decision-Making: Computers need to make decisions
based on different conditions.
• Flexibility: Conditional statements allow programs to
adapt to various input values and situations.
• Control Flow: They control the order in which
instructions are executed.
Means to implement
conditional structures

Using Conditional
Using If-Else Using Switch
operator
Simple if

Conditional Statements using If-else


If-else

Nested if

Else if ladder
Simple if
• Syntax • Explanation
• If the condition evaluates
if (condition) { to true, the code block is
executed.
// block of code
• If false, the program skips
} the block.
Example
#include <stdio.h>
int main() {
int x = 10;
// if statement to check if x is greater than 5
if (x > 5) {
printf("x is greater than 5\n");
}
printf(“program ended”);
return 0;
}
if-else
• Syntax • This is to implement two-
way selection
if (condition) {
// block of code if true
} else {
// block of code if false
}
Example
#include<stdio.h>
int main()
{
int x = 3;
// if statement to check if x is greater than 5
if (x > 5) {
printf("x is greater than 5\n");
}
else{
printf("x is less than 5\n");
}
printf("program ended\n");
return 0;
else-if ladder
• Syntax • Else-if ladder is used to
implement multiway selection
if (condition1) {
• Condition 1 is checked first
// code if condition1 is true and if it fails condition 2 is
} else if (condition2) { checked
// code if condition2 is true • Else block will be
implemented when both the
} else { conditions fail
// code if all conditions are • One can extend this for
false checking any number of
} conditions
Example
#include<stdio.h>
int main(){
int x = 8;
if (x > 10) {
printf("x is greater than 10");
} else if (x > 5) {
printf("x is greater than 5 but less than or equal to 10");
} else {
printf("x is less than or equal to 5");
}
}
Nested-if
• Syntax
if (condition1) {
if (condition2) {
// code if both condition1 and condition2 are true
}
}
else You can place an if statement inside another if block
{ Enabling more complex decision-making.
if (condition 3)
{
//Code to implemented when condition 1 is false but
3 is true
}
}
#include<stdio.h>
Int main(){
int x = 10;
if (x > 5) {
if (x < 15) {
printf("x is between 5 and 15");
}
else{
printf("x is greater than 15");
}
}
else {
if(x>3){
printf("x is between 3 and 5");
}
else{
printf("x is less than 15");
}
}
}
Using Logical operators to reduce
the code
• Instead of using nested if
if (x > 5 && x < 15) {
printf("x is between 5 and 15");
}
However, if the statement is false, we can not guarantee
that x is less than 5.
As x may be less than 5 or greater than 5.
#include<stdio.h>
Int main(){
int x = 10;
if (x > 5 && x<15) {
printf("x is between 5 and 15");
}
else if(x>15){
printf("x is greater than 15");
}
else if(x>3)
{
printf("x is between 3 and 5");
}
else{
printf("x is less than 3");
}
}

You might also like