• conditional branching statements are used to control the flow of
execution based on conditions.
• These statements allow a program to choose different paths of
execution depending on whether a condition is true or false.
• A condition is usually formed using:
• Relational operators (<, >, <=, >=, ==, !=)
• Logical operators (&&, ||, !)
Types
conditional branching statements are classified as:
• if statement
• if–else statement
• Nested if–else statement
• else–if ladder
• switch statement
The if Statement
The if statement is used to execute a block of code only when a given
condition is true.
Syntax
if (condition)
{
statement(s);
}
Program: Check whether a number is positive
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("The number is
positive");
}
return 0;
Sample Output
Enter a number: 5
The number is positive
The if–else Statement
• The if–else statement allows execution of one block when the condition is true
and another block when the condition is false.
Syntax
if (condition)
{
statement(s);
}
else
{
statement(s);
}
Program: Check whether a number is even or odd
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("The number is even");
}
else
{
printf("The number is odd");
}
return 0;
}
Sample Output
Enter a number: 7
The number is odd
Nested if–else Statement
When an if or else block contains another if–else statement, it is called
nested if–else. Syntax
if (condition1)
{
if (condition2)
{
statement(s);
}
else
{
statement(s);
}
}
else
{
statement(s);
Program: Find the largest of three numbers
/*C program to find the largest number among
three number */
// using nested if-else
#include <stdio.h>
int main()
{
int c = 10, b = 22, a = 9;
/* Finding largest by comparing using
relational operators */
if (a > b) {
if (a > c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else {
if (b > c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}
return 0;
}
Output
The numbers A, B and C are: 10, 22, 9
22 is the largest number.
If-else-if statement/ else–if Ladder
The else–if ladder is used to check multiple conditions
sequentially.
As soon as a condition becomes true, the corresponding block
is executed and the rest are skipped.
Syntax
Program: Display grade based on marks
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 90)
printf("Grade A");
else if (marks >= 75)
printf("Grade B");
else if (marks >= 60)
printf("Grade C");
else
printf("Fail");
return 0;
}
Sample Output
Enter marks: 82
Grade B
switch Statement
The switch statement is a multi-branch decision statement
used when a variable is compared against multiple constant
values.
Syntax
Simple calculator using switch
Sample Output
Enter two numbers:
10 5
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice:
3
Product = 50
Conditional Branching statements programming in C

Conditional Branching statements programming in C

  • 2.
    • conditional branchingstatements are used to control the flow of execution based on conditions. • These statements allow a program to choose different paths of execution depending on whether a condition is true or false. • A condition is usually formed using: • Relational operators (<, >, <=, >=, ==, !=) • Logical operators (&&, ||, !)
  • 3.
    Types conditional branching statementsare classified as: • if statement • if–else statement • Nested if–else statement • else–if ladder • switch statement
  • 4.
    The if Statement Theif statement is used to execute a block of code only when a given condition is true. Syntax if (condition) { statement(s); }
  • 5.
    Program: Check whethera number is positive #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num > 0) { printf("The number is positive"); } return 0; Sample Output Enter a number: 5 The number is positive
  • 6.
    The if–else Statement •The if–else statement allows execution of one block when the condition is true and another block when the condition is false. Syntax if (condition) { statement(s); } else { statement(s); }
  • 7.
    Program: Check whethera number is even or odd #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) { printf("The number is even"); } else { printf("The number is odd"); } return 0; } Sample Output Enter a number: 7 The number is odd
  • 8.
    Nested if–else Statement Whenan if or else block contains another if–else statement, it is called nested if–else. Syntax if (condition1) { if (condition2) { statement(s); } else { statement(s); } } else { statement(s);
  • 9.
    Program: Find thelargest of three numbers
  • 10.
    /*C program tofind the largest number among three number */ // using nested if-else #include <stdio.h> int main() { int c = 10, b = 22, a = 9; /* Finding largest by comparing using relational operators */ if (a > b) { if (a > c) printf("%d is the largest number.", a); else printf("%d is the largest number.", c); } else { if (b > c) printf("%d is the largest number.", b); else printf("%d is the largest number.", c); } return 0; } Output The numbers A, B and C are: 10, 22, 9 22 is the largest number.
  • 11.
    If-else-if statement/ else–ifLadder The else–if ladder is used to check multiple conditions sequentially. As soon as a condition becomes true, the corresponding block is executed and the rest are skipped.
  • 12.
  • 13.
    Program: Display gradebased on marks
  • 14.
    #include <stdio.h> int main() { intmarks; printf("Enter marks: "); scanf("%d", &marks); if (marks >= 90) printf("Grade A"); else if (marks >= 75) printf("Grade B"); else if (marks >= 60) printf("Grade C"); else printf("Fail"); return 0; } Sample Output Enter marks: 82 Grade B
  • 15.
    switch Statement The switchstatement is a multi-branch decision statement used when a variable is compared against multiple constant values.
  • 16.
  • 17.
  • 18.
    Sample Output Enter twonumbers: 10 5 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter your choice: 3 Product = 50