Question

bad_at_coding on Thu, 21 Aug 2014 18:05:54


  thats my code and it says else if is a invalid expression.


 Console.WriteLine("would you perfer whats behind door 1,2 or 3?");
            string uservalue;
            uservalue = Console.ReadLine();
            if (uservalue == "1");
            {
                Console.WriteLine("You won a new car");
                Console.ReadLine();
            }
            else if (uservalue == "2");
            {
                Console.WriteLine("You won a new boat");
                Console.ReadLine();
            }
            else if (uservalue == "3");
            {
                Console.WriteLine("you won a new cat!");
                Console.ReadLine();
            }


Sponsored



Replies

Riced on Thu, 21 Aug 2014 18:42:27


Try removing ; at ends of if and else lines.

B. Sinke on Thu, 21 Aug 2014 18:47:34


Hey there,

Well first things first, your code has some problems.

  • The first if statement is immediately ended by the ";" char so the brackets and the code behind it won't behave as the suposed to be. And this also means that the compiler should yell against you for placing uncorrect brackets.
  • This problem as described above happens often in your code.

Now your actual problem -> There is no such keyword as elseif in C#. Though this can be easlly achieved.

Console.WriteLine("would you perfer whats behind door 1,2 or 3?");
            string uservalue;
            uservalue = Console.ReadLine();
            if (uservalue == "1")
            {
                Console.WriteLine("You won a new car");
                Console.ReadLine();
            }
            else 
            {
             if (uservalue == "2")
             {
                Console.WriteLine("You won a new boat");
                Console.ReadLine();
             }
            }
            else 
            {
             if (uservalue == "3")
             {
                Console.WriteLine("you won a new cat!");
                Console.ReadLine();
             }
            } 

This simple solution shows how the goal can be achieved by just moving the if statement inside the else statement.

I hope that answers your question. If you need any more help please do ask!

Regards Bram Sinke.

P.S. The errors should be fixed in the code sample shown above.


bad_at_coding on Thu, 21 Aug 2014 18:53:10


thank you helped a lot and it fixed it 

Dave Doknjas on Thu, 21 Aug 2014 22:25:20



Now your actual problem -> There is no such keyword as elseif in C#. Though this can be easlly achieved.

Console.WriteLine("would you perfer whats behind door 1,2 or 3?");
            string uservalue;
            uservalue = Console.ReadLine();
            if (uservalue == "1")
            {
                Console.WriteLine("You won a new car");
                Console.ReadLine();
            }
            else 
            {
             if (uservalue == "2")
             {
                Console.WriteLine("You won a new boat");
                Console.ReadLine();
             }
            }
            else 
            {
             if (uservalue == "3")
             {
                Console.WriteLine("you won a new cat!");
                Console.ReadLine();
             }
            } 

This simple solution shows how the goal can be achieved by just moving the if statement inside the else statement.

Yes - there is no "else if" keyword, but since C# doesn't care about white space, joining them is a perfectly acceptable practice and is actually the most common practice.  Most would find your nesting of 'if' blocks within 'else' blocks rather convoluted compared with simple "else if" blocks. 

Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter


B. Sinke on Sat, 23 Aug 2014 08:59:51


Hey Dave,

I looked back at my message and realized that. I just like the ordering of the code like that :P but you'r absolutley right!

Regards Bram