1.13 Using if Statements

 <  Day Day Up  >  

1.13 Using if Statements

You want your application to perform a certain action based on the result of evaluating an expression.


Technique

Using a combination of relational operations on variables , you can execute a body of code if the expression evaluates to true or evaluate a different body of code if not. Use the if / else statement to control program flow.

Comments

One of the most widely used conditional statements is the if / else statement. The format of an if / else statement follows :

 
 if( expr ) { } else { } 

You read this code as "If expr is true, then perform these actions; else, perform these actions." In the previous recipe, you used relational operators to compare values and objects with one another. You can then combine the result of these relational expressions using logical expressions. You use the results of these comparisons within the if statement, which is known collectively as a conditional statement .

A shorthand way of creating an if statement is to use a ternary operator. The format of a ternary operator is

 
 (  conditional  )?  true_expression  :  false_expression  

As an example, if you want to test whether a variable is equal to a certain number, you write the following:

 
 Console.Write(( i==42 )? "Meaning of life" : "I don't know"); 

A common technique is to create multiple if statements to further refine the conditional comparisons of variables. For example, the following is perfectly legal and you'll notice that it can perform one of several different actions based on the conditions that are present. Listing 1.6 demonstrates how to create multiple if statements.

Listing 1.6 Creating Multiple if Statements to Control Several Actions
 using System; namespace _13_IfStatement {     /// <summary>     /// Summary description for Class1.     /// </summary>     class Class1     {         /// <summary>         /// The main entry point for the application.         /// </summary>          [STAThread]         static void Main(string[] args)         {             string input;             Console.Write( "Enter your name: " );             input = Console.ReadLine();             if( input.ToLower() == "jordan" )             {                 Console.WriteLine( "{0}, have you cleaned " +                     "your room yet?", input );             }             else if( (input.ToLower() == "jake")                       (input.ToLower() == "jonah") )             {                 Console.WriteLine( "{0}, clean your room!", input );             }             else if( input.ToLower() == "mallorie" )             {                 Console.WriteLine( "{0}, you may do anything you " +                     "want.", input );             }             else             {                 Console.WriteLine( "Sorry {0}, I don't know you",                     input );             }         }     } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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