ideas | discover | explore

Thursday, September 29, 2011

C# 8 - For loop statement

1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Hello World!");
            }
            
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

3. Press F5 to run and see the results.



4. Thank you.


The for-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. Its syntax is:

for(<initialization>;<condition>;<incrementation/decrementation>)
{
                //code to execute
}

Wednesday, September 21, 2011

C# 7 - Switch statement

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

 static void Main(string[] args)
        {
            Console.WriteLine("What is your most favorite sweet stuff?");
            Console.WriteLine("[1]Chocolate");
            Console.WriteLine("[2]Candy");
            Console.WriteLine("[3]Lollipop");
            Console.WriteLine("[4]Sugar");
            Console.WriteLine("[5]Honey");
            Console.WriteLine("[6]Cake");
            Console.Write("Enter your choice: ");
            string choice = Console.ReadLine();
 
            switch (choice)
            {
                case "1":
                    Console.WriteLine("Chocolate, chocolate, chocolate!");
                    break;
                case "2":
                    Console.WriteLine("Candy, candy, candy!");
                    break;
                case "3":
                    Console.WriteLine("Lollipop, lollipop, lollipop!");
                    break;
                case "4":
                    Console.WriteLine("Sugar, sugar, sugar!");
                    break;
                case "5":
                    Console.WriteLine("Honey, honey, honey!");
                    break;
                case "6":
                    Console.WriteLine("Cake, cake, cake!");
                    break;
                default:
                    Console.WriteLine("Hmmm, I don't know what you're favorite is.");
                    break;
            }
            
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
 
        }
3. Press F5 to run and see the results.

4. Thank you.

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

switch(<expression>)
                case <expression result>:
                                //code to execute
                                break;
                case <expression result>:
                                //code to execute
                                Break;
                .
                .
                .
                default:
                                //code to execute if there are no matching case
                                break;


expression – could be anything that would result in a value that would get evaluated in each of the cases.

Sunday, September 18, 2011

C# 6 - – What If? Or, Else If....?


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



        static void Main(string[] args)
        {
            Console.WriteLine("What is your most favorite sweet stuff?");
            Console.WriteLine("[1]Chocolate");
            Console.WriteLine("[2]Candy");
            Console.WriteLine("[3]Lollipop");
            Console.WriteLine("[4]Sugar");
            Console.WriteLine("[5]Honey");
            Console.WriteLine("[6]Cake");
            Console.Write("Enter your choice: ");
            string choice = Console.ReadLine();
 
            if (choice=="1")
            {
                Console.WriteLine("Chocolate, chocolate, chocolate!");
            }
            else if (choice == "2")
            {
                Console.WriteLine("Candy, candy, candy!");
            }
            else if (choice == "3")
            {
                Console.WriteLine("Lollipop, lollipop, lollipop!");
            }
            else if (choice == "4")
            {
                Console.WriteLine("Sugar, sugar, sugar!");
            }
            else if (choice == "5")
            {
                Console.WriteLine("Honey, honey, honey!");
            }
            else if (choice == "6")
            {
                Console.WriteLine("Cake, cake, cake!");
            }
            else
            {
                Console.WriteLine("Hmmm, I don't know what you're favorite is.");
            }
 
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

3.       Press F5 to run and see the results.

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



if(<condition>)
     //code
else if(<condition>)
     // any other code
else
     //this code will execute if there are no matching conditions


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.


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


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.

Tuesday, September 6, 2011

C# 3 - Variable Scope

1.  Create a new console project.
2. Modify Program.cs with the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace CSharpEpisode3
{
    class Program
    {               
        static void Main(string[] args)
        {
            VariableExample variableExample = new VariableExample();
            Console.WriteLine(variableExample.GlobaVariable);
            variableExample.VariableTest();

            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }
    }

    class VariableExample
    {
        public string GlobaVariable = "I'm a global variable"; //available both inside and outside this class
        private string PrivateVariable = "I'm a private variable within VariableExample class"; //available only inside this class

        public void VariableTest()
        {
            string localVariable = "I'm a local variable within VariableTest() method"; //available only within this method VariableTest()
            Console.WriteLine(localVariable);
            Console.WriteLine(PrivateVariable);
        }
    }
}



3. Press F5 to run.
4. Thank you.