You are on page 1of 13

UNIT 2

DECISION MAKING AND BRANCHING


In C, decision-making and branching are achieved using control
structures. These control structures allow you to make decisions
based on certain conditions and execute different blocks of code
accordingly.
If-else statement:
The if-else statement is used to execute a block of code
based on a certain condition. If the condition in the if
statement evaluates to true, the code inside the if block is
executed; otherwise, if the condition is false, the code inside
the else block (if present) is executed.
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

Example:
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
} else {
printf("The number is non-positive.\n");
}
Nested if-else statements:
You can also have multiple levels of if-else statements within each other, creating
nested decision-making structures.
if (condition1) {
// Code to execute if condition1 is true
if (condition2) {
// Code to execute if both condition1 and condition2 are true
} else {
// Code to execute if condition1 is true but condition2 is false
}
} else {
// Code to execute if condition1 is false
}
else-if ladder:
The else-if ladder is used when you have multiple conditions to
check, and you want to choose one of several possible blocks of
code to execute.

if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if all conditions are false
}
int score = 85;

if (score >= 90) {


printf("Grade A\n");
} else if (score >= 80) {
printf("Grade B\n");
} else if (score >= 70) {
printf("Grade C\n");
} else {
printf("Grade D\n");
}
GOTO Statement
The goto statement is a control transfer statement in C that
allows you to transfer control to a labeled statement within
the same function.
It provides an unconditional jump to the specified label,
bypassing any intermediate code or flow control constructs
like loops and conditionals.
The syntax of the goto statement
goto label;
#include <stdio.h> #include <stdio.h>

int main() { int main() {


int i = 1; int num = 1;

loop: if (num == 1) {
if (i <= 5) { goto error;
printf("%d ", i); }
i++;
goto loop; printf("This won't be executed.\n");
}
error:
printf("Loop finished!\n"); printf("Error occurred!\n");

return 0; return 0;
} }
Switch Statement
The switch statement in C is a control statement used
to make multi-way decisions based on the value of an
expression.

It allows you to choose from a list of options (cases)


and execute the corresponding block of code based on
the value of the expression.
Syntax
switch (expression) {
case constant1:
// Code to execute when the expression equals constant1
break;
case constant2:
// Code to execute when the expression equals constant2
break;
// More case statements can follow...
default:
// Code to execute when none of the above cases match the expression
break;
}
#include <stdio.h> case 4:
printf("Wednesday\n");
int main() { break;
int dayOfWeek; case 5:
printf("Thursday\n");
printf("Enter the day of the week (1-7): "); break;
scanf("%d", &dayOfWeek); case 6:
printf("Friday\n");
switch (dayOfWeek) { break;
case 1: case 7:
printf("Sunday\n"); printf("Saturday\n");
break; break;
case 2: default:
printf("Monday\n"); printf("Invalid day of the week\n");
break; break;
case 3: }
printf("Tuesday\n");
break; return 0;
In this example, the user enters a number representing the
day of the week (1 for Sunday, 2 for Monday, and so on).
The switch statement then prints the corresponding day
based on the input. If the user enters a value other than 1-
7, the default case is executed, displaying an "Invalid day
of the week" message.

You might also like