Decision Making and Looping in c#

By
Sometimes ,We have a number of situations where we may have to change the order of execution of statements based on certain conditions are met.This involves a kind of decision making to see whether a particular condition has occurred or not. and then to direct the computer   to execute certain statement accordingly.Every computer language must have features that instruct a computer to perform repetitive task.The process of repeatedly executing a block of statements is known as looping.
There are some decision making statement statements in c#.
  1. if statement 
  2. Switch statement
  3. Conditional operator statement
There are some statement that are used to perform looping operation in c#.
  1. The While statement
  2. The do statement
  3. The for statement
  4. The foreach statement
Decision Making statements in C#:-
     1. ) if statement:- If statement can be implement in different form on the basis of complexity of the conditions.
  • Simple if statement
  • Nested if-else statement
  • if-else statement
  • else if statement
Syntax of if statement:-
if(condition expression)
{
statement;
-----------------
}

Syntax of Nested if-else statement:-
if(condition expression 1)
{
if(condition expression 2)
{
.....statement;
.........................
}
else
{
statement;
.................
}
}
else
{
statement;
...............
}

Syntax of if-else statement:-
if(condition expression 2)
{
statement;
.........................
}
else
{
statement;
}

Syntax of else if statement:-
if(condition expression 1)
{
statement;
................
}
else if(condition expression 2)
{
statement;
..............
}
else if(condition expression 3)
{
statement;
.....................
}
else
{
statement;
.................
}

Program on if statement:-

using System;
namespace decision_making
{
    class Program
    {
        static void Main(string[] args)
        {
            int num,i;
            string value;
            for (i = 1; i <= 6; i++)
            {
                Console.WriteLine("Enter the marks of the student" +i);
                num = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the sex(boy or girl )of student" +i);
                value = Console.ReadLine();
                if (num >= 80)
                {
                    if (value == "boy")
                    {
                        Console.WriteLine("You have passed with honour");
                        Console.WriteLine("You will get discount of 50% in fees");
                    }
                    else
                    {
                        Console.WriteLine("You have passed with honour");
                        Console.WriteLine("You will get discount of 70% in fees");
                    }
                }
                else if (num >= 60 && num < 80 && value == "girl")
                {
                    Console.WriteLine("You have passed First division");
                    Console.WriteLine("You will get discount of 30% in fees");
                }
                else if (num >= 60 && num < 80 && value == "boy")
                {
                    Console.WriteLine("You have passed First division");
                }
                else if (num >= 45 && num < 60)
                {
                    Console.WriteLine("You have passed second division");
                }
                else if (num >= 33 && num < 45)
                {
                    Console.WriteLine("You have passed Third division");
                }
                else
                {
                    Console.WriteLine("You have failed try again");
                }
                Console.ReadLine();
            }
        }
    }
}

Output:-


     2. ) The Switch statement:-
We can use switch statement instead of the if-else statement.But generally we use switch statement whenever we find that if -else  statement is more complex.
Syntax:
switch(expression)
{
case 1:
statement;
case 2:
statement;
case 3:
statement;
.
.
default:
statement;
}

There are some order of execution of switch statement.
  • The expression is evaluated first.
  • The expression value is compare with case value,if it become true then that case statement will be executed,if it become false then default value will be executed.
  • The break statement at the end of each block statement,cause an exit from switch statement.
  • The default is a optional case,when the value of expression does not match any of the case value then it will be executed.
Program on switch statement:-

using System;
namespace switch_statement
{
    class Program
    {
        static void Main(string[] args)
        {
            int marks;
       Console.WriteLine("press: 1 if marks is greater than 80");
       Console.WriteLine("press: 2 if marks is greater than 60 and less than 80 ");
       Console.WriteLine("press: 3 if marks is greater than 45 and less than 60");
       Console.WriteLine("press: 4 if marks is greater than 33 and less than 45");
       Console.WriteLine("press: 5 if marks is less than 33");
            marks = int.Parse(Console.ReadLine());
            switch (marks)
            {
                case 1:
               Console.WriteLine("you have passed  first division with honour");
                    break;
                case 2:
                    Console.WriteLine("you have passedfirst division");
                    break;
                case 3:
                    Console.WriteLine("you have passed second division");
                    break;
                case 4:
                    Console.WriteLine("you have passed Third division");
                    break;
                case 5:
                    Console.WriteLine("you have failed try again");
                    break;
                default:
                    Console.WriteLine("Enter correct value");
                    break;
            }
            Console.WriteLine("Thank you");
            Console.ReadLine();
        }
    }
}
output:


     3. ) The ? : operator:-
The character pair ? :  is a ternary operator in C#. This operator is used to make conditional Expression in C#.
Ex. 1
exp1 ? exp2 : exp3
Where exp1 ,exp2 and exp3 are expressions.
The operator ? : works as follow:
  • exp1 is evaluated first, if it is true, then exp2 is evaluated and becomes the value of the conditional expression.
  • If exp1 is false then exp3 is evaluated and becomes the value of the expression.
Ex. 2
Let  x=20 and y=30
           and
 Expression z =x > y ? x : y ;
Output: Z = 30
Description : x>y is false so expression third ( y) is evaluated and becomes the value of this conditional expression.

  The Looping operations in C#:-
      1. ) The while statement:
while statement is the simplest looping structures in C#.
Syntax:-
initialization;
while(condition)
{
body of the loop
}
Description:-while is an entry controlled loop statement.The condition of while loop is evaluated first.If the condition is true,then the body of loop is executed.After execution of body,the condition is again evaluated and if it is true,the body is executed once again.The process of execution of body continues until the condition finally becomes false and the control is transferred out of the loop. 
Program:-

using System;
namespace while_statement
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i <=5)
            {
                Console.WriteLine(+i);
                Console.WriteLine("\n");
                i = i + 1;
            }
            Console.ReadLine();
        }
    }
}


Output:-

     2. ) The do statement:- It is also same as while statement but there are some difference on the behalf of working.
Syntax:
initialization;
do
{
body of the loop;
}while(condition);

Description:-
In while statement condition is checked first before executing the body of the loop.But in do statement body is executed first and condition is checked after the body.If condition is true then the body is executed once again.The process of execution of the body continues until the condition finally becomes false and control transferred out of the loop.
Program:-

using System;
namespace do_while
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            do
            {
                Console.WriteLine(+i);
                Console.WriteLine("\n");
                i = i + 1;
            } while (i <= 5);
            Console.ReadLine();
        }
    }
}

Output:- Same  output as while statement output.

     3. ) The for statement:-
The for loop is  a entry controlled loop that provides a more concise loop control structure.
Syntax:
for(initialization; condition;increment)
{
body of the loop;
}
Description:-The for statement execution as follows.
  • Initialization of the control variable.
  • Test the condition using control variable.
  • If the condition is true,then increment variable increment the control value.
    Program:-
    
    using System;
    namespace for_statement
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] a = new int[10];
                Console.WriteLine("Enter the array element values");
                for (int i = 0; i <10; i++)
                {
                    a[i] = int.Parse(Console.ReadLine());
                }
                Console.WriteLine("Array element is:");
                for (int i = 0; i <10; i++)
                {
                    Console.WriteLine(+a[i]);
                }
                Console.ReadLine();
            }
        }
    }
    
     4. ) The foreach statement:-
The for and foreach statement is similar to each other but their implementation is different.It enables us to iterate the elements in arrays and collection classes such as List and Hash table.
Syntax:
foreach(type variable 2 in variable 1)
{
body of the loop.
}
Description:-
In this statement no need to increment and Decrement the control values.Here each value of variable 1 is copied one by one to the variable 2,but both variable type should be same .
program:

using System;
namespace forech_statement
{
    class Program
    {
       public static void Main(string[] args)
        {
            int[] a = new int[5]{1,2,3,45,4};
            Console.WriteLine("Array element is:");
            foreach (int b in a)
            {
                Console.WriteLine(b);
            }
            Console.ReadLine();
        }
    }
}

output:-


There are some keywords that are used to perform task in the program.
  1. Break;
  2. continue;
  3. goto;
     1. ) Break :-> It is the cause to terminates the loop.
               Syntax:

Program:

using System;
namespace @break
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            while(i<=10)
            {
                Console.WriteLine(+i);
                i = i + 1;
                break;
            }
            Console.WriteLine("I am break statement ");
            Console.ReadLine();
        }
    }
}
Output:

     2. ) continue: It is the cause to continue the loop with the next iteration after skipping any statement in between.The continue statement enable us to restart the current loop.
Syntax:
program:

using System;
namespace @continue
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            while (i <= 10)
            {
                Console.WriteLine(+i);
                i = i + 1;
                continue;
            }
            Console.WriteLine("I am continue statement ");
            Console.ReadLine();
        }
    }
}
output:
 
     3. ) goto :- This statement uses the concept of level.A label is an valid c# variable name ending with a colon.
Ex.

using System;
namespace goto_statement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" first loop value is:");
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine(+i);
                if (i > 4)
                {
                    Console.WriteLine("value greater than four is" + i);
                    break;
                }
                for (int j =0; j <=5; j++)
                {
                    if (j <4)
                    {
                        goto loop1;
                    loop1: break;
                    }
                    else
                    goto end;
                }
            }
             end:
            Console.WriteLine("i am end");
            Console.ReadLine();
        }
    }
}
output:

For More:-

  1. Type conversion
  2. Overview of c#
  3. Basic elements for compiling the c# codes
  4. setup
  5. Arrays

Click below for download whole application
                       Download

0 comments:

Post a Comment

Powered by Blogger.