Control Statements - If-else - DotNet and DataBase

Wednesday, 4 July 2018

Control Statements - If-else





If statement is used to check the condition validity.

There are various types of if else statements available.

1. If statement
2. If-else statement
3. nested if statement
3. if-else -if statement



if statement
-------------

"if" statement checks the condition validity, if the condition satisfy, the functionality with in the if  block will be executed.

Syntax:
---------
if(condition)
{
statement;
}


Example:
-----------
namespace FirstApplication
{

    class Program
    {
        static void Main(string[] args)
        {
            string gender = "m";
   
           if (gender == "m")
            {
                Console.WriteLine("Welcome Mr. xyz");
                Console.Read();
            }
        }
   }
}


if-else statement:
--------------------
if else statement check the condition validity, if the condition satisfy the code within the if block will be executed otherwise the code in the else block will be executed.


Syntax:
---------
if(condition)
{
statement;
}
else
{
statement;
}


Example:
-----------
namespace FirstApplication
{

    class Program
    {
        static void Main(string[] args)
        {
           string gender = "m";
            if (gender == "m")
            {
                Console.WriteLine("Welcome Mr. xyz");
            }
            else
            {
                Console.WriteLine("Welcome Ms. xyz");
            }
            Console.Read();

        }
   }
}


if-else-if
------------

If else just like a Boolean result (either true or false). If the condition satisfy, the code in the if block will be executed otherwise else block will be executed.

In case we want to check multiple conditions, we have to use if-else if   condition.

Syntax
---------

if(condition1)
{
statement;
}
else if(condition2)
{
statement;
}
else
{
}

For example we are writing functionality based on the age groups of some people.

The expected result should be as below

if age group in between

1-40 -Young
41-60- Senior citigen
61-100- old

we have to write the code as below,


           string agegroup = "10-40";
         
            if (agegroup == "10-40")
            {
                Console.WriteLine("The age group is Young age");
            }
            else if (agegroup == "41-60")
            {
                Console.WriteLine("The age group is senior citigen");
            }
            else if (agegroup == "61-100")
            {
                Console.WriteLine("The age group is old age");
            }
            else
            {
                Console.WriteLine("The age group is very old age");
            }

            Console.Read();


Here, based on the supplied age group to agegroup , the related block will be executed and provides the result.

In case the supplied agegroup doesn't belongs to any of the conditions mentioned, the final else block will be executed.




Practice if Statement if-else Statement nested if-else Statement

1 comment:

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.