Thursday, 18 July 2019

Switch Statement in C

Switch Statement in C


Switch Statement in C


Switch Statement Program in C


Hello Friends,

Today we will learn Switch Statement.
 Switch Statement is used to make a choice from a number of options.It requires only one argument of any datatype which is checked with the number of case options.If the value matches with case constant then the particular case statement is executed otherwise default  statement is executed.Every case statement is terminated with ':' . The break statement is used to exit from current case statement.

In switch statement,the variable or expression must be a character ('a','b') or an integer(1,2,3).

switch expression should not be terminated with a semicolon(;) or any other symbol.

switch statement can also be written without default statement but break statement is mandatory with all cases.If no break statements are given all the cases would be executed.
The case keyword must contain constant value (i.e integer or character) with a colon(:).

lets take a simple example 

A program to perform addition,subtraction,multiplication and division with switch statement.

Practical Example of Switch

Example of Switch code

output:

How to select option using switch








lets take some more examples of switch statement.


A program to use nested if else statement in the switch statement to display ASCII character.

ASCII using Switch Statement

Demo of Switch

Example of Selection Statement




A program to convert years into minutes,hours,days,months or seconds using switch statement.
Example of selection statement to display time






Points to remember
* No two  case statements have identical constant in the same switch.
* In switch statement nested if can be used.
* In switch statement float values or float variable can not be used.
* Only constant values are used with Switch statement.Logical and Relational Operators             cannot be used .



No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...