There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Ran into a bit of a problem with the last exercise when I ran the statement through the terminal. I put Console.WriteLine(); before string genre = Console.ReadLine(); but when I ran the statement with a genre through the terminal it comes up with errors. I am wondering if anyone figured out a solution to this… Thanks.
I see that switch statement is more compact than else if statement.
is there any limitation that prevents programmer from using switch statement over else if statement?
Memory and performance. If uses a lot less in performance, although when there are a lot OR expressions, switch offers more readability. Still, it’s way slower than if-else-if, at some points 12 times slower…
“So the preference of C# Conditional Statements goes in this order - #1 - If- Else If -> #2 Multiple If -> #3 Switch case.”
Here’s a link if you want to learn more about it. Generally, there are things in programming that are considered good or bad practice so questions like this are very important
Good luck and happy coding! Performance Consideration For C# Conditional Statements
using System;
namespace SwitchStatement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What genre of movies do you enjoy?");
string genre = Console.ReadLine();
switch (genre)
{
case "Drama":
Console.WriteLine("Citizen Kane");
break;
case "Comedy":
Console.WriteLine("Duck Soup");
break;
case "Adventure":
Console.WriteLine("King Kong");
break;
case "Horror":
Console.WriteLine("Psycho");
break;
case "Science Fiction":
Console.WriteLine("2001: A Space Odyssey");
break;
default:
Console.WriteLine("No movie found.");
break;
}
}
}
}
Hi robot1cfunk, take a second look at the “default” keyword. For me, that’s where there error was. “:” vs “;”. You can verify by checking the line number holding keyword against the line number mentioned in the error. Also, remember to click “Run” to save the code after making changes, and then run the code using “dotnet run” in the console. I hope this helps!
When I run the file it gives me this:
“System.IO.FileNotFoundException: Unable to find the specified file.
at Interop.Sys.GetCwdHelper(Byte* ptr, Int32 bufferSize)
at Interop.Sys.GetCwd()
at System.Environment.get_CurrentDirectory()
at System.IO.Directory.GetCurrentDirectory()
at Microsoft.DotNet.Tools.Run.RunCommand.Initialize()
at Microsoft.DotNet.Tools.Run.RunCommand.Execute()
at Microsoft.DotNet.Tools.Run.RunCommand.Run(String args)
at Microsoft.DotNet.Cli.Program.ProcessArgs(String args, ITelemetry telemetryClient)
at Microsoft.DotNet.Cli.Program.Main(String args)”
This is my code:
using System;
namespace SwitchStatement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pick a genre");
string genre = Console.ReadLine();
switch (genre)
{
case "Drama":
Console.WriteLine("Citizen Kane");
break;
case "Comedy":
Console.WriteLine("Duck Soup");
break;
case "Adventure":
Console.WriteLine("King Kong");
break;
case "Horror":
Console.WriteLine("Psycho");
break;
case "Science Fiction":
Console.WriteLine("2001: A Space Odyssey");
break;
default:
Console.WriteLine("No movie found.");
break;
}
}
}
}
Hi
I wrote the code like this and it seems some problem while run in .NET. Any kind of suggestion would be good. Thanks in advance.
using System;
namespace SwitchStatement
{
class Program
{
static void Main(string args)
{
Console.WriteLine(“Chosse a movie genre”);
string genre = Console.ReadLine();
switch (genre)
{
case "Drama":
Console.WriteLine("Citizen Khan");
break;
case "Comedy":
Console.WriteLine("Duck Soup");
break;
case "Adventure":
Console.WriteLine("King kong");
break;
case "Horror":
Console.WriteLine("Psycho");
break;
case "Science Fiction":
Console.WriteLine("A Apace odyssey") ;
break;
default:
Console.WriteLine("No Movie Found");
break;
}
}
I wrote code similar to how everyone did, but when I was testing I realized it was case sensitive. I tried to figure out a way to capitalize the first letter of user’s input but was unsuccessful. So, I figured I’d just make their answer all lower then change the if/else arguments to all lower as well. This seemed to work without needing the user to be specific with caps.
using System;
namespace SwitchStatement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Choose genre:");
string genre = Console.ReadLine();
genre = genre.ToLower();
switch (genre)
{
case "drama":
Console.WriteLine("Citizen Kane");
break;
case "comedy":
Console.WriteLine("Duck Soup");
break;
case "adventure":
Console.WriteLine("King Kong");
break;
case "horror":
Console.WriteLine("Psycho");
break;
case "science fiction":
Console.WriteLine("2001: A Space Odyssey");
break;
default:
Console.WriteLine("No movie found");
break;
}
}
}
}
string genre = Console.ReadLine(); does complete it but is wrong because you can’t declare a variable twice.
genre = Console.ReadLine(); is the correct way to make it function, because the variable already exists and you want to change the value, not declare it a second time.
First, create a string variable named genre and save the value "Horror" to it.
Step 5 specifies:
… Swap out"Horror" for Console.ReadLine() to get the user’s response and save it to genre…
Step 5 doesn’t want us to create a new statement from scratch. It wants us to edit the statement from Step 1 by deleting the string "Horror" and replacing it with Console.ReadLine(). So, you still end up with a single declaration and initialization.