Teens party – empty if statement – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#1920 siteicon   siteicon   siteicon  

Teens party – empty if statement

Understanding the behaviour of if when nothing is done when if condition is true.

Learning Objectives

  • Learning to use a lone ; when in the true portion of selection condition we do not want to do anything.

Source Code

TC++ #1920

 

Source Code

Run Output

Tell your age : 21
Sorry! only teens allowed.

- OR -
Tell your age : 16

Code Understanding

if(age<=19 && age >=13) ;
Here no output is shown or no variable is set when the condition is found true. We just put a ; after the if selection to indicate an empty statement.

else  cout<<“Sorry! only teens allowed.”<<endl;
This is displayed when selection condition is not true.

Notes

  • Sometimes empty statements are also used by programmers while developing a code where the statements for a particular block are not final.
  • If else condition is empty then the entire else block can be removed instead of writing an empty condition.
  • Empty condition can be often avoided by reversing the logic of condition. For e.g. above code can also be written as (without any empty block and without the else condition).
    if(age>19 || age<13) cout<<“Sorry! only teens allowed.”;

Common Errors

  • Sometimes programmers confuse between use of && and || between two conditions. Remember that when the range of true values is within the given first and second conditions && is used. When the values are outside first and second condition || is used.


Share

sunmitra| Created: 27-Dec-2017 | Updated: 28-Nov-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