Stock Finished Check – If Else – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#1872 siteicon   siteicon   siteicon  

Stock Finished Check – If Else

Check if stock is finished using if-else selection condition

Learning Objectives

  • Learning to use ! operator as part of if-else condition check.
  • Testing a use case for unsigned integer.

Source Code

TC++ #1872


Source Code

Run Output

Enter items in stock : 0
Items in stock are finished.

- OR -
Enter items in stock : 12
You have 12 items in stock

Code Understanding

unsigned int stock;
This has been declared as unsigned as stock can not have negative values.

cout<<“Enter items in stock : “;cin>>stock;
Collecting the stock value from the user.

if(!stock) cout<<“Items in stock are finished.”<<endl;
Checking if stock is 0. !stock (will be read as not-stock) will be true if stock is zero. Finished status will be reported.

else  cout<<“You have “<<stock<<” items in stock”<<endl;
This selection will go to else condition when !stock is false which means that some positive stock is there. The message of stock count will then be displayed


  • Although this example looks trivial in the sense of entering 0 and then saying that stock is finished, but it has practical applications when the 0 status is passed by an automated iterative process and not directly entered by the user.
  • By declaring unsigned the number of items we can enter in stock will increase to 4,294,967,295 (232-1) instead of 2,147,483,647 (231-1)

Suggested Filename(s): stock.cpp, stockcheck.cpp


sunmitra| Created: 23-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