Conditional Statements

3xodus

Well-known member
Joined
Dec 26, 2004
Messages
47
Location
Derbyshire, UK
Hi again all,

Sorry for the basic question, but Im having trouble finding this information. In MSDN and on the various C# Tutorials and websites Ive read, they give examples but dont give information on the flow of the program in an if statement in C#.

The particular code Im 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 its not looping and I believe this is because of my if statements. To be honest, Im not sure that much of the above code is correct (it builds, but I think its not the result I was aiming for)

Im 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 isnt the case, but since I havent been able to find much good information Im 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 cant 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 - Ive 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 its 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 dont 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 dont recommend you use this shortcut. Hopefully this helps.
 
Thanks for the reply coldfusion244 - the information on scope deliminators is very useful :)

Ive just worked out why my code above wasnt working and it turns out it wasnt 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 Im 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 Ive 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