How do you sand on Rane mp2016s

Conditions for branch and loop statements

  • 5 minutes to read

In this tutorial, you will learn how to write code that examines variables and, based on those variables, changes the path of execution. You write some C # code and there you see the results of your code compiling and executing. This tutorial contains a number of lessons that explore branching and looping constructs in C #. In these lessons you will learn the basics of the C # programming language.


This tutorial assumes that you have set up a computer for local development. On Windows, Linux or macOS you can use the .NET CLI to program, build and run applications. You can use Visual Studio 2019 on Mac and Windows. For setup instructions, see Introduction to .NET Development Tools.

Make decisions using the statement

Create a directory named branches tutorial. Set this as the current directory and run the following command:

This command creates a new .NET console application in the current directory. Open Program.cs in your favorite editor and replace the content with the following code:

Test this code by typing in your console window. You should see the following message in your console: "The answer is greater than 10." Change the declaration from so that the sum is less than 10:

Please enter again. Since the answer is less than 10, nothing is output. The one you tested condition is wrong. There is no code to run because you have just written one of the possible branches for a statement: the true branch.


Your first steps with C # (or any other programming language) can run into errors when writing code. The compiler finds and reports the errors. Take a close look at the error output and the code that generated the error. The compiler error can usually help you identify the problem.

The first example illustrates the advantages of statements and Boolean types. A boolean-Type is a variable that can contain one of the following two values: or. A special type of Boolean variable,, is defined in C #. The statement checks the value of a type. If the value is, the following statement is executed. Otherwise it will be skipped. This process of checking conditions and executing instructions based on those conditions is effective.

Combining if and else statements

To run some other code on the true and false branches, create a branch that runs when the condition is false. Try branching. Add the last two lines in the code below (the first four should already be there):

The statement that follows the keyword is only executed if the condition to be tested is. When you combine and with Boolean conditions, you must process both a and a condition.


The indentation under the and instructions is for better readability. In the C # programming language, indentations or spaces are not taken into account. The statement following the keyword or is executed based on the condition. All of the examples in this tutorial follow the common practice of indenting lines based on the flow control of instructions.

Since indents are not relevant, use and to indicate that you want to use more than one statement as part of the conditionally executed block. C # programmers typically use curly braces like this with all - and statements. The following example is identical to the content you created. Modify the above code to match the following code:


For the remainder of the tutorial, all code examples include curly braces, as per common practice.

You can test more complicated conditions. Add the following code under the code you already wrote:

The symbol tests for equality. The use of differentiates the test for equality from the assignment you saw in.

The sign stands for "and". It means that both conditions must be true for the statement in the true branch to be executed. These examples also show that you can use multiple statements in any conditional branch as long as you surround them with and. You can also use for "or". Add the following code under the code you already wrote:

Change the values ​​of, and, and switch between and to examine them. This will give you a better understanding of how the and operators work.

You have completed the first step. Before proceeding to the next section, let's move the current code into a separate method. This makes it easier to work with a new example. Paste the existing code into a method called. Call them at the beginning of your program. After making these changes, the code should look like this:

Comment out the call to. This will make the output less cluttered as you work in this section:

With becomes a comment started in C #. Comments are pieces of text that you want to keep in your source code but not run as code. The compiler does not generate any executable code from the comments.

Repeating operations by grinding

In this section you will use grindto repeat instructions. Add this code after calling:

The statement checks a condition and executes the statement or statement block after. It repeats checking the condition and executing these statements until the condition is false.

In this example there is another new operator. The -sign after the -variable is the increment-Operator. It increases the value of by 1 and stores this value in the variable.


Make sure the loop condition changes to false after running the code. Otherwise, create one Infinite loopwhich never terminate the program. This is not shown in this example because you are programmatically using CTRL + C or prevent other means.

The loop tests the condition before executing the code after. The ... loop first executes the code and then checks the condition. The do whileLoop is shown in the following code:

This loop and the previous loop produce the same output.

Working with the for loop

The forLoop is commonly used in C #. Test the following code:

The previous code works the same way as the loop and loop that you have already used. The statement has three parts that control how it is executed.

The first part is the for initializer: declares the loop variable and sets the initial value to.

The middle part is the for condition: declares that this loop will be executed as long as the value of the counter is less than 10.

The last part is the for iterator: indicates how the loop variable is changed after the block following the statement has been executed. Here it indicates that it should be increased by 1 for each block execution.

Experiment for yourself. Try each of the following variations:

  • Change the initializer to start with a different value.
  • Change the condition to stop at another value.

When you're done, move on to code yourself using what you've learned.

Another loop statement is not covered in this tutorial: the statement. The statement executes the statement for each element in a sequence of elements. Since they most often use Collections used, it will be covered in the next tutorial.

Created nested loops

A,, or loop can be nested inside another loop to create a matrix using the combination of each element in the outer loop and each element in the inner loop. By doing this, you can create multiple alphanumeric pairs that represent rows and columns.

A loop can generate the lines:

Another loop can generate the columns:

You can nest a loop inside the other to form pairs:

You can see that the outer loop increments once for each complete execution of the inner loop. Reverse the nesting of the rows and columns and see for yourself what changes. When you're done, paste the code from this section into a method called.

Combining branches and loops

Now that you have learned about the statement and the loop constructs in the C # programming language, try to write a C # code that finds the sum of all integers from 1 to 20 that are divisible by 3. Here are a few tips:

  • The operator determines the remainder of a division operation.
  • The statement determines the condition to determine whether a number should be included in the sum.
  • The loop allows you to repeat a series of steps for all numbers from 1 to 20.

Try it yourself. Then see how you performed. You should get 63 in response. You can see one possible answer by looking at the completed code on GitHub.

You have completed the “Branches and Loops” tutorial.

You can continue with the Arrays and Collections tutorial in your own development environment.

For more information on these terms, see these articles: