Learn c# online

c sharp code

Conditions (using C# example)

The easiest way to understand a condition is to think of choices you make in the real world. For example if you decide to go left then that is one condition. If you decide to go right that will be another. There are also conditions that can let you do one or more things. If you are talking on the phone and writing something down simulateneously that is a combination of conditions. You can also do multiple conditions that can vary such as walking, talking or running while listening to music. Now I will break this down in programming terms so you can start to picture this. I also want to mention that these lessons are progressive and I will do my built to build upon examples when I can.

By examining the picture above you can see that there are a series of conditions being evaluated. This is written using the IF statement in most programming languages. So in the example it is saying if a person has clicked on CheckBox1 then an action will carry out the statement below that shows prize += "50 dollars". This is basically saying that a person has earned $50 by choosing box 1.

It continues on to show if (CheckBox2.Checked) to show that the second checkbox was selected. Then finally the third. Each of these is adding a character value into the variable prize. You may want to check the section on What you should know to get more clarity on what variables are. It also provides an explanation about characters and strings to assist in this area.

Less Than (<), Greater Than (>) with AND (&&)

The condition statements can check for other type of comparisons such as whether a variable value is within a certain range. Look at the next example below.

programing in c sharp

This statement is using an integer (numerical) variable to hold a value, which in this case is 550. Then the next line that shows (money < 600) is checking to see if the variable you assigned is less than 600. This equates to true since our number is 550 and the comparison value is less than 600. The symbol && is called "AND" which allows different variable ranges to be compared to each other.

The next line shows (money > 500). Once again this result is true since our variable is 500 and the system is checking to see if the money is greater than 500. The way this reads is If your money is less than 600 AND your money is greater than 500 then print "I earned enough money to pay my rent." The line for Response.Write allows printing to a web screen, such as in ASP.NET.

With the next lesson regarding the if statement I want to expand to tell you about the else statement that can catch if the statement has failed (which means the value being compared was found not to be true). As usual it is better to see this for yourself with an example.

c# for beginners

So the else statement takes over when the if statement is not true. I changed this slightly to make the else statement work to show (money >= 550) which means greater than or equal to 550. Since the variable for money is now 400 and the variable value is less than 550 then the else statement prints the message "Uh oh. Better take out a loan instead." Hopefully you can see the importance of condition evaluation with these examples.

The next example performs a series of condition checks using two if statements and three different comparisions. Look at the picture below to get a better feel on what is happening.

c# programmer

The first line sets up a new variable called checkingacct and sets the value to 5000 as an integer. Another integer variable is named savings and the value is set to 5000 also. Finally there is a string that has the word "Visa" contained within. Keep in mind that a string can accept characters and numbers, but cannot perform any math.

OR condition

On the next line you will see if (checkingacct > 2000). This is checking to see if the value of checkingacct is greater than 2000. Beside this you will notice it shows a symbol || (also known as "OR"). You have just been introduced to the next condition which will compare an alternative value against another one. So this one is checking to see if (savings > 2000), which means is savings greater than 2000. Since both of these conditions are true the whole line was be true which allows the logic to continue to the next condition below.

After this you will see a brace symbol ({) which is used to embed the body of the statement. The brace below that shows (}) is how the statement is closed. Be sure also to always match up an open brace { for a close (}) brace. Some of the common errors in logic result from a missing brace (also known as curly brace).

Moving on to the next statement you will see if (creditcard == "Visa"). This is checking to see if the string variable called creditcard is equal to the word "Visa". Since this was initialized earlier to "Visa" this part of the statement will also be true.

Because both of the statements (checkingacct > 200) || savings > 2000) and (creditcard == "Visa") evaluate as true then the whole statement is true from a binary perspective which results in the final statement to show "I made a down payment with my Visa and still had money in my savings account.");

As you can see this is one of the major advantages of nesting multiple conditions in a statement. Now the statement could also be written on one line, but I figured it would be best to break it down for beginners.

Switch statement

Be sure to review the very first example on this page above for Conditions that used the Checkboxes to compare values because the next example is related in a way. As always it is beneficial to first see a visual display of a program using this.

c# coding

Now it's time to break this down. The first line is an integer with the variable name door set to value of 3. After this a string is initialize with the name of prize.

Then the first switch statment is shown. It is using the numerical value of door (which is 3) to check the following conditions below it. So remember that no matter what the statements say it doesn't change the value of the door currently.

The first statement below introduces a new command called case. The case statement serves as a comparison against the value(s) on the right. In this situation we want to see if the first execution show the value of door is equal to one. You can also look at it as if (case == door). Just remember that the number 1 is being supplimented to show that it is comparing an integer value here.

The next statement shows (case 2). Then you see (case 3). These are equivalent to this statement below.

if (door == 1)

Response.Write("The prize is a brand new ipod.");

if (door == 2)

Response.Write("The prize is a brand new washer.");

if (door == 3)

Response.Write("The prize is a brand new car.");

Hopefully that part clicks for you. After this you are introduced to a new command called default. The default command acts as safety net if all of the conditions above have failed. Just picture this as three fireman trying to catch you jumping from a burning building. If there were no firemen below would you still jump? That is what the default statement does. Basically it catches the falling through point if the other conditions resulted in false.

 

Subscribe to the Programmermind newsletter
Privacy Notice: Programmer Mind will not compromise your privacy