Control Statements - Selection
WriteLine Substitution Parameters
- When you call WriteLine to print a value, you can use substitution parameters. These are numbers, starting with 0 and counting up, enclosed in braces {}. You place the value to substitute for the parameter after the close quote, separated by commas.
- You can have more than one substitution parameter, as long as you count up from zero, and you ensure that the substituted values are listed in the order they are numbered.
int intOne = 5, intTwo = 7, intThree=9;
Console.WriteLine("one: {0}, two: {1}, three: {2}, one again: {0}", intOne, intTwo, intThree);
Control Statements
The if Statement
An if statement allows you to take different paths of logic, depending on a given condition.namespace HelloWorldConsole
{
class Program
{
static void Main(string[] args)
{
string userInput;
int userAge;
Console.Write("Please enter you Age: ");
userInput = Console.ReadLine();
userAge = Int32.Parse(userInput);
#region Single Decision and Action with braces
if (userAge > 0)
{
Console.WriteLine("Your age {0} is greater than zero.", userAge);
}
#endregion
#region Single Decision and Action without brackets
if (userAge < 0)
Console.WriteLine("Your age {0} is less than zero.", userAge);
#endregion
#region Either/Or Decision
if (userAge != 0)
{
Console.WriteLine("Your agee {0} is not equal to zero.", userAge);
}
else
{
Console.WriteLine("Your number {0} is equal to zero.", userAge);
}
#endregion
#region Multiple Case Decision
if (userAge < 0 || userAge == 0)
{
Console.WriteLine("This user is new born, his age {0} yaers", userAge);
}
else if (userAge > 0 && userAge < 10)
{
Console.WriteLine("This user is a chiled , his age is {0} yaer", userAge);
}
else if (userAge >= 10 && userAge <= 20)
{
Console.WriteLine("This user is a teen age , his age is {0} yaer", userAge);
}
else
{
Console.WriteLine("This user is a mature , his age is {0} yaer", userAge);
}
#endregion
}
}
}
The switch Statement
Another form of selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter.namespace PatFinTyp
{
class Program
{
static void Main(string[] args)
{
string userInput;
int finTyp;
Console.WriteLine("please enter number between 1 & 3");
userInput = Console.ReadLine();
finTyp = Int32.Parse(userInput);
#region switch on a value
switch (finTyp)
{
case 1:
Console.WriteLine("Patient Financial Type is Free ");
break;
case 2:
Console.WriteLine("Patient Financial Type is Self-Pay ");
break;
case 3:
Console.WriteLine("Patient Financial Type is Insured ");
break;
default:
Console.WriteLine("error : please enter number between 1 & 3 ");
break;
}
#endregion
}
}
Post a Comment