switch case statement in c langauge in hindi || Simple calculator Program in C Programming

switch case statement in c langauge in hindi || Simple calculator Program in C Programming

 Switch Case Statement in C Language: 

An Introduction If you're new to programming, you might be wondering what a switch case statement is and how it works. In this article, we'll introduce you to switch case statements in C language, and show you how to use them effectively in your code.

What is a Switch Case Statement?


A switch case statement is a conditional statement that allows you to execute a block of code based on the value of an expression. It's often used as a replacement for multiple if-else statements when you need to test a single variable against multiple values.

The basic syntax of a switch case statement looks like this: 

switch case in c language



How does a Switch Case Statement work?


When you use a switch case statement, the program evaluates the expression in the switch statement and compares it to each case value. If the expression matches a case value, the code block associated with that case is executed. The break statement at the end of each code block is used to exit the switch statement and continue executing the rest of the program.

If the expression doesn't match any of the case values, the code block associated with the default case is executed. The default case is optional, but it's good practice to include it to handle any unexpected values.

Example of using Switch Case Statement


Here's a simple example to illustrate how a switch case statement works in C language:

In this example, the program evaluates the value of the variable 'number' and compares it to each case value. Since 'number' is equal to 2, the code block associated with case 2 is executed, which prints the message "The number is two" to the console.



switch case statement in c langauge in hindi


 Switch Case Statement in C Language: Simple Calculator Program


The switch case statement is a powerful tool in the C programming language that allows you to execute different code blocks based on the value of a variable. In this article, we will explore how to use the switch case statement to create a simple calculator program in C.

Calculator Program Overview


The calculator program we will create will take two inputs from the user: the first number, the operator (+, -, *, /), and the second number. The program will then perform the appropriate arithmetic operation on the two numbers and output the result. Let's get started.

Step 1: Define the Variables

To begin, we need to define the variables we will use in our program. We will need three variables: the first number, the operator, and the second number. We will use the float data type for the numbers and the char data type for the operator.

Step 2: Get User Input

Next, we need to get input from the user for the first number, operator, and second number. We can use the scanf() function to get input from the user.

Note that we use a space before the %c format specifier to ignore any whitespace characters that might be left in the input buffer after the first input.

Step 3: Perform Arithmetic Operation

Now that we have the user input, we can use the switch case statement to perform the appropriate arithmetic operation on the two numbers. We will use the switch statement to select the appropriate case based on the value of the operator.
 

The switch statement takes the value of the operator and executes the code block corresponding to the matching case. If none of the cases match, the default case is executed. In our program, if the user enters an invalid operator, we print an error message and exit the program.

Step 4: Output the Result

Finally, we need to output the result of the arithmetic operation to the user.

This line of code uses the printf() function to print the first number, operator, second number, and result to the console.

Full Calculator Program Code

 

Usage:


Switch case statements are typically used when you have a large number of conditions to test against a single expression. This can be more efficient and easier to read than a series of if-else statements. The switch statement is followed by an expression in parentheses. This expression can be any variable or constant value. The case statements are then used to test whether the expression matches a specific constant value. If the expression matches a constant, the corresponding code block is executed.

Example:


Here's an example of a simple calculator program that uses switch case statements to perform basic arithmetic operations: 

calculator program in



In this example, the user is prompted to enter an operator (+, -, *, /) and two operands. The switch statement is used to test the operator value and perform the corresponding arithmetic operation on the operands. The resulting value is then printed to the console.

Conclusion:


Switch case statements are a powerful tool in C programming that can make your code more efficient and easier to read. They are especially useful when dealing with a large number of conditions that need to be tested against a single expression. In this article, we discussed the syntax and usage of switch case statements in C programming, as well as how to create a simple calculator program using switch case statements. 


Watch Video 

 


All C  Topics : -

  1. C Language Intro

  2. Structure of C Language 

  3. Variable & data Types 

  4. Printf and Scanf Function

  5. Operator in c Language 

  6. If Statement in c Language 

  7. If else and Nasted if in C

  8. Switch Case In C 

  9. While loop in C Language 

  10. Exit Control Loop (Do While)

  11. For Loop in c 

  12. C Language Pratice Program 

  13. Goto Statement in c Programming 

  14. Array in c

  15. Pointer in  c Language 

  16. String in c Language 

Post a Comment (0)
Previous Post Next Post