ideas | discover | explore

Friday, September 9, 2011

C# 4 - What If?

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

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

3. Press F5 to run and see the results.
4. Thank you.

The If statement is used to add condition to a piece of code. Its syntax is:

     if()
  {
    //code
  }

condition – could be anything that would result in a true or false. It only runs the code inside its braces when the condition is true.

No comments: