Weather Check – Multiple Instruction in If Scope – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#1863 siteicon   siteicon   siteicon  

Weather Check – Multiple Instruction in If Scope

We collect weather temperature from user and do a range check, then if true will do multiple instructions in the if block scope.

Learning Objectives

  • Using multiple instructions when an if selection condition is true.
  • Using a variable set in one if block as test condition for another block of if.

Source Code

TC++ #1863

 

Source Code

Run Output

Input current weather temperature:23
The weather is good.
Let's call Friends.
We all will have a great outing.

Code Understanding

int good_low_limit=15; int good_high_limit=28;
These are variables to declare limit settings that can be used later for checking within the if conditions. It is a good practice to give such things on the top of the block so that they can be changed conveniently.

int t,good_weather=0;
These are more variable declarations. A variable good_weather has been initialised as 0 as its condition will be made true or 1 when if condition is met.

cout<<“Input current weather temperature:”; cin>>t;
This is simply input data collection.

if(t>good_low_limit && t<good_high_limit)
{
  good_weather=1;
  cout<<“The weather is good.”<<endl;
}
Here a condition truthness has been checked for a range. While range checking multiple checks are related using the && operator as both conditions of lower limit and upper limit must be true for entire test condition to be true. Also, when the condition is tested for true multiple instructions can be put in the { } curly braces block immediately following the if condition test. All the instructions in this block are executed, once the condition is tested to be true.

if(good_weather)
{
  cout<<“Let’s call Friends.”<<endl;
  cout<<“We all will have a great outing.”<<endl;
}
Here again multiple instructions have been run based on a variable set in the previous block of if statement. Since good_weather was set as 1 in the previous block of if , it can be used as a new truthness condition.

 

Notes

  • In these type of programs the variables like good_weather can also be declared as of type bool as true and false condition are only required to be tested for them. In fact, using them as bool variable is actually more efficient in terms of program execution speed.


Suggested Filename(s): weathercheck.cpp



Share

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