C# Course – Worldwideasy – 04

C# lesson 4. I will focus on Getting Control Statements (IF statement, Switch, loops). Finally, Create a simple Power Calculator. It can provide extensive knowledge.

A control statement is a statement that determines whether other statements are active. If a statement decides whether another statement should be executed, or one of the two statements to be executed. A loop determines the number of times another expression should be executed.

Control Statement mean

Basically have 3 control statements in php

  • IF
  • Switch
  • Loops

IF Statement in C#

If a statement is used to check the condition, it can be a condition or more. The special feature of it is that you can check any gap. The following example will make it clear to you.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int mark = Convert.ToInt32(85);
            if (mark > 50)
            {
                Console.WriteLine("Pass");
            }
            else
            {
                Console.WriteLine("Fail");
            }
            Console.ReadLine();
        }
    }
}

This code output is given as Pass. Here, it checks if the value of the mark is greater than 50. It is represented as true pass and False as Fail.

else if

This is used to check several conditions at once. look following example will make it clear to you.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int mark = Convert.ToInt32(67);
            if (mark > 75)
            {
                Console.WriteLine("A");
            }
            else if (mark > 65){
                Console.WriteLine("B");
            }
            else if (mark > 55)
            {
                Console.WriteLine("C");
            }
            else if (mark > 40)
            {
                Console.WriteLine("S");
            }
            else
            {
                Console.WriteLine("F");
            }
            Console.ReadLine();
        }
    }
}

This code output is B. You can see this code many times check conditions.

Switch

The switch expression is used to check several non-gap conditions. This works very fast and gets the idea below.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int day = 2;
            switch (day)
            {
                case 1:
                    Console.WriteLine("Sunday");
                    break;

                case 2:
                    Console.WriteLine("Monday");
                    break;

                default:
                    Console.WriteLine("Wrong");
                    break;

            }

            Console.ReadLine();
        }
    }
}

This Code output is Monday. A Break statement is used for stop the function.

Loops

Loops have main 3 loops in c#

  • For loop
  • While loop
  • Do While loop

Now I show all loops using Code. See Below all code output is the same. output is : 10,9,8,7,6,5,4,3,2,1.

For loop

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        for (int i=10; i >= 1; i--)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
       
        }
    }
}

While Loop

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 10;
            while (i >= 1)
            {
                Console.WriteLine(i);
                i--;
            }
            Console.ReadLine();
       
        }
    }
}

Do While Loop

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 10;
            do
            {
                Console.WriteLine(i);
                i--;
            } while (i >= 1);
            Console.ReadLine();
       
        }
    }
}

Now you have very good knowledge about C #. Practice this over and over again. Let us now see how to make a power calculator from what we have learned so far.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("****POWER CALCULATOR****");
            Console.WriteLine("Enter Base Number : ");
            int x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Power Number : ");
            int y = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Power is: "+getpow(x,y));
            Console.ReadLine();
       
        }
        static int getpow(int basenum, int pow)
        {
            int result = 1;
for(int i = 0; i < pow; i++)
            {
                result = result * basenum;
            }
            return result;
        }
    }
}

Output is Below

Thank you for all!

More C# ClickHere!

See you Next C# Lesson Soon!


Leave a Comment

Your email address will not be published. Required fields are marked *

Translate »