C# Workshop – Flow

Standard

Flow

Just doing the same thing over and over doesn’t make a very interesting application, that’s where decisions come in, a program can make a choice what to do based on a condition to choose what path it should follow in the programme and can be used for a variety of purposes such as checking values or validating any user input.

The if statement can be used for conditions, in dotnetfiddle.net enter the following:

using System;

public class Demo
{
	public static void Main()
	{
		int number = 0;
		if (number == 0)
		{
			number = 9 + 2;
		}
		Console.WriteLine(number);
	}
}

The if statement is followed by a conditional statement in brackets which checks if the number is equal to zero, if it is the action in the curly braces will occur, if not it won’t, so try changing the int value of number in dotnetfiddle.net to the following:

int number = 1;

This will change the output value because the number is no longer equal to zero, you can use anything that worked for bool to control what happens within an if statement. It’s also useful to follow the indentation used in the example as it makes it very easy to see if what flow the programme is going to perform so can avoid any problems or confusion later about what will happen inside the if statement, in that example only one path was used based on whether something was true.

When using Conditions, it may be that you want to do something when the value is true then do something different if it isn’t, this is done with an if else statement where if is combined with else so that either one or the other is performed based on the condition being true or false.

To use an if, else statement in dotnetfiddle.net enter the following:

using System;

public class Demo
{
	public static void Main()
	{
		Console.WriteLine("Enter Number between 1 and 10");
		int number = int.Parse(Console.ReadLine());
		if (number >= 1 && number <= 10)
		{
			Console.WriteLine(number + " is valid");
		}
		else
		{
			Console.WriteLine(number + " is not valid");
		}
	}
}

Here if is followed by a conditional statement in brackets – which in this example checks if the number is greater or equal to one and is less than or equal to 10, when this is true the programme will perform the actions in the first set of curly brackets, but when this is false or when it’s something else it will perform the action after that in the second set of curly brackets of the if statement, when the program is run try entering different numbers that are between 1 and 10 or different as see the different conditions of the programme.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s