Decision making statements - C# FOR BEGINNERS CRASH COURSE (2014)

C# FOR BEGINNERS CRASH COURSE (2014)

Chapter 6 C# Decision making statements

Decision making is structured in the following way:

· There must be at least one condition for the statements to meet

· There must be at least two end outcomes

Looking at the diagram below, we can see a general representation of the decision making logic.

In the diagram above, the arrows represent the flow of statements executing in the system. When the statement reaches a conditional logic (diamond shape) the statement is matched to the condition. This condition has two outcomes True or False.

If the statement matches the condition then it will be declared as True and the statement will then run a set of code (rectangle) before reaching the end. If the statement does not match the condition then it will be declared as false and will directly proceed to the end point.

C# has various types of decision making statements. They are explained in detail.

6.1 If Statement

If statement contains a Boolean expression True or False. It is followed by one or more statements.

Syntax:

if (boolean_expression)

{

//if the Boolean expression is true, statement executed

}

If the expression contains value true, the statements in the block of code will be executed. If the expression has value false, the set of code after the end of if statement is executed.

Flow Diagram

Example of if statement

Example 7:

using System;

namespace decision

{

class Program

{

static void Main(string[] args)

{

/*local variables*/

int i = 15;

/*boolean condition is checked*/

if(i < 15)

{

Console.WriteLine("The value of i is less than 15");

}

Console.WriteLine("Value of i is{0}",i);

Console.Read();

}

}

}

In this code example we can see that the condition is if(i<15).So here the variable i must meet the condition to be less than 15 for the condition to be True or else it is False.

6.2 If else statement

An if statement can be added with an optional else statement. The else statement is executed when the expression value is false.

Syntax:

if(boolean_expression)

{

/* If the Boolean expression is true, this statement is executed

}

else

{

/* If the Boolean expression is false, this statement will be executed

}

If the expression has value true then the if block is executed, otherwise else block is executed.

Flow Diagram

Example of if else statement:

Example 8:

using System;

namespace ifelseconstruct

class User

{

static void Main(string[] args)

{

int age;

Console.WriteLine("Enter the age of the user");

age=Convert.ToInt32(Console.ReadLine());

if(age < 18)

{

Console.WriteLine("Not an adult");

}

else

{

Console.WriteLine("User is an adult");

}

}

}

In the code example, the condition ( age < 18 ) is specified. So here the variable age must meet the condition less than 18 for the condition to be True, otherwise the else block is executed.

The else if else statement

An if statement is also followed by an else if else construct. Many conditions can be checked using if else if statement.

Syntax

if (boolean_expression 1)

{

/*If the Boolean expression 1 is true, the statement is executed*/

}

else if(boolean_expression 2)

{

/* If the Boolean expression 2 is true, the statement is executed*/

}

else if(boolean_expression 3)

{

/* If the Boolean expression 3 is true, the statement is executed*/

}

else

{

/* executes if none of the condition is true*/

}

Example of else if else statement

Example 8:

using System;

namespace ifelse

{

class Program

{

static void Main(string[] args)

{

int x = 30;

if(x == 100)

{

Console.WriteLine("The value is 100");

}

else if(x == 20)

{

Console.WriteLine("The value is 20");

}

else if(x == 10)

{

Console.WriteLine("The value is 10");

}

else

{

Console.WriteLine("No matching value found");

}

Console.WriteLine("Value of x is:{0}",x);

Console.Read();

}

}

}

In the above example, the value x = 30 is specified. If there is an expression that matches the value of x, the appropriate block is executed. If none of the expression matches, the else block is executed.

6.3 Nested if statement

The nested if statement contains one if or else if statement inside another if or else statement.

Syntax for nested if statement

if(boolean_expression1)

{

/*If the expression 1 is true, statement is executed*/

if(boolean_expression2)

{

/*If the expression 2 is true, statement is executed*/

}

}

Example for nested if statement

Example 9:

using System;

namespace nestedif

{

class Program

{

static void Main(string[] args)

{

int x = 20;

int y = 40;

if(x == 20)

{

if(y == 40)

{

Console.WriteLine("Value of x is 20 and y is 40");

}

}

Console.WriteLine("Value of x is:{0}",x);

Console.WriteLine("Value of y is:{0}",y);

Console.Read();

}

}

}

In the above code, the value for x and y variables is assigned. The value inside if statement is checked. If it is same, the inner statements are executed, else the control moves out of the loop.

6.4 Switch statement

A switch statement helps to evaluate a variable for multiple conditions efficiently. Every condition is known as a case and the variable switched is checked for the switch case.

Syntax:

switch(Expression)

{

case ConstantExpression1:

statements;

break;

case ConstantExpression2:

statements;

break;

…..

case ConstantExpression_n:

statements;

break;

default:

statements;

break;

}

The rules added in the switch statement are:

· Expression in a switch statement must be an integer or of a class type.

· A user can add any number of case statements in a switch statement.

· The ConstantExpression must be of the data type similar to the expression in the switch.

· When the Expression matches the ConstantExpression, the code inside that case will be executed and will hit the break keyword which will force the execution out of the switch statement.

· Every case does not need a break keyword.

· The optional default case appears at the end of the switch and acts as a backup in case the expression does not match any of the ConstantExpressions.

Flow Diagram

Example of switch statement

Example 10:

using System;

namespace switchcase

{

class Program

{

static void Main(string[] args)

{

char grade ='A+';

switch(grade)

{

case C:

Console.WriteLine("Fair");

break;

case B:

Console.WriteLine("Good");

break;

case A+:

Console.WriteLine("Excellent");

break;

default:

Console.WriteLine("No matching grade");

break;

}

Console.WriteLine("Grade is {0}",grade);

Console.ReadLine();

}

}

}

In the above code, the value of grade is assigned. It is matched with the different switch cases. If the value matches from among the declared cases, the corresponding message is displayed. If none of the expression matches, the default caseis executed.

6.5 Nested switch statement

User can switch part of the statement sequence of the outer switch. The common values in the switches do not conflict.

Syntax:

switch(c1)

{

case ‘A’:

Console.WriteLine(“A is the part of outer switch”);

break;

switch(c2)

{

case ‘A’:

Console.WriteLine(“ A is the part of inner switch”);

break;

case ‘B’:

}

break;

case ‘B’:

}

Example of nested switch statement

Example 11:

using System;

namespace switchcase

{

class Program

{

static void Main(string[] args)

{

char grade ='A+';

switch(grade)

{

case C:

Console.WriteLine("Fair");

break;

case B:

Console.WriteLine("Good");

break;

case A+:

Console.WriteLine("Excellent");

break;

default:

Console.WriteLine("No matching grade");

break;

}

Console.WriteLine("Grade is {0}",grade);

Console.ReadLine();

}

}

}

Example 12:

using System;

namespace nestedswitch

{

class Program

{

static void Main(string[] args)

{

int x = 20;

int y = 40;

switch(x)

{

case 20:

Console.WriteLine("Part of outer switch");

switch(y)

{

case 40:

Console.WriteLine("Part of inner switch");

break;

}

break;

}

Console.WriteLine("Value of x is :{0}",x);

Console.WriteLine("Value of y is: {0}",y);

Console.Read();

}

}

}

In the above code, the value of variable x and y is assigned. If the value for variable x matches the expression, the statements inside the switch case are executed, else the control moves out of the case structure