Finding the day by days of week number – if else – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#1968 siteicon   siteicon   siteicon  

Finding the day by days of week number – if else

User gives the day number and the program outputs the day name for five days and holiday for sat. and sun. using the if else logic instead of switch case

Learning Objectives

  • Understanding the switch-case replacement with the if-else-if ladder.

Source Code

TC++ #1968

 

Source Code

Alternate (?) :   Related (?) :

Run Output

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

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

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

Code Understanding

if(d==1) cout<<“Monday”;
else if(d==2) cout <<“Tuesday”;
else if(d==3) cout <<“Wednesday”;
else if(d==4) cout <<“Thursday”;
else if(d==5) cout <<“Friday”;

For first five cases from 1 to 5 re checked with if else ladder and the day name is output. No break is required here like a switch-case construct as each item after the first one is in else-if condition.

else if(d==6 || d==7) cout <<“HOLIDAY”;
For both case 6 and 7 value of day number, HOLIDAY is printed. The fall through of switch-case is implemented here as multiple comparison check.

else 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 else  is executed. This is like the default case of switch.

Notes

  • Switch case can always  be implemented by if-else-if ladder but vice-versa is not always true.

Common Errors

  • Proper if-else-if construct removes the need of break in switch case. The break may be there for other reasons required by the program.


Suggested Filename(s): daysofweek-ifelse.cpp



Share

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


Back