-1

I am trying to create a simple switch case with If-Statements code.

Problem:

I don't get any value back.

For Example:

If I put int Temperature = 0; the Code should output "Es ist kalt". But my console doesn't display anything.

using System;

namespace SwitchCase
{
    class Program
    {
        static void Main(string[] args)
        {
            int Temperatur = 25;

            switch (Temperatur)
     
            {
                    case 1:
                    if (Temperatur <= 0)
                    {
                        Console.WriteLine("Es ist kalt");
                    }
                    break;
                    case 2:
                    if (Temperatur >= 25)
                    {
                        Console.WriteLine("Es ist überdurchschnittlich warm");
                    }
                    break;
                    case 3:
                    if (Temperatur <= 13)
                    {
                        Console.WriteLine("Es ist mild");
                    }
                    break;
                    
                


            };

        }
    }
}
Community
  • 1
  • 1
Draven Main
  • 29
  • 1
  • 3
  • It works perfectly fine. Just use the only input (`3`) that meets both `switch` cases and `if` conditions (like 1 is never less or equal than 0) – Alexei Levenkov Feb 02 '20 at 20:04

3 Answers3

3

Your code doesn't make much sense. When Temperatur is exactly 1 or 2, it cannot be less than or equal 0 nor greater than or equal 25. So there's no way Temperatur has a value that gets any of the first two branches of the switch selected and additionally satisfies the if in that branch.

Just using if and else if does what you presumably want:

...
if (Temperatur <= 0)
{
    Console.WriteLine("Es ist kalt");
}
else if (Temperatur <= 13)
{
    Console.WriteLine("Es ist mild");
}
else if (Temperatur >= 25)
{
    Console.WriteLine("Es ist überdurchschnittlich warm");
}
...
sticky bit
  • 36,626
  • 12
  • 31
  • 42
1

your if block never reaches a situation where temperature is 0.

you only have cases for temperatures 1, 2 and 3 (case 1:), so if temperature is anything else than those, nothing will happen.

Therefore, you should use if/else statements:

if (Temperatur <= 0)
{
    Console.WriteLine("Es ist kalt");
}
else if (Temperatur <= 13)
{
    Console.WriteLine("Es ist mild");
}
else
{
    Console.WriteLine("Es ist überdurchschnittlich warm");
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Nyhlus
  • 60
  • 9
0

You Just need if-statement:

using System;

public class Example
{
   public static void Main()
   {
      if (Temperatur <= 0)
{
    Console.WriteLine("Es ist kalt");
}
else if (Temperatur <= 13)
{
    Console.WriteLine("Es ist mild");
}
else if (Temperatur >= 25)
{
    Console.WriteLine("Es ist überdurchschnittlich warm");
}
   }
}
Community
  • 1
  • 1
Hassan Hosseini
  • 410
  • 1
  • 4
  • 20
  • would there be a way to combine switch with if in this case? – Draven Main Feb 04 '20 at 17:48
  • 1
    @DravenMain ,When you use if statements, you have to use dots too.The compiler will not connect the "dots" and understand that the "break;" statement inside your if statement is linked to the switch statement. Instead it will try to link it to a loop, since "break;" statements on their own can only be used with loops, to break out of it. That means that your case block is missing its break statement to complete it. – Hassan Hosseini Feb 04 '20 at 22:12