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

Code Learning #CPP#1863

## 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

 #include using namespace std; int main( ) { int good_low_limit=15; int good_high_limit=28; int t,good_weather=0; cout<<"Input current weather temperature:"; cin>>t; if(t>good_low_limit && t

## 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

sunmitra| Created: 22-Dec-2017 | Updated: 15-Sep-2018|

×