Software
C# Tutorial For Beginners
Software Basics - Part 2
C# Tutorial

Here we explain some more of the basics of software. Including how to make a decision and how to go round in circles!

Conditions

Often in software, you want to do something different depending on what the user has entered into your program, what the result of a calculation is, what time of day it is or a million other things that are possible. To do this you need what is called a condition. In the previous section you saw how to declare variables and how to give them a value, now you can use them to decide what to do.

To perform an action depending on a condition, in C# you use the 'if' keyword. You also need a condition, such as 'is equal to', or 'is not equal to'. A condition usually looks something like this:

if(a==0)
{
    action;
}

Which means if the variable 'a' equals 0 then do the action that is in the braces (between the { and } symbols).

Here's a list of conditions that you can use:

Condition Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

You can also combine a number of conditions with what are called 'conditional logical operators' or to put it simply, by adding an 'and' or an 'or' between them. To use an 'and' or an 'or' you use the following symbols:

Conditional Logical Operator Description
&& And
|| Or

Here's an example (notice you can also have more than 1 action between the braces that will be executed sequentially):

if(a==0 && b!=5)
{
    action1;
    action2;
}

Which means if the variable 'a' equals 0 and 'b' does not equal 5 then do the action that is in the braces.

You can also perform another action (or actions) if the condition is not met by using the 'else' keyword:

if(a==0 && b!=5)
{
    action1;
    action2;
}
else
{
    action3;
    action4;
}

Actions 3 and 4 are performed if either 'a' does not equal 0 or 'b' does equal 5.

For Loops

If you want something to occur a number of times in your program you can use loops to repeat a particular action. How many times something is repeated can be fixed before the program starts, or can be decided as the program runs.

If you know how many times you want to repeat an action you use a 'for' loop, which usually consists of a variable that controls how many times you want to repeat an action, a condition, which works in the same way as an 'if' statement that we saw earlier and an expression that modifies the control variable.

Here's an example:

for(int a = 0 ; a < 100 ; a++)
{
    action1;
    action2;
}

The 'int a = 0' is a declaration that creates an integer variable called 'a' and gives it the value of 0 before the loop starts. This is the variable that will be used to control how often the loop iterates.

The 'a < 100' part is the condition that tells the program how many times the loop should repeat. It can be read as while 'a' is less than 100.
The loop will occur 100 times from when 'a' is 0 at the start until 'a' is 99, when it reaches 100 the loop exits and the code carries on after the closing brace.

The 'a++' section is an expression that causes 'a' to be incremented on every pass of the loop. The loop could also go backwards by using a decrement expression -- or it could go twice as quickly by using an expression such as 'a=a+2'.

Notice there is no semi-colon ';' after the 'if' statement, its important that there isn't one otherwise the loop won't perform the actions until it's finished.

It is always necessary to take care that the loop will not get stuck, if the condition can never be true because the expression does not allow it then the program will be stuck in an 'infinite loop', which luckily can usually be stopped by closing the program or more drastically by pulling the plug on the computer. For example the following loop would never end because by adding 2 to 'a' each loop it would never be equal to 99, the condition is to perform the loop while 'a' does not equal 99, which it never will:

for(int a = 0 ; a != 99 ; a=a+2)
{
    action1;
    action2;
}

While Loops

Another type of loop is the 'while' loop which has the following format:

while(a < 100)
{
    action1;
    action2;
}

A 'while' loop can be used when you don't know how many times you want the loop to execute while you are writing your program. It uses the actions within the loop to modify the control variable, in this case 'a', until the condition is true.

Do Loops

Similar to a while loop is a 'do' loop which has the following format:

do
{
    action1;
    action2;
}while(a < 100);

A 'do' loop is different to a 'while' loop because the actions will always be executed at least once. In a 'while' loop the condition is tested and needs to be true for the actions to be performed.

Notice that at the end of the 'do..while' there is a semi-colon after the condition in brackets.

          Previous   Next