Mark As Completed Discussion

Control Flow and Decision Making

In .NET, control flow statements allow us to control the flow of execution in a program based on conditions. These statements can be used to make decisions and execute different blocks of code based on the result of a condition.

One of the most common control flow statements in .NET is the if statement. The if statement allows us to specify a condition, and if the condition is true, the block of code inside the if statement is executed.

Here's an example of using the if statement to check if a number is positive, negative, or zero:

TEXT/X-CSHARP
1int num = 10;
2
3if (num > 0)
4{
5    Console.WriteLine("The number is positive.");
6}
7else if (num == 0)
8{
9    Console.WriteLine("The number is zero.");
10}
11else
12{
13    Console.WriteLine("The number is negative.");
14}

In this example, we declare a variable num and assign it a value of 10. We use an if statement to check if the value of num is greater than 0. If it is, we print "The number is positive." If the condition is false, we move to the next condition using the else if statement. If none of the conditions are true, we execute the code inside the else block.

By using control flow statements like the if statement, we can make our programs more dynamic and responsive by executing different blocks of code based on different conditions.

Now it's your turn to try out some control flow statements and decision-making in .NET!

CSHARP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment