Finding the day by days of week number – switch case – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#1963 siteicon   siteicon   siteicon  

Finding the day by days of week number – switch case

User gives the day number and the program outputs the day name for five days and holiday for sat. and sun.

Learning Objectives

  • Understanding the operation of switch-case selection construct.
  • Understanding the passing of integer value for switch case selection.
  • Understanding break after each switch case.
  • Understanding the fall through in switch case.
  • Understanding the purpose of default case.

Source Code

TC++ #1963


Source Code

Alternate (?) :   Related (?) :

Run Output

Enter the day number of week(1-7) : 1

Enter the day number of week(1-7) : 7

Enter the day number of week(1-7) : 8
Error in input

Code Understanding

switch (d) {
The integer value of day value input is passed to switch.

case 1: cout <<“Monday”; break;
case 2: cout <<“Tuesday”;break;
case 3: cout <<“Wednesday”;break;
case 4: cout <<“Thursday”;break;
case 5: cout <<“Friday” ;break;
For first five cases from 1 to 5 the day name is output. after outputting each name a break is given so that program exits the switch selection and goes to line after the switch.

case 6:
case 7: cout <<“HOLIDAY” ;break;
For both case 6 and 7 value of day number, HOLIDAY is printed. This is also called fall through as condition at values 6 falls through the condition for next case at 7.

default: cout <<“Error in input”;
If none of cases finds a match, means the value given by user is not available from case 1 to 7 then this default case is executed.


  • Switch case can always  be implemented by if-else-if ladder.
  • Its construct is like a single pole muti throw switch used on electrical circuits. Things like fan regulator, radio band switch use these kind of switching mechanism.

Common Errors

  • The break command in case statement is often missed by programmers. If it is not given then fall through action happens.
  • Switch case bracket positions have to be properly remembered.
  • The case <value>: syntax is often incorrectly written by using semicolon or comma.

Suggested Filename(s): daysofweek.cpp


sunmitra| Created: 28-Dec-2017 | Updated: 15-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues