5.3. if-else StatementsΒΆ

Run the example program, Clothes.cs. Try it at least twice, with inputs 50 and then 80. As you can see, you get different results, depending on the input. The main code of Clothes.cs is:

1
2
3
4
5
6
7
8
9
      Console.Write ( "What is the temperature? ");
      double temperature = double.Parse(Console.ReadLine());
      if (temperature > 70) {
           Console.WriteLine("Wear shorts.");
      }
      else {
          Console.WriteLine("Wear long pants.");
      }
      Console.WriteLine("Get some exercise outside.");

The lines labeled 3-8 are an if-else statement. Again it is close to English, though you might say “otherwise” instead of “else” (but else is shorter!). There are two indented statements in braces: One, like in the simple if statement, comes right after the if condition and is executed when the condition is true. In the if-else form this is followed by an else (lined up under the if by convention), followed by another indented statement enclosed in braces that is only executed when the original condition is false. In an if-else statement exactly one of two possible parts in braces is executed.

A final line is also shown that is not indented, about getting exercise. The if and else clauses each only embed a single statement as option, so the last statement is not part of the if-else statement. Instead it is a part of the normal sequential flow of statements. It is always executed after the if-else statement, no matter what happens inside the if-else statement. Again: inside the if-else there is a choice made, but the whole if-else construction is a single larger statement, which exists in the normal sequential flow. The compiler does not require the indentation of the if-true-statement and the if-false-statement, but it is a standard style convention.

The general C# if-else syntax is

if ( condition ) {
statement(s) for if-true
}
else {
statement(s) for if-false
}

The statements chosen based on the condition can be any kind of statement. This is the suggested form, but as with the plain if statement, the if-true compound statement or the if-false compound statement can be replace by a single statement without braces, except in one otherwise ambiguous situation discussed later with two ifs and an else.

More on Compound Statements and Scope

The section on local scope referred to function and method bodies, which happen to be enclosed in braces, making a compound statement. If fact braces limit the scope of things declared inside, wherever they appear.

As a result the following code makes no sense:

static int BadScope(int x)
{
   if ( x < 100) {
      int val = x + 2;
   else {
      int val = x - 2:
   }
   return val;
}

The if-else statement is legal, but useless, because whichever compound statement gets executed, val ceases being defined after the closing brace of its compound statement, so the val in the return statement has not been declared or given a value. If we want val be used inside the braces and to make sense past the end of the compound statement, it cannot be declared inside the braces. Instead it must be declared before the compound statements that are parts of the if-else statement. The following would work:

static int OkScope(int x)
{
   int val;
   if ( x < 100) {
      val = x + 2;
   else {
      val = x - 2:
   }
   return val;
}

Previous topic

5.2. Simple if Statements

Next topic

5.4. More Conditional Expressions

This Page