Skip to content

Can somebody explain why this simple code doesnt work

Christopher Clarkson

Asked Oct 8, 2006, 2:41 PM EDT2 answers
Share
LinkedInX

Can somebody explain why even when the number generated isnt the number in the if statement it still runs?



using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Random r = new Random();

int scores = r.Next(1, 7);

System.Console.WriteLine("{0}", scores);

System.Console.ReadLine();

if (scores == 4) ;

{

System.Console.WriteLine("=4");

System.Console.ReadLine();

}

}

}

}


Thanks

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    It doesn't work because you put a semicolen after the If statement declaration:

    ...

    if (scores == 4) ;
    {
       System.Console.WriteLine("=4");
       System.Console.ReadLine();
    }

    ...

    Take out the semi colen, so that the snippet looks like this:

    ...
    if (scores == 4)
    {
       System.Console.WriteLine("=4");
       System.Console.ReadLine();
    }

    ...

    And you should be fine.

    Later
    Mark

    0
  • 1 short reply
    • 0
      Good Eye Buddy
      0