All Type Coding

Search Here

IF Statement in c#

IF Statement 
The conditional statement if.. else in C# is using for check the conditions 
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
The conditional statement examining the data using comparison operators as well as logical operators. The else statement is optional , so we can use the statement in two ways ;
1- if (condition)
{
  statement;
}
   
2- if (condition)
{
 statement;
}  
else
{
 statement;
}

If the condition is true then the control goes to the body of if block , that is the program will execute the code inside if block.If the condition is false then the control goes to next level , if you provide else block the program will execute the code block of else statement, otherwise the control goes to next line of code.
If you want to check more than one conditions at the same time , you can use else if statement .

  if (condition)
{
  statement;
}  
else if(condition)
{
 statement;
}
else if(condition)
{
 statement;
}

Here we will see how to use if statement let's say we'll compare three number to each other and got highest  number out of three


using System;

class abc
{
    int a, b, c, d, e, f, g;
    public void grt()
    {
        Console.WriteLine("Enter first no that is a");
        a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter second no that is b");
        b = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter third no that is c");
        c = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter third no that is d");
        d = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter third no that is e");
        e = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter third no that is f");
        f = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter third no that is g");
        g = Convert.ToInt32(Console.ReadLine());

        if ((a > b) && (a > c) && (a > d) && (a > e) && (a > f) && (a > g))
        {
            Console.WriteLine("a is greater");
        }
        else if ((b > a) && (b > c) && (b > d) && (b > e) && (b > f) && (b > g))
        {
            Console.WriteLine("b is greater");
        }
        else if ((c > a) && (c > b) && (c > d) && (c > e) && (c > f) && (c > g))
        {
            Console.WriteLine("c is greater");
        }
        else if ((d > a) && (d > b) && (d > c) && (d > e) && (d > f) && (d > g))
        {
            Console.WriteLine("d is greater");
        }
        else if ((e > a) && (e > b) && (e > c) && (e > d) && (d > f) && (d > g))
        {
            Console.WriteLine("e is greater");
        }
        else if ((f > a) && (f > b) && (f > c) && (f > d) && (f > e) && (f > g))
        {
            Console.WriteLine("f is greater");
        }
        else
        {
            Console.WriteLine("g is greater");
        }
        Console.ReadKey();
    }
}
class grt2
{
    public static void Main(string[] args)
    {
        abc a = new abc();
        a.grt();
    }
}
Output



No comments :

Post a Comment