ideas | discover | explore

Wednesday, September 14, 2011

C# 5 - What If? Or, Else ?

Manage and share your stuff for FREE!
www.meestuf.com
-----------------------------------------------------------------------------------------------------

1. Create a new console project.
2.  Edit the Program.cs file as shown below.

static void Main(string[] args)
{
           string favoritePet = "Dog";
 
           Console.WriteLine("My favorite pet is a " + favoritePet);
                   
              if (favoritePet == "Cat")
           {
                   Console.WriteLine("Then it says: Meow!");
           }
           else
           {                                  
                    Console.WriteLine("Then it says: Woof!");
           }
   
           Console.WriteLine("Press any key to continue....");
           Console.ReadLine();
  }

3. Press F5 to run and see the results.



4. Thank you.


The if-else statement is used to add condition to a piece of code. Its syntax is:

if()
      //code
else 
      // any other code

condition – could be anything that would result in a true or false. "if" only runs the code inside its braces when the condition is true. If the condition fails or evaluates to false the code under the else will get executed.

-----------------------------------------------------------------------------------------------------

Manage and share your stuff for FREE!
www.meestuf.com


No comments: