Showing posts with label loops in c#. Show all posts
Showing posts with label loops in c#. Show all posts

Switch statement in c#

Switch statement

The switch statement is like a set of if statements. It's a list of possibilities, with an action for each possibility, and an optional the default action, in case nothing else evaluates to true, so we can say that A switch statement allows a variable to be tested for equality against a list of values.

Syntax

switch ( CaseVariable )
{
    case constantExpression_1:
    statements;
    break;
    case constantExpression_2:
    statements;
    break;
    case constantExpression_3:
    statements;
    break;
    default:
    statements;
    break;
}



Example of a switch statement

public class switchStatementExample
{
  static void Main(string[] args)
  {
    char grade = 'B';

    switch (grade)
    {
      case 'A':
        Console.WriteLine("Excellent!");
        break;
      case 'B':
      case 'C':
        Console.WriteLine("Well done");
        break;
      case 'D':
        Console.WriteLine("You passed");
        break;
      case 'F':
        Console.WriteLine("Better try again");
        break;
      default:
        Console.WriteLine("You Failed!");
        break;
    }
  }
}

For Loop in c#

For Loop

The for loop contains three parts: initialization, conditional expression, and steps, which are separated by a semicolon,
1.  variable initialization: Declare & initialize a variable here which will be used in conditional expression and steps part.
2.      condition: The condition is a boolean expression that will return either true or false.
3.      steps: The steps define the incremental or decremental part

Syntax



for ( initialization; termination; increment/decrement)
{
    statements
}



For Loop example

class ForLoopExampleProgram
{
  static void Main(string[] args)
  {
    int i;
    i = 1;
    for (i = 0; i <= 50; i++)
    {
      Console.WriteLine("current Value of the i variable is :" + i.ToString());
      i = i + 5;
    }
    Console.Read();
  }

Interface and Abstract Class in C#

An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. the interface is not a class that contains only the signature of functionality. It’s a pattern with no implementation. Conceptually speaking, it’s just the definition of methods that contains only the declaration of members. It’s an empty shell that does not contain the implementation of its members. It’s like an abstract base class which only contains abstract members such as methods, events, indexers, properties, etc.


Syntax of Interface:-


interface
{
     //Interface member
}

Example of Interface in c#:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceExampleNamespace
{
    interface MyfirstInterface
    {
        void myFirstInterfaceMethod();
    }
    class InterfaceExampleClass : MyfirstInterface
    {
        public static void Main()
        {
            InterfaceExampleClass cls = new InterfaceExampleClass();
            cls.myFirstInterfaceMethod();
        }
        public void myFirstInterfaceMethod()
        {
            Console.WriteLine("welcome to dotnetTpoint , hi this is nitin kumar");
            Console.ReadLine();
        }
    }
}




Abstract Class


An abstract class is a special type of class that acts as a base of other classes and cannot be instantiated. The implementation logic of an abstract class is provided by its derived classes. To make a class abstract, the “abstract” modifier is used which means some missing implementation needs to be implemented in the class derived from it. It contains both abstract and non-abstract members.

Example of Abstract Class:-


using System;

namespace AbstractClassExampleNamespace
{
    abstract class AbstractClassExampleClass
    {
        public void methodwithDefination()
        {
            Console.WriteLine("methodwithDefination Method: This is the method of Abstract Class with Defination");
        }
        public abstract void methodWithoutDefination();
    }

    class TestingClass : AbstractClassExampleClass
    {
        public override void methodWithoutDefination()
        {
            Console.WriteLine("methodWithoutDefination method: this is second method of interface without defination inside interface ");
        }

        public void TestingClassOwnMethod()
        {
            Console.WriteLine("TestingClassOwnMethod Method: This is another method of testing class");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TestingClass objTestingCls = new TestingClass();
            objTestingCls.methodwithDefination();
            objTestingCls.methodWithoutDefination();
            objTestingCls.TestingClassOwnMethod();
            Console.ReadKey();
        }
    }
}

Difference between interface and Abstract Class in c#:-


1.      An abstract class cannot support multiple inheritance, but an interface can support   multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.

2.      An interface is an empty shell, just only the signatures of the methods. The methods do not contain anything. The interface can't do anything. It's just a pattern. An Abstract class is a class which will contains both definition and implementation in it.

3.      Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods.

4.      Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but all methods of an interface must be defined as public.



Control Statements and types of Loops in c#

Control statements give you additional means to control the processing within the applications you develop. These conditions are specified by a set of decision-making statements having Boolean expressions that are evaluated to a Boolean value true or false.

Comparison Operators

Operator
Action
==
Equal to
!=
Not equal to
> 
Greater than
>=
Greater than or equal to
< 
Less than

Types of Control statements in C#

1.      IF statement
2.      Switch statement
3.      For Loop
4.      While loop
5.      Do ... While Loop



1      IF statement
C# if statement will execute a block of code if the given condition is true.


Syntax:

if(statement is TRUE)
EXECUTE this line of code


Syntax Example 1 

if(expression)
{
    statements;
}
else
{
    statements;
}


Example 1

class Program
{
  static void Main(string[] args)
  {
    int age = 0;
    Console.WriteLine(“please enter your age to find out your eligibility for voting”) ;
    age = Convert.ToInt32(Console.ReadLine());

    if (age < 18)
    {
      Console.WriteLine(“You are not eligible for voting”) ;
    }
    else
    {
      Console.WriteLine(“You are eligible for voting”) ;
    }
    Console.Read();
  }
}



  2 - Switch statement

The switch statement is like a set of if statements. It's a list of possibilities, with an action for each possibility, and an optional the default action, in case nothing else evaluates to true, so we can say that A switch statement allows a variable to be tested for equality against a list of values.

Syntax: -


switch ( CaseVariable )
{
    case constantExpression_1:
    statements;
    break;
    case constantExpression_2:
    statements;
    break;
    case constantExpression_3:
    statements;
    break;
    default:
    statements;
    break;
}


 Example of switch statement

public class switchStatementExample
{
  static void Main(string[] args)
  {
    char grade = 'B';

    switch (grade)
    {
      case 'A':
        Console.WriteLine("Excellent!");
        break;
      case 'B':
      case 'C':
        Console.WriteLine("Well done");
        break;
      case 'D':
        Console.WriteLine("You passed");
        break;
      case 'F':
        Console.WriteLine("Better try again");
        break;
      default:
        Console.WriteLine("You Failed!");
        break;
    }
  }
}


 While loop

The while loop includes a Boolean expression as a condition that will return true or false. It executes the code block, as long as the specified conditional expression returns true

Syntax:-

while ( expression )
{
    statements;
}

Example of while loop


class whileLoopExampleProgram
{
  static void Main(string[] args)
  {
    int i;
    i = 1;
    while (i < 50)
    {
      Console.WriteLine(“current Value of the i variable is :”+i);
      i = i + 5;
    }
    Console.Read();
  }
}





For Loop

The for loop contains three parts: initialization, conditional expression and steps, which are separated by a semicolon,
1-      variable initialization: Declare & initialize a variable here which will be used in conditional expression and steps part.
2-      condition: The condition is a boolean expression that will return either true or false.
3-      steps: The steps defines the incremental or decremental part





Syntax

for ( initialization; termination; increment/decrement)
{
    statements
}

For Loop example



class ForLoopExampleProgram
{
  static void Main(string[] args)
  {
    int i;
    i = 1;
    for (i = 0; i <= 50; i++)
    {
      Console.WriteLine("current Value of the i variable is :" + i.ToString());
      i = i + 5;
    }
    Console.Read();
  }



Do-while loop

The do-while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again.




Syntax of do-While loop


do
{
    statements;
} while ( expression );


Example of do-while loop:-

namespace ConsoleApplication1
{
  class do_while_LoopExampleProgram
  {
    static void Main(string[] args)
    {
      int i = 0;
      i = 60;
      do
      {
        Console.WriteLine("value of i is-  " + i.ToString());
        i = i + 60;
      } while (i < 20000);
      Console.Read();
    }
  }
}