C Branching Statement

Branching statements are fundamental in programming, allowing developers to control the flow of their programs based on specific conditions. In C programming, branching statements enable the execution of different blocks of code depending on whether certain conditions are true or false. This guide will explore the most common branching statements in C: if, else, else if, switch, and the ternary operator.

The if Statement

The if statement is the most basic form of a branching statement. It checks a condition and executes a block of code if the condition is true. If the condition is false, the code inside the if block is skipped.

Syntax:

if (condition) {
// Code to execute if condition is true
}

Example:

int age = 18;
if (age >= 18) {
printf("You are eligible to vote.\n");
}

In this example, if the value of age is 18 or greater, the message “You are eligible to vote.” is printed.


The else Statement

The else statement is used in conjunction with the if statement. It defines a block of code that will execute if the if condition is false.

Syntax:

if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}

Example:

int age = 16;
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}

In this example, since age is less than 18, the message “You are not eligible to vote.” is printed.


The else if Statement

The else if statement allows you to check multiple conditions in a sequence. If the first condition is false, it checks the next condition, and so on.

Syntax:

if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}

Example:

int score = 75;
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: F\n");
}

Here, the program checks the score and assigns a grade based on its value.


The switch Statement

The switch statement is used to execute one block of code among many options. It’s often used when you have multiple conditions based on a single variable.

Syntax:

switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
// Additional cases...
default:
// Code to execute if none of the cases match
}

Example:

int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}

In this example, since day is 3, the output will be “Wednesday”.


The Ternary Operator

The ternary operator (? :) is a compact form of an if-else statement. It evaluates a condition and returns one of two values depending on whether the condition is true or false.

Syntax:

condition ? expression1 : expression2;

Example:

int age = 20;
printf("You are %s.\n", (age >= 18) ? "an adult" : "a minor");

Here, the message “You are an adult.” is printed because age is 20.


Conclusion

Branching statements are essential tools in C programming that allow you to create dynamic and responsive programs. Whether you’re choosing between simple if statements or the more complex switch cases, understanding these control structures will give you greater flexibility and control over your code’s execution flow. As you continue learning C, experiment with these branching statements to see how they can be used to solve various programming challenges.

Quiz

1. What is the primary purpose of branching statements in C programming?

  • a) To perform arithmetic operations
  • b) To define functions
  • c) To control the flow of program execution based on conditions
  • d) To allocate memory dynamically

Answer: c) To control the flow of program execution based on conditions

2. Which of the following is the correct syntax for an if statement in C?

  • a) if (condition) { // code }
  • b) if condition { // code }
  • c) if condition ( // code )
  • d) if { condition // code }

Answer: a) if (condition) { // code }

3. What will be the output of the following code snippet?

int x = 10;
if (x > 5) {
printf("A");
} else {
printf("B");
}
  • a) A
  • b) B
  • c) AB
  • d) None of the above

Answer: a) A

4. Which of the following statements is true about the else statement in C?

  • a) It must be used with every if statement.
  • b) It executes only when the if condition is true.
  • c) It executes only when the if condition is false.
  • d) It can be used without an if statement.

Answer: c) It executes only when the if condition is false.

5. What will be the output of the following code snippet?

int score = 85;
if (score >= 90) {
printf("A");
} else if (score >= 80) {
printf("B");
} else if (score >= 70) {
printf("C");
} else {
printf("F");
}
  • a) A
  • b) B
  • c) C
  • d) F

Answer: b) B

6. In a switch statement, what is the purpose of the break keyword?

  • a) To terminate the switch block
  • b) To skip the current case and proceed to the next one
  • c) To exit the loop
  • d) To continue the program without checking further cases

Answer: a) To terminate the switch block

7. What will be the output of the following switch statement?

int day = 5;
switch (day) {
case 1:
printf("Monday");
break;
case 5:
printf("Friday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid");
}
  • a) Monday
  • b) Friday
  • c) Sunday
  • d) Invalid

Answer: b) Friday

8. Which of the following correctly uses the ternary operator to print “Even” if a number is even and “Odd” if it is odd?

  • a) printf("%s", num % 2 == 0 ? "Even" : "Odd");
  • b) printf("%s", num ? "Even" : "Odd");
  • c) printf("%s", num % 2 ? "Odd" : "Even");
  • d) Both a) and c) are correct

Answer: d) Both a) and c) are correct

9. Which statement is used in a switch block to handle cases not explicitly defined?

  • a) else
  • b) default
  • c) case
  • d) break

Answer: b) default

10. What is the output of the following code?

int age = 17;
printf("You are %s.\n", (age >= 18) ? "an adult" : "a minor");
  • a) You are an adult.
  • b) You are a minor.
  • c) You are a child.
  • d) Compilation error

Answer: b) You are a minor.

Leave a Comment