Age based discount using ternary operator – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#2318 siteicon   siteicon   siteicon  

Age based discount using ternary operator

Solving age based discount problem with ternary (three operands) operator.

Learning Objectives

  • Learning the age based discount problem using the ternary operator based condition check.

Source Code

TC++ #2318

 

Source Code

  Related (?) :

Run Output

The discount rate is 10 %

Code Understanding

int age=63;
Here some test value has been stored in integer variable age.

int disc=age >= 60  ?   10   :    5;            //The ternary operator conditional syntax
If you study the right hand side there are three parts
age >=60   //Operand 1
10               //operand 2
               //operand 3
The first part is before the ?. This is the test condition, In this example this would be true.
The second part is between ? and : symbol. In the ternary operator combination like this value is returned when the condition is true. So in this example the int disc will be set equal to 10 (the second part) as the condition checked in the first part is true.
The third part is returned if the first part condition is not true. So in this case this will not execute.

cout<<“The discount rate is “<<disc<<” %”<<endl;
Here the final display as above condition will come for 10%

Notes

  • Ternary operators are handy for quick calculations like this, but most programmers prefer if-else constructs. However for quick condition based arithmetic operations they will find use in making complex arithmetic formulas.

Common Errors

  • Care is required to build this operator to put right value for true condition and false condition and ultimately these values have to be equated to the right hand side so data type of both sides should be definitely same both for true and false logic.


Suggested Filename(s): age-ter.cpp



Share

sunmitra| Created: 6-Jan-2018 | Updated: 31-Aug-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