4 Replies - 346 Views - Last Post: 20 February 2013 - 09:23 AM Rate Topic: -----

#1 Afrosephurai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-February 13

Getting error with if/else statement

Posted 19 February 2013 - 05:48 PM

I'm sure the syntax is right.

// Check if amount equals $1.00
                if (totalChange >= 100)
            {    
           
                MessageBox.Show("Equal to $1.00");
            
            
                else

                MessageBox.Show("Not equal to $1.00");
            }
           



Is This A Good Question/Topic? 0
  • +

Replies To: Getting error with if/else statement

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,500
  • Joined: 05-May 12

Re: Getting error with if/else statement

Posted 19 February 2013 - 06:03 PM

Not with the braces there, it's not correct. Try removing the braces and you'll get it to compile nicely.
Was This Post Helpful? 0
  • +
  • -

#3 Afrosephurai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-February 13

Re: Getting error with if/else statement

Posted 19 February 2013 - 06:05 PM

Thanks, I did, I have a bad habit of not cleaning up the code so the braces line up with the according classes and namespaces.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,500
  • Joined: 05-May 12

Re: Getting error with if/else statement

Posted 19 February 2013 - 07:23 PM

I recommend picking and consistently applying an indent style when you write code. It will help you spot issues like the above more easily.
Was This Post Helpful? 1
  • +
  • -

#5 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Getting error with if/else statement

Posted 20 February 2013 - 09:23 AM

Removing the braces fixes it in this instance, but I wonder if it isn't a fundamental understanding issue (if it isn't, forgive me).

When you only execute one line of code per condition, then this is fine:
if (condition)
    DoStuff();
else
    DoOtherStuff();



But you [i]will need those curly braces if you execute multiple lines of code per condition. But a set of curly braces belongs to each condition, and there isn't a set for the entire block.

if (condition)
{
    var thingy = GetThingy();
    DoStuff(thingy);
}
else
{
    var thingy = GetOtherThingy();
    DoOtherStuff(thingy);
}



Make sense?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1