Writing if-else Statements


Now that you know how to write conditional clauses and even how to combine multiple clauses, it's time to write if statements. if statements enable you to execute some code, either a single line of code or a block of code if the conditional evaluates to true. You can optionally execute a line of code or even a block of code if the expression is false.

To write an if-else statement:

  1. Type if .

  2. Type an open parenthesis ( .

  3. Type a clause that evaluates to a Boolean value (bool in C#).

  4. Type a close parenthesis ) .

  5. If you wish to execute a single statement type the statement after the parenthesis (most of the time people write the statement in the next line) and type a semicolon ( Figure 3.15 ), and skip to step 9.

    Figure 3.15 If statements have an expression inside parenthesis. If the expression results in true then you can execute a single statement as shown here.
     if (  items.Count == 0  )    HideGrid(); 
  6. If you wish to execute more than one statement, type an open curly bracket { .

  7. Write the statements you wish to execute if the condition evaluates to true.

  8. Type a close curly bracket } ( Figure 3.16 ).

    Figure 3.16 With if statements you can also execute an entire code block if the expression is true.
      if (qty >= 0 && name != "")   {  //create new purchased item    PurchasedItem pi = new PurchasedItem();    pi.SetInfo(name,qty);    items.Add(pi);    ShowGrid();    UpdateGrid();  }  
  9. If you would like to execute alternate statements if the condition evaluates to false, then type the word else .

  10. If you have only a single alternate statement, then type the statement and end with a semicolon ; ( Figure 3.17 ), then skip the remaining steps.

    Figure 3.17 else is the evil counterpart of if. It gets executed when the expression results in false. You can see that you can execute a single statement if you like in the else.
     if (balance > 0)    PurchaseNewPhone();  else   KeepOldPhone();  
  11. If you wish to execute more than one statement, type an open curly bracket { .

  12. Type the statements for the else condition.

  13. Type a close curly bracket } ( Figure 3.18 ).

    Figure 3.18 As with if, else also supports the execution of an entire code block.
     if (balance > 0)    PurchaseNewPhone();  else   {   BegSpouseForMoney();   PurchaseNewPhone();   }  

graphics/tick.gif Tip

  • If you want to add another if statement as part of the else clause, simply type if after the else clause and repeat steps 18 ( Figure 3.19 ).

    Figure 3.19 In the code below there is a second if condition as part of the first else. The second else is part of the second if condition. It gets executed if the else if condition results in false.
     if (balance > 0)    PurchaseNewPhone();  else if (IsTodayBirthday())   AskParentsForNewPhone();   else   {   BegSpouseForMoney();   PurchaseNewPhone();   }  



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net