Home > Back-end >  using a switch statement in a while loop c#
using a switch statement in a while loop c#

Time:06-03

EDITED i have this project for a lab, the requirments are that we use a loop, we need temp to go from 60-100 and humidity form 40-100% and then get the THI ( temp humidity index), i originally wanted to make a while loop and have rh and temp then a switch stament with both variables but it did not want to work with 2 variables so i decided to have a while loop and switch inside a switch statement inside a while loop which ik is not readable, i would make methods for everything else but i need to use a loop

using System;

namespace _1012Lab3
{
    class Program
    {
        static void Main(string[] args)
        {
            // calculate THI(Temp- 0.55(1-Relative Humidity)(Temp -58))
            //relative humidity range 40% - 100%
            //temp range 60F - 100F
            //increments of 10
            // make a method for the formula?
            //THI = Tdb – [0.55 – (0.55 x RH/100)] x (Tdb – 58)
            //create loop that goes up in incremets of 10
            //while temp<10 
            // rh is 0-1 so for 60% humidity  60/100=0.6
            // test example works temp 60 ,RH 50(0.5) answer is 59.45
            //temp ranges 60,70,80,90,100
            //Rh ranges 40,50,60,70,80,90,100
            double temp = 0;
            double THI=0;
            double rh=0;
            while (temp <=100)
            {
                temp  ;
                Console.WriteLine(temp);
                switch (temp)
                {
                    case 60:
                       while(rh<=100)
                        {
                            rh  ;
                        }
                            switch(rh)
                            {
                            case 40:
                                Console.Write("yes");
                                Console.Write(rh);
                                Console.Write(temp);
                                Console.WriteLine($" case 40the temp is{temp}rh is{rh}and thi is {THI}");
                                break;

                            case 50:
                                THI = Thi(temp, rh);
                                Console.WriteLine($" case 50the temp is{temp}rh is{rh}and thi is {THI}");
                                break;
                            
                            case 60:
                                
                                break;

                            case 70:
                                
                                break;
                            
                            case 80:
                               
                                break;
                            
                            case 90:
                                
                                break;
                            
                            case 100:
                                
                                break;
                        }
                        
                        break;
                   
                    case 70:
                       
                        break;
                    
                    case 80:
                        
                        break;
                    
                    case 90:
                        
                        break;
                  
                    case 100:
                        
                        break;


                }




            }
        }
        
        static double Thi(double temp1, double RH1)
        {
            double RH2 = (RH1 / 100);
            double THI = temp1 - 0.55 * (1 - RH2) * (temp1 - 58);
            return THI;
        }
    
    
    }
}

CodePudding user response:

When you get your code with lots of levels, it's recomended simplify:

double temp = 0;
double THI = 0;
double rh = 0;
while (temp <= 100)
{
    ManageTemp(ref temp, ref rh, ref THI);
}

And move the code into other methods, reducing the cyclomatic complexity:

private void ManageTemp(ref double temp, ref double rh, ref double THI)
{
    temp  ;
    Console.WriteLine(temp);

    switch (temp)
    {
        case 60: ManageTemp60(temp, ref rh, ref THI); break;
        case 70: ManageTemp70(temp, ref rh, ref THI); break;
        case 80: ManageTemp80(temp, ref rh, ref THI); break;
        case 90: ManageTemp90(temp, ref rh, ref THI); break;
        case 100: ManageTemp100(temp, ref rh, ref THI); break;
    }
}

private void ManageTemp60(double temp, ref double rh, ref double THI)
{
    while (rh <= 100)
    {
        rh  ;

        switch (rh)
        {
            case 40:
                Console.Write("yes");
                Console.Write(rh);
                Console.Write(temp);
                Console.WriteLine($" case 40the temp is{temp}rh is{rh}and thi is {THI}");
                break;

            case 50:
                THI = Thi(temp, rh);
                Console.WriteLine($" case 50the temp is{temp}rh is{rh}and thi is {THI}");
                break;

            case 60:
                break;

            case 70:
                break;

            case 80:
                break;

            case 90:
                break;

            case 100:
                break;
        }
    }
}

You may also create a class like:

public class TempData
{
    public double temp { get; set; }
    public double THI { get; set; }
    public double rh { get; set; }
}

And simplify the work with the parameters:

var tempData = new TempData();
while (tempData.temp <= 100)
{
    ManageTemp(tempData);
}

private void ManageTemp(TempData tempData)
{
    tempData.temp  ;
    //...

    switch (tempData.temp)
    {
        case 60: ManageTemp60(tempData); break;
        //...
    }
}

private void ManageTemp60(TempData tempData)
{
    while (tempData.rh <= 100)
    {
        tempData.rh  ;
        //...
    }
}

In this way you can avoid lots of problems and get a more readable code.

CodePudding user response:

If your goal is simply to calculate the thi value for each possible combination of temp and rh, you can achieve that in a nested for loop that iterates over only the relevant temp and rh values:

int tempMin = 60;
int tempMax = 100;
int tempStep = 10;

int rhMin = 40;
int rhMax = 100;
int rhStep = 10;

double thi;

for (int temp = tempMin; temp <= tempMax; temp  = tempStep)
{
    for (int rh = rhMin; rh <= rhMax; rh  = rhStep)
    {
        thi = Thi(temp, rh);

        Console.WriteLine($"The temp is {temp}, rh is {rh} and thi is {thi}");
    }
}

Example fiddle here.

  • Related