Conditional+Statements

=Conditional Statements= <-- Prev - Contents - Next -->

So far we've seen how to create and manipulate variables, and how to print them to the screen. But what if we want our program to make decisions based on our variables. For example, suppose we have two integer variables, v1 and v2, and we want to print "v1 is bigger" if v1 is bigger, and "v2 is bigger" if v2 is bigger. That's where conditional statements come into play. Conditional statements allow us to specify //conditions -// an expression that may or may not be true - and a block of code to execute if that condition IS true.

The basic conditional statement is the if-statement. It looks like this: code if ( expression ) { block of code } code

The code above (which is not valid C# code) will check to see if whatever we plug in for is true. If it IS true, it will execute whatever we put between in the block of code that follows it. For example, going back to our variables v1 and v2... let's check if v1 is greater than v2. If so, let's print "v1 is bigger". It would look like this: code if ( v1 > v2 ) { System.Console.WriteLine("v1 is bigger"); } code What if we then want to check if v2 is bigger than v1, and print a different message if it is? Obviously, we could put the following after the code: code if ( v1 > v2 ) { System.Console.WriteLine("v1 is bigger"); } if ( v1 < v2 ) { System.Console.WriteLine("v2 is bigger"); }

code But this is inefficient. Why? Because if v1 > v2, then it's not possible for v1 < v2... so there's no need to execute the second if-statement. The proper way to handle this is an else-if. An else-if looks like this: code if ( expression ) { code_block_1 } else if ( new_expression ) { code_block_2 }

code Hopefully, it should seem intuitive what this does. It checks the first expression, and executes code_block_1 if expression is true. If it IS true, it skips the "else if" and code_block_2 entirely. If the first expression is false, it checks new_expression, and executes code_block_2 if it's true. So, going back to our example, we would say: code if ( v1 > v2 ) { System.Console.WriteLine("v1 is bigger"); } else if ( v1 < v2 ) { System.Console.WriteLine("v2 is bigger"); }

code Much better. Also, what's neat is that you can have as many else-if statements attached to an if statement as you want! Which means we can even do the following: code if ( v1 > v2 ) { System.Console.WriteLine("v1 is bigger"); } else if ( v1 < v2 ) { System.Console.WriteLine("v2 is bigger"); } else if ( v1 == v2 ) { System.Console.WriteLine("v1 equals v2"); }

code The "==" symbol is not a typo; to compare two numbers to each other in C#, you use a double equal sign. A single "=" is reserved for assigning values to variables. So in C#, "v1 = v2" sets v1 to the same value as v2, while "v1==v2" asks the question "is v1 equal to v2?" and returns true or false accordingly.

Finally, there is a way to specify a default case in our if statements - the "else" statement. The else statement basically says "if none of the other if and else-if statements are true, execute this block of code instead." To demonstrate the else statement, we could replace the last else-if from our previous example with an else. If v1 is not greater than v2, and v2 is not greater than v1, then they MUST be equal... so there is no need to explicitly check. So, the following does the same exact thing as before: code if ( v1 > v2 ) { System.Console.WriteLine("v1 is bigger"); } else if ( v1 < v2 ) { System.Console.WriteLine("v2 is bigger"); } else { System.Console.WriteLine("v1 equals v2"); } code Notice there is no condition for an else statement; it is the default. You may have only one else-statement associated with any if-statement... although you may have as many else-if statements before the else as you want.

The if,else-if,else combination allows us to express a very wide variety of conditional statements. Did you know that you can even nest if-statements inside of one another? Here's an example: code if ( v1 > v2 ) { if ( v1 > 0 ) {   System.Console.WriteLine("v1 is bigger than v2 AND v1 is bigger than 0"); } else {   System.Console.WriteLine("v1 is bigger than v2 BUT v1 is NOT bigger than 0"); } } code You can nest if-statements as many times as you want, although your code may start to look confusing! You can see how complex this can get.

What if we wanted to check more than one condition in an if statement? For example, what if we wanted to print something only if v1 was bigger than v2 AND v1 was positive? For that, we could use nested if-statements... but that can quickly lead to confusing code. Instead, it's much simpler to use the and-operator. The //double-ampersand// (&&) is used to represent "and" in C# expressions. For example, the previously described if-statement could be accomplished with the following: code if ( v1 > v2 && v1 > 0 ) { System.Console.WriteLine("v1 is bigger than v2 AND v1 is greater than 0"); } code You can "and together" as many conditions you want in one expression, so the following is also valid: code if ( v1 > v2 && v1 > 0 && v2 > 0 )    ... etc etc code This can make for long expressions, so you may find it useful to enclose each **clause** in parenthesis: code if ( (v1 > v2) && (v1 > 0) && (v2 > 0) ) code but this is not necessary. Similar to &&, you may want to use an "or" in your expression. For example, what if we wanted to check if either v1 OR v2 was positive? The or-operator in C# is "||". Note that those are "pipes", and are usually shift+"\" on most keyboards. So, the following accomplishes our example: code if ( (v1 > 0) || (v2 > 0) ) { System.Console.WriteLine("Either v1 or v2 is positive"); } code Note that the above if-statement will be true if v1 or v2 are positive, or if BOTH are positive.

This should give you most of the tools to build the if-statements that you want. You may chain together &&'s and II's, but use parentheses to group things the way you want, otherwise they may seem ambiguous. For example, the following two expressions are NOT the same: code if ( (false && true) || (true && true) )  ... returns true if ( false && (true || true) && true )  ... returns false

code So, without parenthesis: code if ( false && true || true && true )  ... ambiguous code You may or may not get the results you expect.

Also, remember you may use arithmetic in your expressions as well. So, for example: code if ( (v1+2) > 2 ) { System.Console.WriteLine("v1 was greater than 0!"); }

code

So that's it! There are a few more operators that you may need in your if-statements- for example, what if you want to see if something is NOT equal to something else? The following table summarizes the operators you may need.


 *  **Symbol** ||  **Meaning** ||
 * > || greater than ||
 * < || less than ||
 * >= || greater than or equal to ||
 * <= || less than or equal to ||
 * == || equal to ||
 * != || not equal to ||
 * && || AND ||
 * ``|````|`` |||| OR ||

Consider the following Main function: code int a = 1; int b = 2; int c = 3;
 * Pop Quiz **:

if ( a == 200 ) {  System.Console.WriteLine("A"); } else if ( b == 2 ) {  System.Console.WriteLine("B"); } else if ( c == 3 ) {  System.Console.WriteLine("C"); } else {  System.Console.WriteLine("Default"); } System.Console.WriteLine("End"); code What would be the output of our program? media type="custom" key="3955795"

<-- Prev - Contents - Next -->