Logical Operator Example – Check if Temperature is Comfortable (between 20°C and 30°C)
Introduction of the Lesson
Table of Contents
Operators are the building blocks of decision-making in programming. They allow us to compare values, perform calculations, and make choices based on certain conditions. So far, you have learned about arithmetic operators (for basic math) and relational operators (for comparing values). This time, we will explore logical operators in C#.
Logical operators are used to combine two or more conditions into a single decision. They are very powerful because in real life, most decisions are not based on just one factor. For example, you might say: “I’ll go outside if the weather is sunny and it’s not too hot.” That’s already a logical condition combining two rules!
In this lesson, we’ll apply logical operators to check if a temperature is comfortable. We’ll define comfortable as being between 20°C and 30°C (inclusive). Using logical operators, you’ll see how multiple comparisons can be joined together to make your programs smarter and closer to real-life reasoning.
Objectives (OBE-Oriented)
Before diving into the code, let’s set what we want to achieve in this lesson. This ensures you understand, learn, practice, and apply programming concepts meaningfully.
- Understand: Explain the role of logical operators (AND, OR, NOT) in decision-making within C#.
- Learn: Demonstrate how to write conditions using logical operators to evaluate multiple rules at once.
- Practice: Build and run a program that checks if a given temperature is within a comfortable range (20–30°C).
- Apply: Extend the program to handle real-life variations, such as checking if the weather is too cold, comfortable, or too hot, and provide meaningful feedback.
By the end of this lesson, you’ll be more confident in combining multiple conditions in your code, which is essential for writing real-world applications.
Beginner-Friendly Source Code & Instructions
Step-by-Step Instructions
- Open Visual Studio Code.
- Ensure the .NET SDK is installed. You can confirm by typing:
dotnet --version
- Create a new console project:
dotnet new console -n temperature_checker cd temperature_checker
- Open Program.cs inside the folder.
- Replace the existing code with the example below.
- Save the file and run the program with:
dotnet run
Program.cs
using System;
namespace TemperatureCheckerApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Temperature Comfort Checker ===");
// Step 1: Ask for temperature
Console.Write("Enter the current temperature in °C: ");
int temperature = Convert.ToInt32(Console.ReadLine());
// Step 2: Check if the temperature is between 20 and 30 (inclusive)
if (temperature >= 20 && temperature <= 30)
{
Console.WriteLine("The temperature is comfortable.");
}
else
{
Console.WriteLine("The temperature is not comfortable.");
}
}
}
}
Example Outputs
Case 1 – Comfortable Temperature
=== Temperature Comfort Checker ===
Enter the current temperature in °C: 25
The temperature is comfortable.
Case 2 – Too Cold
=== Temperature Comfort Checker ===
Enter the current temperature in °C: 15
The temperature is not comfortable.
Case 3 – Too Hot
=== Temperature Comfort Checker ===
Enter the current temperature in °C: 35
The temperature is not comfortable.
Explanation of the Program
- We start by importing
System
, which provides access to console input and output. - The program asks the user to enter the current temperature in Celsius.
- The input is converted into an integer using
Convert.ToInt32()
. - The if condition checks if the temperature is greater than or equal to 20 AND less than or equal to 30. This is achieved with the logical AND operator (
&&
). - If the condition is true, the program prints “The temperature is comfortable.”
- If it is false, the program prints “The temperature is not comfortable.”
This demonstrates how logical operators allow us to combine multiple relational checks into one clear decision.
Summary
In this lesson, you learned how to use logical operators in C# to combine multiple conditions. We built a simple program that checks whether the temperature is comfortable (20–30°C). You practiced working with relational operators together with logical AND (&&) to make a decision based on two rules. Logical operators are essential for real-world applications where decisions often depend on multiple factors.
5-Item Multiple Choice (Bloom’s Taxonomy)
- Which logical operator is used to ensure two conditions must both be true?
- A. ||
- B. &&
- C. !
- D. ==
- If the input temperature is 25, what does the program output?
- A. The temperature is not comfortable.
- B. The temperature is comfortable.
- C. Error
- D. Nothing displays
- What does
temperature >= 20 && temperature <= 30
mean?- A. Temperature is exactly 20 or 30
- B. Temperature is between 20 and 30 (inclusive)
- C. Temperature must be less than 20
- D. Temperature must be greater than 30
- What will the program display if the user inputs 10?
- A. The temperature is comfortable.
- B. The temperature is not comfortable.
- C. Error occurs
- D. No output
- Why are logical operators important in programming?
- A. They perform mathematical calculations
- B. They allow combining multiple conditions ✅
- C. They convert input types
- D. They loop through code
Exercises, Assessment, and Lab Exam
Intro to Exercises
To deepen your understanding of logical operators, it’s important to practice beyond the simple example. The following exercises and tasks will challenge you to think critically and apply operators in different scenarios. This prepares you for building decision-making programs in real-life applications.
Exercises
- Modify the program to check if the temperature is too cold (<20), comfortable (20–30), or too hot (>30).
- Ask the user for both temperature and humidity. Consider it comfortable only if temperature is 20–30 AND humidity is below 70%.
- Create a program that checks if a given number is both positive AND even.
- Write a program that checks if a student passed an exam. A student passes if the grade is 75 or higher AND attendance is at least 80%.
- Modify the original code to display different comfort levels: cool (20–23), pleasant (24–27), warm (28–30).
Assessment
Performance Task:
Write a C# program that asks for the temperature. The program should:
- Display if it’s too cold, comfortable, or too hot.
- Use logical operators effectively.
Lab Exam
Instruction:
Develop a C# console program that:
- Accepts temperature input from the user.
- Checks if the value is within a comfortable range (20–30).
- Displays appropriate messages: “Too Cold,” “Comfortable,” or “Too Hot.”
- Uses logical operators (
&&
) correctly in decision-making.
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.