Conditional Statements in C: switch in C

Hello, fellow coders! Today, we’re going to explore the fascinating world of conditional statements in C, specifically focusing on the switch statement. If you’ve ever found yourself tangled in a web of if-else statements, you’ll appreciate the simplicity and readability that switch brings to the table. So, let’s get started!

Understanding Conditional Statements

Before we dive into the switch statement, let’s take a moment to understand what conditional statements are. In C programming, conditional statements are used to make decisions based on certain conditions. These conditions are evaluated to either true or false, and depending on the result, the program executes certain blocks of code.

The most common conditional statements in C are if, if-else, and switch. Today, we’re going to focus on the switch statement, a powerful tool that can make your code more efficient and easier to read.

Understanding the Switch Statement

In the realm of C programming, the switch statement is a powerful tool that allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

The switch statement in C is a type of conditional statement that allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Here’s the basic syntax of a switch statement:

switch(expression) {
    case constant-expression:
        statement(s);
        break; /* optional */
    /* you can have any number of case statements */
    default: /* Optional */
        statement(s);
}
C

In this syntax, the switch expression is evaluated once. The value of the expression is compared with the values of each case. If there’s a match, the corresponding block of code is executed.

Examples

Let’s dive into some examples to understand this better. Consider a scenario where you want to execute different code blocks based on the day of the week. You could use an if-else-if ladder, but a switch statement makes this much more straightforward:

#include <stdio.h>

int main() {
  int day = 2;
  switch(day) {
     case 1 :
        printf("It's Monday!\n");
        break;
     case 2 :
        printf("It's Tuesday!\n");
        break;
     // And so on for the rest of the week
     default :
        printf("Invalid day!\n");
  }
}
C

Code Examples

Let’s dive deeper with two more examples.

Example 1: Grading System

Let’s consider a grading system where ‘A’ stands for “Excellent”, ‘B’ and ‘C’ for “Well done”, ‘D’ for “You passed”, and ‘F’ for “Better try again”. Here’s how you can implement this using a switch statement in C:

#include <stdio.h>

int main() {
  char grade = 'B';
  switch(grade) {
     case 'A' :
        printf("Excellent!\n");
        break;
     case 'B' :
        printf("Good\n" );
        break; 
     case 'C' :
        printf("Well done\n");
        break;
     case 'D' :
        printf("You passed\n");
        break;
     case 'F' :
        printf("Better try again\n");
        break;
     default :
        printf("Invalid grade\n");
  }
  printf("Your grade is  %c\n", grade);
}
C

When the above code is compiled and executed, it produces the following result: “Well done. Your grade is B”.

Example 2: Day of the Week

Another example could be printing the day of the week based on the provided number (1-7). Here’s how you can do it:

#include <stdio.h>

int main() {
  int day = 3;
  switch(day) {
     case 1 :
        printf("Monday\n");
        break;
     case 2 :
        printf("Tuesday\n");
        break;
     // And so on for the rest of the week
     default :
        printf("Error!\n");
  }
}
C

Wrapping Up

The switch statement in C is a powerful conditional statement that provides a more readable alternative to lengthy if-else-if ladders when dealing with multiple conditions. Remember, practice is key when it comes to programming. So, try to write your own switch statements and see how they work!

Frequently Asked Questions (FAQ)

How to use switch with condition in C?

In C, the switch statement is used to perform different actions based on different conditions. The expression inside the switch statement can be byte, short, char, and int primitive data types. Here’s the basic syntax:

   switch(expression) {
      case constant-expression  :
         statement(s);
         break; // optional
      // you can have any number of case statements
      default : // Optional
         statement(s);
   }
C

Can we use conditional statement in switch?

No, switch statements in C only allow discrete values like integers or characters. They do not support conditions or ranges. For conditions, you would typically use if-else statements.

What are conditional statements in switch statement?

In a switch statement, the conditions are the various cases that the switch expression is tested against. If a match is found, the code block associated with that case is executed.

What are the 4 conditional statements in C?

The four conditional statements in C are if, if-else, nested if, and switch-case statements.

Can a switch statement be used inside a loop?

Yes, a switch statement can be used inside a loop. This can be useful when you want to perform different actions in each iteration of the loop based on the value of a variable.

What happens if we don’t use a break in a switch case?

If you don’t use a break in a switch case, the program will continue executing the next case statements regardless of the case match. This is known as fall-through.

Can we use a string in a switch case in C?

No, C does not support strings in switch-case statements. The switch expression must be an integral or enumerated type.

What is the difference between if-else and switch-case statements?

The if-else statement is used to perform different actions based on different conditions. The switch-case statement is used to perform different actions based on different cases or values of a variable. The switch-case statement can be more readable when dealing with multiple conditions.

Can we use a switch statement for boolean in C?

Yes, you can use a switch statement with boolean values in C. However, it’s not common because a boolean value can only be true or false, which is more straightforwardly handled with an if-else statement.

  1. Understanding Loops in C: Dive into the world of loops in C and learn how to automate repetitive tasks in your code.
  2. Functions in C: Learn about the building blocks of a C program and how to create reusable pieces of code.
  3. Pointers in C: Unravel the mystery of pointers in C and understand how to work with memory addresses.
  4. Arrays in C: Discover how to store and manipulate a collection of items using arrays in C.
  5. Structures in C: Learn how to group related variables together using structures in C.

And that’s a wrap! I hope this tutorial has helped you understand the switch statement in C better. Remember, the key to mastering programming is practice. So, keep coding and keep learning!

Scroll to Top