Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 3 - Decisions


if statement

The if statement is used when you want to make a decision in your program. Here is an example of how you would check if the user enters the correct password.
Console.Write("Enter password: ");
string password;
password = Console.ReadLine();
if (password == "abc")
   Console.Write("The password is correct");


You will see from the example above that the if statement starts with the if keyword. It is followed by the condition in brackets. Notice that when you compare 2 values you must use 2 equals signs instead of 1. The code after the brackets is run only if the condition evaluates to true. This means that if the password entered by the user is equal to "abc" then it will print the message telling the user that the password is correct.
If you want to run some code for when the condition is false then you must use the else keyword.
if (password == "abc")
   Console.Write("The password is correct");
else
   Console.Write("The password is wrong");

If you want to use more than one command for either the true or the false part then you must put them all between curly brackets.
if (password == "abc")
{
   Console.Write("The password is correct");
   Console.Write("You may use the program");
}
else
{
   Console.Write("The password is wrong");
   Console.Write("Try again");
}

Not only can you check if values are equal but you can also check if they are not equal among other things. Here are the comparisons that you can use.
==Equal
!=Not equal
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to
You can use && (AND) to make 2 or more comparisons at once. Here is an example of how to check that both the username and password entered by a user are correct.
string username,password;
Console.Write();
username = Console.ReadLine();
Console.Write();
password = Console.ReadLine();
if (username == "abc" && password == "def")
   Console.WriteLine("Access Granted");

You can also use || (OR) to check if one of the comparisons is equal to true. Here is a simple example where the value of i is not equal to 5 but the value of j is equal to 6 which means the condition evaluates to true.
int i = 5;
int j = 6;
if (i == 5 || j == 6)
   Console.WriteLine("Either i == 5 or j == 6");

switch

The switch statement is used to compare 1 value to many others at the same time instead of having to write out a whole lot of if statements. Here is an example that tells the user what they entered if they entered either a 1, 2 or 3 and prints a default message if they didn't enter any of those numbers.
Console.Write("Enter a number: ");
string s = Console.ReadLine();
switch(s)
{
   case "1":
      Console.WriteLine("You entered 1");
      break;
   case "2":
      Console.WriteLine("You entered 2");
      break;
   case "3":
      Console.WriteLine("You entered 3");
      break;
   default:
      Console.WriteLine("You entered some other number");
      break;
}

The value to be compared against all the others goes in brackets after the switch keyword. All other parts of the switch statement must go between brackets. A case must be used for each value that must be compared followed by the code to run if the values are equal. The break keyword must be used to exit the switch without doing any of the other comparisons. The default is run if no values are matched.

0 comments:

Post a Comment

Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India