Friday, January 13, 2012

selection(conditional) statements

A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not.

The following keywords are used in selection statements:

if
else
switch
case
default

The if Statement
When the condition evaluates to a boolean true, a block of code for that true condition will execute.

It takes the following form:
if (expression)
statement1
[else
statement2]

where:

expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators.
statement1
The embedded statement(s) to be executed if expression is true.
statement2
The embedded statement(s) to be executed if expression is false.

eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ifdecisionstmt
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please type your lucky number");
string number = Console.ReadLine();
if(number=="1")
{
Console.WriteLine("you have won a car");

}
else if(number=="2")
{
Console.WriteLine("you have won a boat");

}
else if(number=="3")
{
Console.WriteLine("you have won a van");

}
else
{
Console.WriteLine("sorry, try next time");

}

Console.ReadLine();
}
}
}

output
please type your lucky number
1
you have won a car

Switch Statement
The switch statement is a control statement that selects a switch section to execute from a list of candidates.

eg :
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}

Each case label specifies a constant value.Control is transferred to the switch section whose case label contains a constant value that matches the value of the switch expression, caseSwitch. If no case label contains a matching value, control is transferred to the default section.

==>no two case labels can contain the same constant value.

==>Execution of the statement list in the selected section begins with the first statement and proceeds through the statement list, typically until a jump statement
is reached, such as a break, goto case, return, or throw. At that point, control is transferred outside the switch statement or to another case label.

==>C# does not allow execution to continue from one switch section to the next.The following code causes an error.

switch (caseSwitch)
{
// The following switch section causes an error.
case 1:
Console.WriteLine("Case 1...");
// Add a break or other jump statement here.
case 2:
Console.WriteLine("... and/or Case 2");
break;
}

Inline conditional operator
Its a shortcut for writing a long code
eg;
string message = "";
if (x>y)
{
message = " car ;
]
else
{
message = "bus";
}

shortcut for this code is :
string message =(x>y)? "car" : "bus" ;

this is called in line conditional operators . Here we are trying to write code in few lines.

No comments:

Post a Comment