Čítať v angličtine Upraviť

Zdieľať cez

Learn conditional logic with branch and loop statements

Make decisions using the if statement

Run the following code in the interactive window. Select the Enter focus mode button. Then, type the following code block in the interactive window and select Run:

C#
int a = 5;
int b = 6;
if (a + b > 10)
    Console.WriteLine("The answer is greater than 10.");

If you are running this on your environment, you should follow the instructions for the local version instead.

Modify the declaration of b so that the sum is less than 10:

C#
int b = 3;

Select the Run button again. Because the answer is less than 10, nothing is printed. The condition you're testing is false. You don't have any code to execute because you've only written one of the possible branches for an if statement: the true branch.

Tip

As you explore C# (or any programming language), you'll make mistakes when you write code. The compiler will find those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise will help you learn the structure of C# code.

This first sample shows the power of if and boolean types. A boolean is a variable that can have one of two values: true or false. C# defines a special type, bool for boolean variables. The if statement checks the value of a bool. When the value is true, the statement following the if executes. Otherwise, it's skipped.

This process of checking conditions and executing statements based on those conditions is powerful. Let's explore more.

Try the code in your browser