Saturday, April 30, 2016

CSharp Statements and Expressions

CSharp Statements and Expressions

In C# a statement is a line of code which form part of the program flow. Each line of code may perform an action or it may contain a non-action piece of code. Lets see how many types of C# statements we may write and then we will see a complete program using all these types of statements.

Types of Statements

  1. Declare Statements
  2. Assignment Statements
  3. Function Call Statements
  4. Return Statements
  5. Break Statement
  6. Conditional Statements
  7. Loop Statements

Expressions in C# are a combination of one or more variables, operators, a function call that would be processed and gives back a value. This value may be used by other expressions or contexts. Below are few examples of expressions

  • a>10
  • x = 2+3;
  • counter == 100
  • Math.Pow(2,4);


Lets try to understand them with example program. You can copy this program into a Visual Studio Console Application Project. Copy the code into Program.cs file and you are good to go. Hit F5 and you have an interactive application.

Example:


using System;
namespace SampleConsoleApp
{
    class Program
    {
        static void Main()
        {

            //Declare Statements

            double counter;

            string DoWhat; //1: Calculate Power, 2: Mutiply


            //Assignment Statements 

            counter = 10;
            DoWhat = "";

            while (true)

            {
                Console.WriteLine("");
                Console.WriteLine("Press One of these Options");
                Console.WriteLine("1. Power Calculation");
                Console.WriteLine("2. Multiples");
                Console.WriteLine("3. Say Hello");
                Console.WriteLine("0. Exit");
                DoWhat = Console.ReadLine();

                Console.WriteLine("You selected " + DoWhat);


                if (DoWhat.Equals("1"))  //Conditional Statements

                {
                    for (double i = 0; i < counter; i++) //Loop Statement and Expressions
                    {
                        PrintPower(i, (i - 2)); //Function Call Statement
                    }
                }
                else if (DoWhat.Equals("2"))  //Conditional Statements
                {
                    for (int times = 0; times < 5; times++) //Loop Statement
                    {
                        PrintMultiples(times, 6);//Function Call Statement
                    }
                }
                else if (DoWhat.Equals("3"))  //Conditional Statements
                {
                    SayHello();
                }
                else if (DoWhat.Equals("0"))  //Conditional Statements
                {
                    break; //Break Statement
                }
                else  //Conditional Statements
                {
                    Console.WriteLine("Invalid Input. Try Again");
                }
            }

            Environment.Exit(0);

        }

        private static void SayHello()

        {
            Console.WriteLine("Hello There");
        }

        private static void PrintMultiples(int i, int j)

        {
            Console.WriteLine("{0} x {1} = {2}", i, j, i * j);
        }

        private static void PrintPower(double i, double PowerOf)

        {
            try
            {
                Console.WriteLine("{0} to the power of {1} is {2}", i, PowerOf, Math.Pow(i, PowerOf));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Press Enter to EXIT");
                Console.ReadLine();
                Environment.Exit(1);
            }
        }
    }
}


Here is the program output





No comments:

Post a Comment