Conditional Statements

3xodus

Freshman
Joined
Dec 26, 2004
Messages
47
Location
Derbyshire, UK
Hi again all,

Sorry for the basic question, but I'm having trouble finding this information. In MSDN and on the various C# Tutorials and websites I've read, they give examples but don't give information on the flow of the program in an if statement in C#.

The particular code I'm having trouble with is:
C#:
  do
   {
       // get data to perform conditional statement on
       if (a != b)
       {
           if ( c == d )
               // do something
           if ( c != d )
           {
               // do something different
           }
       }
   }
       
   while ((a == b) && (a != -1));
   MessageBox.Show("Done");
   }

I want this section of code to loop until either a != b, or a == -1, but it seems it's not looping and I believe this is because of my if statements. To be honest, I'm not sure that much of the above code is correct (it builds, but I think it's not the result I was aiming for)

I'm used to VB, where I can do End If to end the if statement, and the program will continue to the line after "End If" - it seems to me that in C# that isn't the case, but since I haven't been able to find much good information I'm not sure.

Please could anyone explain how C# if statements work? As I understand it at the moment, the "}" at the end of the statement ends not only the if statement, but the whole function which the code is in (unless a certain result is returned), but I can't figure out how, if that is the case, it works.

Any help or links are greatly appreciated - I suppose since this is a basic subject, the info is out there - I've just managed to miss it :mad:
 
If statements work like this:

if (something is true)
{
do this code
}
elseif (something else is true)
{
do this code
}
else
{
default
}

In C#, '{}' are scope deliminators. What that means is, that they control what variables and such have scope and where. For instance:

[CS]
if ( a == b)
{
int c = 5;
Console.WriteLine("Equal");
}

c = 10 //error, since it's undefined for this scope
[/CS]

in this case c is only defined for inside the if statement. You said you want it to loop if either a != b or a !=1, which is correctly done in your code. It is good practice to put deliminators after all if statements, but if you have only 1 statement after it, you don't have to use it (though I always do). for example:

[CS]
if(a == 1)
Console.WriteLine("A == 1");
[/CS]

is the same as

[CS]
if(a == 1)
{
Console.WriteLine("A == 1");
}
[/CS]

this again, is only for a single statement afterwords and I don't recommend you use this shortcut. Hopefully this helps.
 
Thanks for the reply coldfusion244 - the information on scope deliminators is very useful :)

I've just worked out why my code above wasn't working and it turns out it wasn't a problem with the if statements, however I still need to learn how to use them properly.

Would you mind explaining the use of "default" in your example coldfusion? Other than that, thanks a lot for your example ;)

I think I'm going to have to make a sample project with different conditional statements and nested statements and such to get used to them.

Oh - Also, since variables defined within scope deliminators are only availible within that scope, what is the proper way to declare a variable that I may need both inside of and outside of an if statement? Up to now I've been putting them after the "static void Main()" function, but not inside any function.

Thanks again for your reply!
 
If a variable is only needed within a single function, declare it within that function; this helps reduce the chance of either cluttering up the namespace with lots of similar variable names, or accidentaly changing the value of a variable when calling another method.
If the variable needs to be used in more than one function then either consider passing it as a parameter or declaring it at the class level (outside of a function).

The use of default above just means whatever processing should be done if neither of the if statement conditions are met.
 
Back
Top