Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Conditional Statement in C-Sharp:-

It is used to solve a condition-based program, It provides a separate section for true condition and false condition.

Ternary operator can contain only a single statement on the true section and false section. that's why we mostly prefer conditional statement.


Condition Statements have two different statements first is IF and another one is Else

Syntax of IF Statements:-

if(condition)  
{
          Statements;
}

It will execute when the condition will be true.

Syntax of  ELSE Statements:-

else
{

}

It will not execute separately, It is a dependent statement under if, when IF condition will be false then Else statement will be executed.


Combination of else and IF:-

else if(condition)
{


}

It is a combined statement of Else and IF  that will work similarly to else but we can provide the condition, it is also a dependent statement under IF statement.


Type of Conditional Statement:-

1)  Simple If:-

   It will execute when the condition will be true.

    if(condition)
   {

   }

2) If--else:-
   It will execute when the condition will be true and false.

    if(condition)
   {

   }
  else
   {

   }

3) Ladder If-Else or ElseIf:-    

It will execute with the combination of IF statement, Else IF statement,... and Else statement. Ladder means Step by Step when the first condition will be false then the second will be cheeked.

It is also called Ladder if-else because it will work step by step.


Syntax of Ladder if else:-

if(condition)
{
      Statements;

}
else if(condition)
{

     Statements;
}
else if(condition)
{

    Statements;
}
else
{
   Statement;
}


Example of Ladder if-else to calculate program of the greatest number?

class Ladder
    {
        static void Main()
        {
            int a = 50, b = 200, c = 300;
            if (a > b && a > c)
            {
                Console.WriteLine("a is greatest");
            }
            else if (b > c)
            {
                Console.WriteLine("b is greatest");
            }
            else
            {
                Console.WriteLine("c is greatest");
            }

            Console.ReadKey();

        }

4) Nested If--Else:-   If we write more than one if-else statement using nested sequence then it is called nested if-else.


Syntax of Nested If Else:-


if(condition)
{

    if(condition)
    {


     }

   else
    {

   }

}
else
{

if(condition)
    {


     }

   else
    {

   }



}


Example of Nested If--else to calculate the greatest number?

class NestedExample
    {
        static void Main()
        {
            int a = 50, b = 200, c = 300;
            if (a > b)
            {
                if(a>c)
                Console.WriteLine("a is greatest");
                else
                Console.WriteLine("c is greatest");
            }
         
            else
            {
                if (b > c)
                    Console.WriteLine("b is greatest");
                else
                    Console.WriteLine("c is greatest");
            }

            Console.ReadKey();

        }
    }

5) Multiple If:-  We will write more than one IF  condition that will work when multiple conditions will be true.


Syntax of Multiple IF:-

 if(condition)
   {

   }

 if(condition)
   {

   }


ASSIGNMENT OF IF--ELSE:-


Q Check Userid and Password with all combinations?


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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            String uid, pass;
            Console.WriteLine("Enter userid");
            uid = Console.ReadLine();
            Console.WriteLine("Enter password");
            pass = Console.ReadLine();
            if (uid == "abc" && pass == "1234")
            {
                Console.WriteLine("Valid Userid and Password");
            }
            else if (uid != "abc" && pass != "1234")
            {
                Console.WriteLine("Invalid Userid and password");
            }
            else if (pass != "1234")
            {
                Console.WriteLine("Invalid Password");
            }
            else
            {
                Console.WriteLine("Invalid Userid ");
            }
            Console.ReadKey();
        }
    }
}



Q WAP to provide grace marks to the student if the mark will be less then <33  otherwise the same marks will be displayed, grace marks will be up-to five. if pass with grace then displays grace + grace mark otherwise mark.

Q WAP to increase the salary of an employee from 500, if entered salary will be less then 20000,1000 if entered salary will be > 20000 but <30000 if the salary will be above 30000 then increase 2000.


Q WAP  to declare income tax slab according to entered income?

Q WAP to check that temperature is normal, cold, or heat according to entered temperature, the temperature should be in Celsius but the comparison will be in Fahrenheit?

The solution of this program:-

class TempratureConvertor
    {
        static void Main()
        {
            float f, c;
            Console.WriteLine("Enter temprature in celsius");
            c = float.Parse(Console.ReadLine());
            f = (9 * c + 160) / 5;
            if (f >= 0 && f <= 62.6)
            {
                Console.WriteLine("COLD and Tempratute is "+c+ "c");
            }
            else if (f <= 77)
            {
                Console.WriteLine("Normal");
            }
            else
            {
                Console.WriteLine("Heat");
            }

            Console.ReadKey();
        }
    }

Q WAP to check divisibility of number that number is divisible by 3,5 and 9 with all combinations?


Q WAP to create a mark sheet of Students using five different subjects with the following condition?

1)  All Subject Marks Should be 0 to 100.

2) If only Subject Mark is <33 Then Student will Suppl

3) If Minimum Two Subject Marks is <33 Then Student Will Fail

4) IF all Subject Marks is > 33 then percentage and division should be calculated.

5)  IF only subject Mark is >28 and <33 then 5 grace marks will be applied and the student will pass by grace.

6)  Display Grace Subject Name, Distinction Subject name, Supp Subject name, and Failed Subject name.


The solution of This Program:-

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

namespace ConsoleApplication2
{
    class MarksheetProgram
    {
        static void Main()
        {
            int m1, m2, m3, m4, m5,grace=0;
            String sub1, sub2, sub3, sub4, sub5,sub="",dist="";
            int count = 0;
            Console.WriteLine("Enter First Subject Name and Mark");
            sub1 = Console.ReadLine();
            m1 = int.Parse(Console.ReadLine());
         
            Console.WriteLine("Enter Second Subject Name and Mark");
            sub2 = Console.ReadLine();
            m2 = int.Parse(Console.ReadLine());
         
            Console.WriteLine("Enter Third Subject Name and Mark");
            sub3 = Console.ReadLine();
            m3 = int.Parse(Console.ReadLine());
         
            Console.WriteLine("Enter Fourth Subject Name and Mark");
            sub4 = Console.ReadLine();
            m4 = int.Parse(Console.ReadLine());
         
            Console.WriteLine("Enter Fifth Subject Name and Mark");
            sub5 = Console.ReadLine();
            m5 = int.Parse(Console.ReadLine());
         
            if ((m1 >= 0 && m1 <= 100) && (m2 >= 0 && m2 <= 100) && (m3 >= 0 && m3 <= 100) && (m4 >= 0 && m4 <= 100) && (m5 >= 0 && m5 <= 100))
            {
                if (m1 >=75)
                {
                    dist+= sub1 + " ";
                }
                if (m2 >= 75)
                {
                    dist += sub2 + " ";
                }
                if (m4 >= 75)
                {
                    dist += sub4 + " ";
                }
                if (m5 >= 75)
                {
                    dist += sub5 + " ";
                }
                if (m3 >= 75)
                {
                    dist += sub3 + " ";
                }
                if (m1 < 33)
                {
                    grace = m1;
                    sub += sub1 + " ";
                    count++;
                }
                if (m2 < 33)
                {
                    grace = m2;
                    sub += sub2 + " ";
                    count++;
                }
                if (m3 < 33)
                {
                    grace = m3;
                    sub += sub3 + " ";
                    count++;
                }
                if (m4 < 33)
                {
                    grace = m4;
                    count++;
                    sub += sub4 + " ";
                }
                if (m5 < 33)
                {
                    grace = m5;
                    count++;
                    sub += sub5 + " ";
                }
                if (count == 0 || (count==1 && grace>=28))
                {
                    float per = (m1 + m2 + m3 + m4 + m5) / 5;
                    if(per>=33 && per<45)
                    Console.WriteLine("Congrats you are pass with third division with "+per +"%");
                    else if(per<60)
                        Console.WriteLine("Congrats you are pass with second division with " + per + "%");
                    else
                        Console.WriteLine("Congrats you are pass with first division with " + per + "%");
                    if (grace >= 28)
                        Console.WriteLine("You are pass by grace and grace marks is " + (33 - grace));
                    if (dist != "")
                        Console.WriteLine("Distinction Subject name is " + dist);

                }
                else if (count == 1)
                {
                    Console.WriteLine("Try again you are suppl in "+sub);
                }
                else
                {
                    Console.WriteLine("You are fail in "+sub);
                }

            }
            else
            {
                Console.WriteLine("All Subject Marks Should be 0 to 100");
            }
            Console.ReadKey();

        }
    }
}


Q WAP to increase the salary of an employee from 500, if entered salary will be less then 20000,1000 if entered salary will be > 20000 but <30000 if the salary will be above 30000 then increase 2000.
not use any logical operator


Q WAP to find the middle in three different where the number should not be equal?

a=1
b=7
c=2




                                     

4 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. Ans1:- Passed by Grace+

    using System;

    public class Program
    {
    public static void Main()
    {
    int a ;
    Console.WriteLine("Enter Marks: ");
    a= int.Parse(Console.ReadLine());
    if (a >0 && a<=100)
    {
    if (a==28)
    {
    Console.WriteLine("Grace+ = {0}",(a + 5));
    }
    else if (a==29)
    {
    Console.WriteLine("Grace+ = {0}",(a + 4));
    }
    else if (a==30)
    {
    Console.WriteLine("Grace+ = {0}",(a + 3));
    }
    else if (a==31)
    {
    Console.WriteLine("Grace+ = {0}",(a + 2));
    }
    else if (a==32)
    {
    Console.WriteLine("Grace+ = {0}",(a + 1));
    }
    else
    {
    Console.WriteLine("Mark is {0}",a);
    }
    }
    }
    }

    ReplyDelete
  2. Ans2 :- Salary Increment(500,1000,2000) Program.

    public static void Main()
    {
    int a;
    Console.WriteLine("Enter Salary : ");
    a = int.Parse(Console.ReadLine());
    if(a>=0 && a<=19999)
    {
    Console.WriteLine("Salary is :{0} \nSalary with Increment :{1} ",a,a+500);
    }
    else if(a>=20000 && a<=29999)
    {
    Console.WriteLine("Salary is :{0} \nSalary with Increment :{1} ",a,a+1000);
    }
    else
    {
    Console.WriteLine("Your Salary is :{0} \nSalary With Increment :{1}",a,a+2000);
    }
    }

    ReplyDelete
  3. using System;

    public class Program
    {
    public static void Main()
    {
    float a,b,c,d,e,f,g,h;
    Console.Write("Enter Annual Salary Lakh: ");
    a= float.Parse(Console.ReadLine());
    b=((a*5)/100);
    c=((a*10)/100);
    d=((a*15)/100);
    e=((a*20)/100);
    f=((a*25)/100);
    g=((a*30)/100);
    if (a>=0 && a<=250000)
    {
    Console.WriteLine("Your are not under Income-Tax");
    }
    else if(a>=250001.00 && a<=500000.00)
    Console.WriteLine("You will Pay 5% Income-Tax : {0}",b+" " +"Thousand");
    else if(a>=500001.00 && a<=750000.00)
    Console.WriteLine("You will Pay 10% Income-Tax : {0}",c+" " +"Thousand");
    else if (a>=750001.00 && a<=1000000.00)
    Console.WriteLine("You will Pay 15% Income-Tax : {0}",d+" " +"Lakh");
    else if(a>=1000001 && a<=1250000)
    Console.WriteLine("You will Pay 20% Income-Tax : {0}",e+" " +"Lakh");
    else if(a>=1250001 && a<=1500000)
    Console.WriteLine("You will Pay 25% Income-Tax : {0}",f+" " +"Lakh");
    else
    Console.WriteLine("You will Pay 30% Income-Tax : {0}",g+" " +"Lakh");
    }

    }

    ReplyDelete
  4. int a;
    Console.WriteLine("Enter Number : ");
    a =int.Parse(Console.ReadLine());
    if (a%3==0 && a%5==0 && a%9==0)
    {
    Console.WriteLine("Divisible Number :{0}",a);
    }else
    Console.WriteLine("Not Divisible Number :{0}",a);

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post