Now at this point in C#, most of everything we have observed so far is similar to Java, and various other object-oriented languages. So let's dive into conditional statements with C# to see if the same ideas hold true.
The first example I would like so show is a very simple conditional to decide if a declared boolean variable, x is true. This can be done easily by using x.Equals(true) as our condition in the if-then statement to check, as shown below.
Simple. Already we can see that this should be a style that we are familiar with.
Getting into a more serious subject to check, I would like to show how an if-then-else statement would show me if I would be able to eat or not. We will use another boolean variable, properly named hasFood. We will check to see if hasFood is true, being if it is then I get to eat tonight. If not, I clearly need to go grocery shopping. The code for this weekly scenario is shown below.
Now I realize that this may be too straight forward of an approach to decide if I can eat tonight without going grocery shopping. There must be another variable to add to determine whether I would even have the money to go grocery shopping in the first place. Enter the else-if statement, and the addition of our next boolean variable hasMoney. So if I have food, everything is fine. If I do not have food, the next thing I would need to check is my bank account. Now if this is not empty (or true for our example), then I will be able to go buy groceries. However, if I neither have food or money (or both are false, in this case our else condition), there must be a third option in which I would have to seek desperate measures in order to eat...finding a friend with available guest swipes at the John Carroll Dining Hall. The code to show this situation is below.
Once again, this is quick and easy to pick up since we have seen code like this before. So now that we are familiar with conditionals in C#, let's check out logical operators other than .Equals().
Logical Operators
The logical operators for C# stick to the same of which we are used to for Java or Python. For example, let a and b be of type int.
- a == b checks to see if a is equal to b
- a != b checks to see if a is not equal to b
- a > b checks to see if a is greater than b
- a < b checks to see if a is greater than b
This is something that we are familiar with, so now that we know how to write conditional statements and use logical operators on variables, a good example to show we know what we are doing is to take a random number x to be from -99 to 99. We can do this by adding a Random function which we will call rand, and its declaration will be the first line of code in our main. Next, since the rand.Next() function only returns a positive value, we will create another int, n, and set it to be a random integer either 0 or 1. If n is 1, we will multiply x by -1, otherwise, we will leave x alone.
After we have decided the official value of x to check, we will run a multi-conditional statement to determine if x is an EVEN or ODD integer, as well as if x is POSITIVE or NEGATIVE. If x is none of these, then x must be ZERO. The code to implement this is shown below along with one example of its output.
This is all great, right? C# provides us with capabilities and methods of implementation that we are already familiar with to work with conditional statements and logic. Once again, we can see that C# has a lot of overlap with our past knowledge; making the beginning stages of learning the groundwork for the language relatively simple.
No comments:
Post a Comment