C# Examples
C#

C# Examples

In this post you will learn few simple C# Examples. Specifically, you will get know basic language constructs like conditional statements and looping statements. The conditional elements allow us to decide the course of action on the basis of a condition being true or false. In C#, there are two conditional statements – if … else and switch … case.

C# Examples on Conditional Statements

if…else Statement Example

The if…else statement consists of if keyword followed by a condional expression. Basically, the conditional statement is a boolean expression as opposed to the conditional expression used in C language where is is an integer. Likewise, if condition is true, then statements followed by if keyword are executed. In other case, statements followed by else keyword are executed.

using System;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            Console.WriteLine("Enter a Number: ");
            a = Int32.Parse(Console.ReadLine());
            if (a % 2 == 0)
                Console.WriteLine(a + " is Even Number.");
            else
                Console.WriteLine(a + " is Odd Number.");
        }
    }
}

Output:

if…else example

Examples on switch…case Statement

We can use switch…case statement when there are many nested if..else statements should be used. Switch…case statement is also a conditional statement. However, it matches the expression with case labels and keep on doing so, until a matching label is found. Moreover, in C# the switch expression can be a string also as opposed to switch statement in C language, where it is either an integer or a char type.

using System;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            Console.WriteLine("Enter your choice [1, 2, 3]: ");
            a = Int32.Parse(Console.ReadLine());
            switch (a)
            {
                case 1: Console.WriteLine("One"); break;
                case 2:
                    Console.WriteLine("Two"); break;
                case 3:
                    Console.WriteLine("Three"); break;
                default:
                    Console.WriteLine("Wrong Choice"); break;
            }
        }
    }
}

Output:

C# Switch..Case Example

C# for loop Example

using System;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            for (int j = 1; j <= 5; j++)
            {
                sum += j;
            }
            Console.WriteLine("Sum = " + sum);
        }
    }
}

Output:

The for loop in C# language performs an iteration over all the statemens present in the loop until the statements are repeated for a specified number of times.

for loop example

C# while loop Example

using System;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            int i = 1;
            while (i <= 15)
            {
                sum += i;
                i++;
            }
            Console.WriteLine("Sum = " + sum);
        }
    }
}

Output:

The while loop is similar to the for loop, as it repeats the statements until the condition becomes false. However, we need to update the loop control variable, otherwise, the loop will execute indefinitely.

while loop example

C# do…while Example

The do…while loop also executes a set of statements repeatedly, until the loop condition becomes false. However, the condition is checked after the statements are executed.

using System;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            int i = 1;
            do
            {
                sum += i;
                i++;
            } while (i <= 25);
            Console.WriteLine("Sum = " + sum);
        }
    }
}

Output:

do..while loop example

Further Reading

How to create a C# Console Application

Leave a Reply

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