Daily Temperature Monitor

While Loop in C#: Daily Temperature Monitor

Introduction

In many real-world systems, data is collected continuously until a specific condition is met. Examples include environmental monitoring, health tracking devices, weather stations, and industrial sensors. These systems often gather measurements repeatedly without knowing in advance how many data entries will be recorded. In programming, this type of situation is commonly handled using a while loop.

In this lesson, we will build a Daily Temperature Monitor using a while loop in C#. The program will repeatedly ask the user to input temperature readings. The process continues until the user enters a special value that signals the program to stop. This value is called a sentinel value, which indicates the end of input.

The while loop is ideal for problems where the number of repetitions is unknown before execution begins. The loop continues to run as long as a specified condition remains true. Once the condition becomes false, the loop terminates automatically.

Through this exercise, students will learn how to collect multiple temperature readings, compute the average temperature, and identify the highest recorded temperature. This example demonstrates how loops can be used for data monitoring, analysis, and processing, which are common tasks in many real-world applications.

By the end of this lesson, students will understand how a while loop operates, how sentinel values work, and how to perform calculations within repeated program execution.


Lesson Objectives

Outcome-Based Education (OBE) emphasizes measurable learning outcomes. The objectives in this lesson guide students through progressive stages of learning, from conceptual understanding to practical application. Students will explore how loops function, implement them in a monitoring system, and apply the concept to other data-processing programs.

These objectives help learners develop programming competency by combining theoretical knowledge with hands-on coding practice.

1. Understand

  • Explain the structure and syntax of a while loop in C#.
  • Describe how loops control repeated program execution.
  • Identify situations where the number of repetitions is unknown.

2. Learn

  • Learn how sentinel values control loop termination.
  • Understand how to store multiple temperature readings.
  • Recognize how variables are used for totals, averages, and comparisons.

3. Practice

  • Write a program that repeatedly collects temperature data.
  • Use loop conditions to control program execution.
  • Test the program with different temperature inputs.

4. Apply

  • Apply the temperature monitoring logic to other real-world scenarios such as rainfall monitoring or energy consumption tracking.
  • Extend the program to track minimum temperature values.
  • Modify the system to display temperature statistics.

Beginner-Friendly Source Code

Below is a simple C# console application that demonstrates a Daily Temperature Monitor using a while loop.

using System;

namespace DailyTemperatureMonitor
{
    class Program
    {
        static void Main(string[] args)
        {
            double temperature;
            double totalTemperature = 0;
            double highestTemperature = double.MinValue;
            int count = 0;

            Console.WriteLine("=== Daily Temperature Monitor ===");
            Console.WriteLine("Enter temperature readings. Type -1 to stop.\n");

            Console.Write("Enter temperature: ");
            temperature = Convert.ToDouble(Console.ReadLine());

            while (temperature != -1)
            {
                totalTemperature += temperature;
                count++;

                if (temperature > highestTemperature)
                {
                    highestTemperature = temperature;
                }

                Console.Write("Enter temperature: ");
                temperature = Convert.ToDouble(Console.ReadLine());
            }

            if (count > 0)
            {
                double average = totalTemperature / count;

                Console.WriteLine("\nNumber of entries: " + count);
                Console.WriteLine("Average Temperature: " + average);
                Console.WriteLine("Highest Temperature: " + highestTemperature);
            }
            else
            {
                Console.WriteLine("No temperature data entered.");
            }
        }
    }
}

Code Explanation

Namespace
The namespace DailyTemperatureMonitor organizes the program within a logical project structure.

Variables

  • temperature stores the user’s input.
  • totalTemperature accumulates the total temperature values.
  • highestTemperature stores the highest temperature recorded.
  • count tracks how many readings were entered.

Sentinel Value

-1

The value -1 indicates that the user wants to stop entering data.

While Loop

while (temperature != -1)

The loop continues running until the sentinel value is entered.

Average Calculation

After exiting the loop, the program computes:

Average = Total Temperature / Number of Entries

 

Example Output

=== Daily Temperature Monitor ===
Enter temperature readings. Type -1 to stop.

Enter temperature: 30
Enter temperature: 31
Enter temperature: 29
Enter temperature: 32
Enter temperature: -1

Number of entries: 4
Average Temperature: 30.5
Highest Temperature: 32

Summary

In this lesson, we developed a Daily Temperature Monitor using a while loop in C#. The program continuously collects temperature readings until a sentinel value is entered. During the loop execution, the program calculates the total number of entries, determines the highest temperature, and computes the average temperature. This exercise demonstrates how while loops are useful when the number of iterations is unknown beforehand. The concept learned here can be applied to various data collection systems such as weather monitoring, sensor data logging, and statistical analysis programs.


Multiple Choice Questions (Bloom’s Taxonomy)

1. Remembering

Which loop continues running while a condition remains true?

A. for loop
B. while loop
C. do while loop
D. switch statement


2. Understanding

Why is a while loop suitable for the Daily Temperature Monitor?

A. Because the number of temperature entries is fixed
B. Because the number of entries is unknown
C. Because it runs only once
D. Because it does not use conditions


3. Applying

Which value stops the temperature input loop?

A. 0
B. 100
C. -1
D. 50


4. Analyzing

What will happen if the sentinel value is not used?

A. The loop may run indefinitely
B. The program will stop immediately
C. The program will skip inputs
D. The compiler will show an error


5. Evaluating

Which improvement would enhance the program?

A. Remove the loop
B. Add minimum temperature tracking
C. Remove input
D. Remove calculations


Exercises, Assessment, and Lab Exam

Introductory Paragraph

To strengthen understanding of the while loop, students should experiment with program enhancements and modifications. These activities encourage problem-solving and deeper comprehension of loop control structures while improving the program’s analytical capabilities.


Exercises

  1. Modify the program to also display the lowest temperature recorded.
  2. Display the temperature difference between highest and lowest values.
  3. Format the average temperature to two decimal places.
  4. Prevent negative values other than -1 from being accepted.
  5. Display a message if the temperature exceeds 35°C.

Assessment Activity

Create an Environmental Data Monitor that collects:

  • Temperature
  • Humidity
  • Wind speed

The program should compute averages for each measurement.


Lab Exam

Task: Develop a Weather Data Monitoring System

Requirements:

  • Accept temperature readings until a sentinel value is entered
  • Display total readings, highest temperature, lowest temperature, and average
  • Use proper loop control and variable management
  • Display results clearly

Evaluation Criteria:

  • Loop implementation (30%)
  • Correct calculations (30%)
  • Code readability (20%)
  • Program functionality (20%)

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.

, , , , , , , , ,

Post navigation