-
switch
Heya;
Im a newbie in C# and Im trying to loop 5 times in a console app. However the program exits on the second entry. The problem seems to be the For control . Anyone can point out to me the mistake pls/
Code:
namespace ConsoleSwitch
{
class Program
{
static void Main(string[] args)
{
int x;
string colour,str;
string Blue,Red, Yellow ;
Console.WriteLine("Enter the primary colours colour");
colour = Console.ReadLine();
for (x = 1; x <= 5; x++) ;
switch (colour)
{
case "Blue":
Console.WriteLine("Blue is a primary colour");
//str = Console.ReadLine();
break;
case "Red":
Console.WriteLine("Red is a primary colour");
//str = Console.ReadLine();
break;
case "Yellow":
Console.WriteLine("Yellow is a primary colour");
break;
case "White":
Console.WriteLine("White is a primary colour");
break;
case "Black":
Console.WriteLine("Black is a primary colour");
break;
default:
Console.WriteLine("Colour is NOT a primary colour");
//str = Console.ReadLine();
break;
} //end of switch
str = Console.ReadLine();
} //end of For
}
-
Re: switch
You have an empty 'for' loop - the semi-colon at the end of the statement is an empty statement that is the only subject of the loop.
-
Re: switch
As David says. The semicolon after your 'for' statement is the equivalent of Next in VB. If the 'for' loop contains more than one statement then you must enclose those statements in braces to indicate that they constitute a single block.
-
Re: switch