SlideShare a Scribd company logo
Conditional Statements
Understanding if, else, switch statements
Introduction
•Conditional statements allow us to make decisions in programs based on conditions.
•It helps control the flow of execution depending on whether a condition is true or false.
Types of Conditional Statements in C
•1. If statement
•2. If-else statement
•3. Nested If-else statement
•4. Switch statement
If Statement
• Syntax:
if (condition) {
// Code to be executed if the condition is true
};
• Example:
int num = 10;if (num > 5) {
printf("Number is greater than 5");
};
If-Else Statement
• Syntax:
• if (condition) { // Code to be executed if condition is true} else { //
Code to be executed if condition is false}
• Example:
int num = 3;if (num > 5)
{ printf("Number is greater than 5");}
else { printf("Number is less than or equal to 5");}
Nested If-Else Statement
• Syntax:
• if (condition1) { if (condition2) { // Code for condition2 } else {
// Code for else condition2 }} else { // Code for else condition1}
• Example:
• int num = 15;if (num > 10) { if (num % 2 == 0) { printf("Number is
even and greater than 10"); } else { printf("Number is odd and greater
than 10"); }}
Switch Statement
• Syntax:
• switch (expression) { case value1: // Code to be executed if expression equals value1 break; case
value2: // Code to be executed if expression equals value2 break; default: // Code to be executed
if expression doesn't match any case}
• Example:
int num = 2;switch (num) {
case 1:printf("Number is 1");
break; case 2: printf("Number is 2");
break;
default: printf("Number is neither 1 nor 2");}
Comparison of If-Else and Switch
•If-Else:
•Flexible and can evaluate complex conditions.
•Suitable for a range of different data types.
•Switch:
•Best for handling multiple discrete values of the same variable.
•More efficient when dealing with multiple case conditions.
Common Errors in Conditional Statements
•Forgotten Break:
•Omitting the break in a switch can cause unwanted fall-through.
•Incorrect Condition:
•Using the wrong operators or logic in conditions.
•Unreachable Code:
•Placing code after a return or break in the wrong places.
Conclusion
•Summary:
•Conditional statements are crucial for making decisions in C programs.
•We have learned about if, if-else, nested if-else, and switch statements.
•Takeaway:
•Proper usage of conditional statements helps control the flow of logic based on
conditions.
Conditional Statements(Unlocking decissions in code).pptx

More Related Content

PPTX
Unit_3A_If_Else_Switch and a if else statment example
PPTX
Introduction to Conditional Statements.pptx
PPTX
DECISION MAKING AND BRANCHING - C Programming
PPTX
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
PPTX
IF & SWITCH (conditional control) in computer science
PDF
Programming in C Conditional statements.pdf
PPTX
Condition Stmt n Looping stmt.pptx
PPTX
Java Control Statements
Unit_3A_If_Else_Switch and a if else statment example
Introduction to Conditional Statements.pptx
DECISION MAKING AND BRANCHING - C Programming
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
IF & SWITCH (conditional control) in computer science
Programming in C Conditional statements.pdf
Condition Stmt n Looping stmt.pptx
Java Control Statements

Similar to Conditional Statements(Unlocking decissions in code).pptx (20)

PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PDF
w=iu006m2m=a9'[5n4.pdfhhhhhhhhhhhjjhhjjj
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
conditional.pptx
PDF
Loops and conditional statements
PDF
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
PPTX
Conditional statement in c
PPTX
SWITCH-CASE, Lesson Computer Programming.pptx
PPT
If-else and switch-case
PPTX
CONDITIONAL STRUCTURE(IF,IF-ELSE,ELSE-IF)COM.pptx
DOCX
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
PDF
PPT
4b C switch structure .ppt
PPT
control-statements....ppt - definition
PPTX
Unit-2 control Structures.pptx.pptx20201
PPTX
basics of python - control_structures_and_debugging.pptx
PPTX
Final project powerpoint template (fndprg) (1)
PPTX
Switch case.pptx
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
w=iu006m2m=a9'[5n4.pdfhhhhhhhhhhhjjhhjjj
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
conditional.pptx
Loops and conditional statements
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
Conditional statement in c
SWITCH-CASE, Lesson Computer Programming.pptx
If-else and switch-case
CONDITIONAL STRUCTURE(IF,IF-ELSE,ELSE-IF)COM.pptx
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
4b C switch structure .ppt
control-statements....ppt - definition
Unit-2 control Structures.pptx.pptx20201
basics of python - control_structures_and_debugging.pptx
Final project powerpoint template (fndprg) (1)
Switch case.pptx
Ad

Recently uploaded (20)

PPTX
introduction to high performance computing
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
Occupational Health and Safety Management System
PDF
737-MAX_SRG.pdf student reference guides
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Information Storage and Retrieval Techniques Unit III
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PPTX
Current and future trends in Computer Vision.pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
introduction to high performance computing
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Occupational Health and Safety Management System
737-MAX_SRG.pdf student reference guides
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Information Storage and Retrieval Techniques Unit III
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Current and future trends in Computer Vision.pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Exploratory_Data_Analysis_Fundamentals.pdf
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Abrasive, erosive and cavitation wear.pdf
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
Automation-in-Manufacturing-Chapter-Introduction.pdf
Ad

Conditional Statements(Unlocking decissions in code).pptx

  • 2. Introduction •Conditional statements allow us to make decisions in programs based on conditions. •It helps control the flow of execution depending on whether a condition is true or false.
  • 3. Types of Conditional Statements in C •1. If statement •2. If-else statement •3. Nested If-else statement •4. Switch statement
  • 4. If Statement • Syntax: if (condition) { // Code to be executed if the condition is true }; • Example: int num = 10;if (num > 5) { printf("Number is greater than 5"); };
  • 5. If-Else Statement • Syntax: • if (condition) { // Code to be executed if condition is true} else { // Code to be executed if condition is false} • Example: int num = 3;if (num > 5) { printf("Number is greater than 5");} else { printf("Number is less than or equal to 5");}
  • 6. Nested If-Else Statement • Syntax: • if (condition1) { if (condition2) { // Code for condition2 } else { // Code for else condition2 }} else { // Code for else condition1} • Example: • int num = 15;if (num > 10) { if (num % 2 == 0) { printf("Number is even and greater than 10"); } else { printf("Number is odd and greater than 10"); }}
  • 7. Switch Statement • Syntax: • switch (expression) { case value1: // Code to be executed if expression equals value1 break; case value2: // Code to be executed if expression equals value2 break; default: // Code to be executed if expression doesn't match any case} • Example: int num = 2;switch (num) { case 1:printf("Number is 1"); break; case 2: printf("Number is 2"); break; default: printf("Number is neither 1 nor 2");}
  • 8. Comparison of If-Else and Switch •If-Else: •Flexible and can evaluate complex conditions. •Suitable for a range of different data types. •Switch: •Best for handling multiple discrete values of the same variable. •More efficient when dealing with multiple case conditions.
  • 9. Common Errors in Conditional Statements •Forgotten Break: •Omitting the break in a switch can cause unwanted fall-through. •Incorrect Condition: •Using the wrong operators or logic in conditions. •Unreachable Code: •Placing code after a return or break in the wrong places.
  • 10. Conclusion •Summary: •Conditional statements are crucial for making decisions in C programs. •We have learned about if, if-else, nested if-else, and switch statements. •Takeaway: •Proper usage of conditional statements helps control the flow of logic based on conditions.