Descision making: if else statements

Conditions are something that is inescapable.  Whenever you create a program, you are very likely to make decisions in it or let your users make some decisions.

Example
If you are to create a program in which you ask your user whether he/she has a pet. Now if user says that he/she has a pet then you can ask him/her what type of animal it is? But if user replay is in “no”, then your program should not ask him about type of pet because he/she doesn’t own one.

Flow Diagram

How to handle it in Programming
One way to handle the conditions when making decisions in your program is through if-else statement.

Basic Syntax

if (condition) {
	//here goes your code if condition is true
} else {
	//here goes your code if condition is false
} 

Coding Example

Below are some examples in which you discuss different cases to use if-else statements.

Example: IF statement
namespace Conditions
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 50;
            if (num < 100)
            {
                Console.WriteLine("Value of num is less than 100.");
            }
            Console.ReadLine();
        }
    }
}
Here is a case where only if condition is used and else (which is optional) is skipped.
When the code executes you can see that in output the line 10 is executed because the if condition is true.

Output:

If you give value more than 100 you can see that line 10 won’t execute because if condition is false.

Example: If-else statement
Below is an example in which else case is used.
namespace Conditions
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 120;
            if (num < 100)
            {
                Console.WriteLine("Value of num is less than 100.");
            }
            else
            {
                Console.WriteLine("Value of num is not less than 100.");
            }
            Console.ReadLine();
        }
    }
}
Output:

Here the else case is executed because if condition proved to be false and there exist a else case which was executed.

Example: IF-else-if statement

You can use if – else if case when you need more conditions.
namespace Conditions
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 100;
            if (num < 100)
            {
                Console.WriteLine("Value of num is less than 100.");
            }
            else if(num == 100)
            {
                Console.WriteLine("Value of num is 100.");
            }
            else
            {
                Console.WriteLine("Value of num in greater than 100.");
            }
            Console.ReadLine();
        }
    }
}
Output:

Here second condition was true so that code was executed.

Example: Nested IF

You can use if statement inside if. This type of condition is known as Nested if statement.
namespace Conditions
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 100;
            int num2 = 50;
            if (num < 100)
            {
                Console.WriteLine("Value of num is less than 100.");
            }
            else if(num == 100)
            {
                Console.WriteLine("Value of num is 100.");
                if (num2 == 50)
                {
                    Console.WriteLine("Value of num2 is 50");
                }
            }
            else
            {
                Console.WriteLine("Value of num in greater than 100.");
            }
            Console.ReadLine();
        }
    }
}
Output:

Note: If or else statements are enclosed inside braces. They indicate the statements that belong to that if or else portion. It is good practice to always use them. But if you have only one line of code you can skip the braces.

<< Descision making: if else statements
Tags: C-sharp
comments powered by Disqus