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
|
Run Output
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
Notes
- 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|