Sunday, 21 July 2024

If block in c#

 ## C# `if` and `else` Conditional Statements


### Explanation


Conditional statements in C# allow you to execute certain blocks of code based on whether a condition is true or false. The most common conditional statements are `if` and `else`.


### Theory


#### `if` Statement


An `if` statement evaluates a boolean expression and executes a block of code if the expression is true. The syntax is:


```csharp

if (condition)

{

    // Code to be executed if condition is true

}

```


#### `else` Statement


An `else` statement follows an `if` statement and executes a block of code if the `if` condition is false. The syntax is:


```csharp

if (condition)

{

    // Code to be executed if condition is true

}

else

{

    // Code to be executed if condition is false

}

```


#### `else if` Statement


An `else if` statement allows you to check multiple conditions. If the initial `if` condition is false, the `else if` condition is evaluated. The syntax is:


```csharp

if (condition1)

{

    // Code to be executed if condition1 is true

}

else if (condition2)

{

    // Code to be executed if condition2 is true

}

else

{

    // Code to be executed if both condition1 and condition2 are false

}

```


### Diagram


Here is a flowchart representing the `if-else` structure:


```

            +------------------+

            |   Condition 1    |

            +------------------+

                     |

          +----------+----------+

          | true                 | false

          v                      v

   +---------------+     +------------------+

   |  Execute Code |     |   Condition 2    |

   +---------------+     +------------------+

                                |

                     +----------+----------+

                     | true                 | false

                     v                      v

              +---------------+     +----------------+

              |  Execute Code |     |  Execute Code  |

              +---------------+     +----------------+

```


### Examples


#### Example 1: Checking a Number


Here is an example of an `if-else` statement in C# that checks if a number is positive, negative, or zero:


```csharp

using System;


class Program

{

    static void Main()

    {

        int number = 10;


        if (number > 0)

        {

            Console.WriteLine("The number is positive.");

        }

        else if (number < 0)

        {

            Console.WriteLine("The number is negative.");

        }

        else

        {

            Console.WriteLine("The number is zero.");

        }

    }

}

```


**Output**:

```

The number is positive.

```


#### Example 2: Checking User Input


Here is an example of using `if-else` statements to validate user input:


```csharp

using System;


class Program

{

    static void Main()

    {

        Console.Write("Enter a number: ");

        string input = Console.ReadLine();

        int number;


        if (int.TryParse(input, out number))

        {

            if (number % 2 == 0)

            {

                Console.WriteLine("The number is even.");

            }

            else

            {

                Console.WriteLine("The number is odd.");

            }

        }

        else

        {

            Console.WriteLine("Invalid input.");

        }

    }

}

```


**Output** (Example 1):

```

Enter a number: 4

The number is even.

```


**Output** (Example 2):

```

Enter a number: hello

Invalid input.

```


#### Example 3: Grading System


Here is an example of using `if-else` statements to determine the grade based on a score:


```csharp

using System;


class Program

{

    static void Main()

    {

        int score = 85;


        if (score >= 90)

        {

            Console.WriteLine("Grade: A");

        }

        else if (score >= 80)

        {

            Console.WriteLine("Grade: B");

        }

        else if (score >= 70)

        {

            Console.WriteLine("Grade: C");

        }

        else if (score >= 60)

        {

            Console.WriteLine("Grade: D");

        }

        else

        {

            Console.WriteLine("Grade: F");

        }

    }

}

```


**Output**:

```

Grade: B

```


### Summary


Conditional statements using `if` and `else` in C# are essential for controlling the flow of a program based on specific conditions. They allow for decision-making processes within your code, enabling different outcomes based on varying conditions. This is fundamental for creating dynamic and responsive applications.

No comments:

Post a Comment

Thanks for the contribution, our team will check and reply back if response required.