SlideShare a Scribd company logo
C PROGRAMMING
Explore Now
UNIT-2
DECISION MAKING AND BRANCHING
• 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.
• They are also known as Decision-Making Statements and are used to evaluate one or
more conditions and make the decision whether to execute a set of statements or not.
These decision-making statements in programming languages decide the direction of the
flow of program execution.
Introduction to Decision
Making:
Types of Conditional
Statements:
1.if Statement
2.if-else Statement
3.Nested if Statement
4.if-else-if Ladder
5.switch Statement
6.Conditional Operator
7.Jump Statements
if
Statement:
An if statement is like asking a computer a question.
• If the answer is "yes" (true), it does one thing.
• If the answer is "no" (false), it does something else or nothing at all.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
if else
statement:
An if-else statement provides two possible execution paths based on a condition.
• If the condition is true, the code inside the if block is executed.
• If the condition is false, the code inside the else block is executed.
Syntax:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Nested if
statement:
A nested if statement is an if statement within another if statement. This allows for more complex
decision-making processes.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if both condition1 and condition2 are true
} else
{
// code to be executed if condition1 is true but condition2 is false
}
} else {
// code to be executed if condition1 is false
}
if-else-if
ladder:
An if-else-if ladder is used when you have multiple conditions to check and want to execute different code blocks
based on those conditions.
It's like a series of if statements connected together. The conditions are checked sequentially, and as soon as one
condition is true, the corresponding code block is executed, and the rest of the ladder is skipped.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
// code to be executed if condition1 and condition2 are false, but condition3 is true
} else {
// code to be executed if
none of the conditions are true
}
A switch statement is a multi-way branch statement that allows you to select one of many code blocks to
be executed. It's often used as an alternative to multiple if-else statements when you have a variable with a
limited number of possible values.
Syntax:
C
switch (expression) {
case constant1:
// code to be executed if expression == constant1break;
case constant2:
// code to be executed if expression == constant2break;
// ...default:
// code to be executed
if no case matches
}
Switch statement:
Conditional
Operator:
The conditional operator, often called the ternary operator, is a shorthand way to express an
if-else statement in a single line. It's represented by the question mark (?) and colon (:).
Syntax:
condition ? expression1 : expression2
Example:
int x = 10, y = 20, max;
max = (x > y) ? x : y;
Jump
Statement:
Jump statements alter the normal flow of a program's execution. They allow you to transfer
control to a different part of the code.
Types of Jump Statements
1. Break Statement
• Exits the current loop or switch statement immediately.
• Commonly used to terminate a loop when a specific condition is met.
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
2. Continue Statement
• Skips the rest of the current iteration of a loop and jumps to the next iteration.
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
3. Return Statement
• Terminates the current function and returns a value (optional).
int add(int a, int b) {
return a + b;
}
4. Goto Statement
• Transfers control to a labeled statement within the same function.
• Generally discouraged due to potential for creating hard-to-follow code.
#include <stdio.h>int main() {
int i = 0;
loop:
printf("%d ", i);
i++;
if (i < 5) {
goto loop;
}
return 0;
}
Looping
Statement:
Looping statements in programming allow you to execute a block of code repeatedly until a
certain condition is met. They are essential for automating repetitive tasks and performing
calculations iteratively.
Types of Loops
There are primarily three types of loops in most programming languages:
1. For Loop
• Used when you know the number of iterations in advance.
Syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
2.While
loop:
• Used when the number of iterations is unknown, but you have a condition to check
before each iteration.
Syntax:
while (condition) {
// code to be executed
}
3.do-while
loop:
• Similar to a while loop, but the condition is checked after the
loop body is executed at least once.
Syntax:
do {
// code to be executed
} while (condition);
Jump in
loop:
Jump statements can be used to modify the normal flow of execution within loops. The two
primary jump statements used in loops are break and continue.
Break Statement
• Terminates the loop entirely.
• Once encountered, the program exits the loop immediately and continues execution after
the loop.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
Continue Statement
• Skips the rest of the current iteration.
• When encountered, the program jumps to the beginning of the next iteration of the loop.
Example:
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
10
Thank
You

More Related Content

PPT
2. Control structures with for while and do while.ppt
PPTX
Control statements in java
PPT
control-statements, control-statements, control statement
PPT
control-statements....ppt - definition
PPTX
Programming Fundamentals in C++ structures
PDF
Unit 2=Decision Control & Looping Statements.pdf
DOCX
Chapter 4(1)
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
2. Control structures with for while and do while.ppt
Control statements in java
control-statements, control-statements, control statement
control-statements....ppt - definition
Programming Fundamentals in C++ structures
Unit 2=Decision Control & Looping Statements.pdf
Chapter 4(1)
controlStatement.pptx, CONTROL STATEMENTS IN JAVA

Similar to DECISION MAKING AND BRANCHING - C Programming (20)

PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PPTX
C language 2
PPS
Programming in Arduino (Part 2)
PPTX
Constructs (Programming Methodology)
PDF
Learn C# Programming - Decision Making & Loops
PPTX
Control statement
PPT
cprogrammingprogramcontrolsHJKHJHJHJ.ppt
DOCX
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
PDF
CS305PC_C++_UNIT 2.pdf jntuh third semester
PPTX
Java Control Statements
PPTX
Bansal presentation (1).pptx
PPTX
BSc. III Unit iii VB.NET
PDF
Controls & Loops in C
PPTX
What is loops? What is For loop?
PPT
control-statements detailed presentation
PPTX
C Programming Control Structures(if,if-else)
PPTX
DECISION MAKING.pptx
PPTX
Control Structure in JavaScript (1).pptx
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
C language 2
Programming in Arduino (Part 2)
Constructs (Programming Methodology)
Learn C# Programming - Decision Making & Loops
Control statement
cprogrammingprogramcontrolsHJKHJHJHJ.ppt
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
CS305PC_C++_UNIT 2.pdf jntuh third semester
Java Control Statements
Bansal presentation (1).pptx
BSc. III Unit iii VB.NET
Controls & Loops in C
What is loops? What is For loop?
control-statements detailed presentation
C Programming Control Structures(if,if-else)
DECISION MAKING.pptx
Control Structure in JavaScript (1).pptx
Ad

More from MSridhar18 (13)

PDF
Linked List - Part - 2 Linked List - Part - 2
PDF
Singly Linked List - Singly Linked List - Part -1
PDF
CONCEPT OF ARRAY IN DATA STRUCTURES CONCEPT OF ARRAY IN DATA STRUCTURES
PDF
unit 1 ds.INTRODUCTION TO DATA STRUCTURES
PPT
Data Warehouse Introduction to Data Warehouse
PPT
Cluster Analysis K-Means Clustering Typically measured by Euclidean distance
PPT
Classification and Cluster 2BCasic Concepts
PPT
Linked Lists: A Comprehensive Guide Advantages, various types, and fundamenta...
PPT
Data Structures: A Foundation for Efficient Programming
PPTX
ARRAY's in C Programming Language PPTX.
PPTX
Fundamentals of computers - C Programming
PPTX
POINTERS AND FILE HANDLING - C Programming
PPTX
USER DEFINE FUNCTION AND STRUCTURE AND UNION
Linked List - Part - 2 Linked List - Part - 2
Singly Linked List - Singly Linked List - Part -1
CONCEPT OF ARRAY IN DATA STRUCTURES CONCEPT OF ARRAY IN DATA STRUCTURES
unit 1 ds.INTRODUCTION TO DATA STRUCTURES
Data Warehouse Introduction to Data Warehouse
Cluster Analysis K-Means Clustering Typically measured by Euclidean distance
Classification and Cluster 2BCasic Concepts
Linked Lists: A Comprehensive Guide Advantages, various types, and fundamenta...
Data Structures: A Foundation for Efficient Programming
ARRAY's in C Programming Language PPTX.
Fundamentals of computers - C Programming
POINTERS AND FILE HANDLING - C Programming
USER DEFINE FUNCTION AND STRUCTURE AND UNION
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Computing-Curriculum for Schools in Ghana
Anesthesia in Laparoscopic Surgery in India
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
TR - Agricultural Crops Production NC III.pdf
Supply Chain Operations Speaking Notes -ICLT Program
PPH.pptx obstetrics and gynecology in nursing

DECISION MAKING AND BRANCHING - C Programming

  • 2. • 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. • They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making statements in programming languages decide the direction of the flow of program execution. Introduction to Decision Making: Types of Conditional Statements: 1.if Statement 2.if-else Statement 3.Nested if Statement 4.if-else-if Ladder 5.switch Statement 6.Conditional Operator 7.Jump Statements
  • 3. if Statement: An if statement is like asking a computer a question. • If the answer is "yes" (true), it does one thing. • If the answer is "no" (false), it does something else or nothing at all. Syntax of if Statement if(condition) { // Statements to execute if // condition is true }
  • 4. if else statement: An if-else statement provides two possible execution paths based on a condition. • If the condition is true, the code inside the if block is executed. • If the condition is false, the code inside the else block is executed. Syntax: if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
  • 5. Nested if statement: A nested if statement is an if statement within another if statement. This allows for more complex decision-making processes. Syntax: if (condition1) { // code to be executed if condition1 is true if (condition2) { // code to be executed if both condition1 and condition2 are true } else { // code to be executed if condition1 is true but condition2 is false } } else { // code to be executed if condition1 is false }
  • 6. if-else-if ladder: An if-else-if ladder is used when you have multiple conditions to check and want to execute different code blocks based on those conditions. It's like a series of if statements connected together. The conditions are checked sequentially, and as soon as one condition is true, the corresponding code block is executed, and the rest of the ladder is skipped. Syntax: if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition1 is false and condition2 is true } else if (condition3) { // code to be executed if condition1 and condition2 are false, but condition3 is true } else { // code to be executed if none of the conditions are true }
  • 7. A switch statement is a multi-way branch statement that allows you to select one of many code blocks to be executed. It's often used as an alternative to multiple if-else statements when you have a variable with a limited number of possible values. Syntax: C switch (expression) { case constant1: // code to be executed if expression == constant1break; case constant2: // code to be executed if expression == constant2break; // ...default: // code to be executed if no case matches } Switch statement:
  • 8. Conditional Operator: The conditional operator, often called the ternary operator, is a shorthand way to express an if-else statement in a single line. It's represented by the question mark (?) and colon (:). Syntax: condition ? expression1 : expression2 Example: int x = 10, y = 20, max; max = (x > y) ? x : y;
  • 9. Jump Statement: Jump statements alter the normal flow of a program's execution. They allow you to transfer control to a different part of the code. Types of Jump Statements 1. Break Statement • Exits the current loop or switch statement immediately. • Commonly used to terminate a loop when a specific condition is met. for (int i = 0; i < 10; i++) { if (i == 5) { break; } printf("%d ", i); }
  • 10. 2. Continue Statement • Skips the rest of the current iteration of a loop and jumps to the next iteration. for (int i = 0; i < 5; i++) { if (i % 2 == 0) { continue; } printf("%d ", i); } 3. Return Statement • Terminates the current function and returns a value (optional). int add(int a, int b) { return a + b; }
  • 11. 4. Goto Statement • Transfers control to a labeled statement within the same function. • Generally discouraged due to potential for creating hard-to-follow code. #include <stdio.h>int main() { int i = 0; loop: printf("%d ", i); i++; if (i < 5) { goto loop; } return 0; }
  • 12. Looping Statement: Looping statements in programming allow you to execute a block of code repeatedly until a certain condition is met. They are essential for automating repetitive tasks and performing calculations iteratively. Types of Loops There are primarily three types of loops in most programming languages: 1. For Loop • Used when you know the number of iterations in advance. Syntax: for (initialization; condition; increment/decrement) { // code to be executed }
  • 13. 2.While loop: • Used when the number of iterations is unknown, but you have a condition to check before each iteration. Syntax: while (condition) { // code to be executed } 3.do-while loop: • Similar to a while loop, but the condition is checked after the loop body is executed at least once. Syntax: do { // code to be executed } while (condition);
  • 14. Jump in loop: Jump statements can be used to modify the normal flow of execution within loops. The two primary jump statements used in loops are break and continue. Break Statement • Terminates the loop entirely. • Once encountered, the program exits the loop immediately and continues execution after the loop. Example: for (int i = 0; i < 10; i++) { if (i == 5) { break; } printf("%d ", i); }
  • 15. Continue Statement • Skips the rest of the current iteration. • When encountered, the program jumps to the beginning of the next iteration of the loop. Example: for (int i = 0; i < 5; i++) { if (i % 2 == 0) { continue; } printf("%d ", i); }