// Check if amount equals $1.00
if (totalChange >= 100)
{
MessageBox.Show("Equal to $1.00");
else
MessageBox.Show("Not equal to $1.00");
}
Getting error with if/else statement
Page 1 of 14 Replies - 126 Views - Last Post: 20 February 2013 - 09:23 AM
#1
Getting error with if/else statement
Posted 19 February 2013 - 05:48 PM
I'm sure the syntax is right.
Replies To: Getting error with if/else statement
#2
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.
#3
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.
#4
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.
#5
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:
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.
Make sense?
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?
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|