Wednesday, August 17, 2011

Selection Statements

Programs often have to make choices about what to do with a value. If statements and switches are programming structures used to select what to do if a value falls into a certain range.

If statements


An if consists of the if keyword and a condition in parenthesis. The condition must evaluate to true or false (a bool). If there is only one statement after the if you don't have to use curly braces {}, though I would encourage you to use them just to be in the habit. If there are multiple statements dependent on the condition you do have to use curly braces.

Here is a method that shows an example of simple if statements




void SimpleIFStatement()
{
Console.WriteLine("Enter a number between 1 and 10");
int number = int.Parse(Console.ReadLine());


if (number <1 )
Console.WriteLine("the number is too small");


if (number > 10)
Console.WriteLine("The number is too Large");

}



The next example shows the use of the "else" clause in an if statement. The else clause handles any value not covered by the main if statement.




void IfElseStatement()
{
Console.WriteLine("Enter your age");
int age = int.Parse(Console.ReadLine());

if (age >= 18)
{
Console.WriteLine("You are old enough, Welcome");
}
else
{
//for anything less than 18
Console.WriteLine("Come back when your are older");
}
}



If you need to test for multiple possible conditions you can use the if, else if, else structure. It is important to note that sequence matters. The program stops at the first true statement. In the following example if I put the lowest temperature first, the program would always read "cool" for any temperature over 40, even if the temperature were 110.




void IFElseIFExample()
{
Console.WriteLine("Enter the day's high Temperature");
int temp = int.Parse(Console.ReadLine());

if (temp >= 90)
{
Console.WriteLine("Way too hot");
}
else
if(temp >= 80)
{
Console.WriteLine("A bit warm");
}
else
if (temp >= 70)
{
Console.WriteLine("Just right");
}
else
if (temp >= 60)
{
Console.WriteLine("Pleasant");
}
else
if (temp >= 40)
{
Console.WriteLine("cool");
}
else
{
Console.WriteLine("Cold");
}

}



Below is an if example used with the TryParse method. If you enter a bad value into the Parse method it crashes the program. With TryParse it doesn't. TryParse returns a boolean, True if the value actually parses into an int or double or whatever, false if it fails to parse. The try parse also requires an out parameter. If the values parses correctly it's value is assigned to the outside variable--in this case "number".
You can test the boolean variable with an if statement to determine if the parse worked or not.



void TryParseExample()
{
bool goodint;
int number;

Console.WriteLine("Enter an integer value");
goodint = int.TryParse(Console.ReadLine(), out number);

if (goodint == true)
{
Console.WriteLine("Your integer is {0}", number);
}
else
{
Console.WriteLine("Not an integer");
}
}



Finally here is an example of if statements with multiple conditions using and (&&) and or (||) to combine them.
In and conditions both sides of the statement must be true for the if to evaluate as true. For an or statement, if one or the other statement is true, then the whole statement is true.
Here is the example:



void ANDORExamples()
{
Console.WriteLine("Enter a number between 1 and 10");
int number = int.Parse(Console.ReadLine());

//and example both must be true
if (number > 1 && number < 10)
{
Console.WriteLine("Valid Entry");
}

//orExample
{
if (number < 1 || number > 10)
{
Console.WriteLine("Invalid Entry");
}
}
}


Switch


Switch is another way to select based on a set of values, but unlike the if else if structure it only matches exact values.




void SwitchExample()
{
Console.WriteLine("Enter a grade 1, 2, 3, or 4");
int grade = int.Parse(Console.ReadLine());

switch (grade)
{
case 1:
Console.WriteLine("D");
break;
case 2:
Console.WriteLine("c");
break;
case 3:
Console.WriteLine("B");
break;
case 4:
Console.WriteLine("A");
break;
default:
Console.WriteLine("Not a valid grade");
break;
}
}








No comments:

Post a Comment