Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Swich Statement in C

Date- Sep 23,2023

4121


In C, the switch statement is a control structure used for making decisions based on the value of a variable or expression. It allows you to test a variable against a list of values (cases) and execute a block of code associated with the first matching case.

Here's how the switch statement works:

  1. 1. The switch keyword is followed by an expression (usually a variable) enclosed in parentheses. This expression is evaluated to determine which case to execute.

  2. 2. Inside the switch block, you list one or more case labels. Each case label represents a possible value that the expression might have.

  3. 3. When the switch statement is executed, it compares the value of the expression to each case constant sequentially.

  4. 4. If a match is found (i.e., if the expression's value is equal to a case constant), the code block associated with that case is executed.

  5. 5. The break statement is used to exit the switch block. Without break, the execution would continue into subsequent case blocks until a break is encountered or the switch block ends.

  6. 6. If none of the case values matches the expression, the default block is executed (if it exists). The default block is optional and serves as a fallback.









Here's the basic syntax of a switch statement:
switch (expression) {
    case constant 1:
        // Code to be executed if expression equals constant 1
        break;
    case constant 2:
        // Code to be executed if expression equals constant 2
        break;
   
    default:
        // Code to be executed if no case matches
}
In this example, the user enters a number, and the switch statement determines which option was chosen based on the input value. If the input doesn't match any of the cases, the default block is executed.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page