C# C# Basics If Statements "else if" and "else" Statements

Ilya Sikharulidze
PLUS
Ilya Sikharulidze
Courses Plus Student 1,072 Points

Can someone please help me, I don't understand how to use 'return' value in this case

Program.cs
using System;

class Program
{

    static string CheckSpeed(double speed)
    {

        if(speed > 65.0)
        {
             Console.WriteLine("too fast");
        }
        else if(speed < 45.0)
        {
            Console.WriteLine("too slow");
        }
        else
        {
            Console.WriteLine("speed OK");
        }// YOUR CODE HERE

    }

    static void Main(string[] args)
    {
        // This should print "too slow".
        Console.WriteLine(CheckSpeed(44));
        // This should print "too fast".
        Console.WriteLine(CheckSpeed(88));
        // This should print "speed OK".
        Console.WriteLine(CheckSpeed(55));
    }

}

5 Answers

Steven Parker
Steven Parker
203,231 Points

Ilya Sikharulidze — Did you mean to mark your own comment as "best answer"? :see_no_evil:
(You can still change it)

Steven Parker
Steven Parker
203,231 Points

You're pretty close there. But instead of outputting the strings directly (using "Console.WriteLine"), your code should return the string values (using "return").

Ilya Sikharulidze
Ilya Sikharulidze
Courses Plus Student 1,072 Points

Thanks, I understand that, but I don't understand how to exactly to implement the "return"

Steven Parker
Steven Parker
203,231 Points

Where you currently have the term "Console.WriteLine" (in 3 places), replace it with the word "return". Optionally, you can also remove the parentheses around the string (replacing the open parenthesis with a space).

Michal Lysiak
Michal Lysiak
9,650 Points

Program.cs(6,19): error CS0161: 'Program.CheckSpeed(double)': not all code paths return a value [/workdir/workspace.csproj] Program.cs(27,9): error CS0127: Since 'Program.Main(string[])' returns void, a return keyword must not be followed by an object expression [/workdir/workspace.csproj] Program.cs(29,9): error CS0127: Since 'Program.Main(string[])' returns void, a return keyword must not be followed by an object expression [/workdir/workspace.csproj] Program.cs(31,9): error CS0127: Since 'Program.Main(string[])' returns void, a return keyword must not be followed by an object expression [/workdir/workspace.csproj] Program.cs(29,9): warning CS0162: Unreachable code detected [/workdir/workspace.csproj]

The build failed. Please fix the build errors and run again.

I've done as you advise and still not working

Michal Lysiak
Michal Lysiak
9,650 Points

using System;

class Program {

static string CheckSpeed(double speed)
{

    if(speed > 65.0)
    {
         Console.WriteLine("too fast");
    }
    else if(speed < 45.0)
    {
        Console.WriteLine("too slow");
    }
    else
    {
        Console.WriteLine("speed OK");
    }// YOUR CODE HERE

}

static void Main(string[] args)
{
    // This should print "too slow".
    return(CheckSpeed(44));
    // This should print "too fast".
    return(CheckSpeed(88));
    // This should print "speed OK".
    return(CheckSpeed(55));
}

}

what's wrong?

Steven Parker
Steven Parker
203,231 Points

Instead of posting a question as an "answer" to another one, always start a fresh new question and show your code there.

But here's a hint: all your code should go in the "CheckSpeed" routine, the "Main" should remain as it was supplied.

Michal Lysiak
Michal Lysiak
9,650 Points

Yeah, but it still doesn't work and shows me an error as above :(