Here we are, back again.
Ready to learn some more about programming? I hope so, otherwise I don’t know why you’re here. If you’ve somehow stumbled onto this page by accident feel free to try out some of my games. Just saying.
I was trying to figure out the best way to move to our next subject, there’s so much to talk about in regards to programming. Some specific stuff, some broad stuff, some stuff you need to know for other stuff; in general just lots of stuff. I think what I’m going to try is going over a lot of general information you still need to know, as much as I can think of that would be useful right now. Again as before any interesting subjects that I may not cover (that you should google) will be in bold.
Now just a general thing as it wasn’t mentioned previously, keywords: they’re important. Keywords are terms that are predefined and used by your code. In fact, you’ve already used them. Ints, bools, strings, they’re all keywords; and there are a lot more used for different things you can do in code. You cannot name a variable the same as any existing keywords, for obvious reasons. Here’s a list of some more keywords to look at.
Conversions
Converting variables is pretty cool, the hot thing on the block right now, and pretty much forever. You’re able to change a variable of one type to another but with some limitations. You can’t just change a string into a bool, unless you’re some wizard. There are different kinds of conversions and some take more work than others.
Implicit
Implicit conversions are ones you don’t really have to do anything with. Lets say you have a float variable, if you try to assign an int value to that float it just accepts that, you don’t need to do anything more. You can just do float myFloat = someIntValue
without issues.
Explicit
These are the other side of conversions, when we cannot implicitly convert between types. This means our code cannot figure out how to make a float into a int even though it’s possible. But we can force it to, though there are still limitations (again, cannot make a string into a bool). A float can be made into an int but we have to change some of its information. If we make a float with a value of 1.4f, as an int it will become 1.
To actually convert this way we just wrap what type we want to convert to in parenthesis, like so: int value= (int)myFloat;
There are some other nifty things you can do, like making your own classes be able to convert to other types, but if you want to know more about that you can read up about it on the Microsoft Docs.
Logical Operators
Logical operators are very important, they allow you to check boolean values. If you remember the bool
variable type, it represents a value that is either true or false. So you could check this for example
int number = 5; bool isNumberFive = number == 5;
Here, we check if our int value is 5, if it is then the bool will hold true. But what if we want to check multiple things? We don’t want to keep checking a bunch of separate bool variables. We can do this in one, with logical operators.
We have three main operators,
||
OR &&
AND !
NOT
And they do what they sound like. You can check if something is one value or another, check if one value is true and another is true; or you can check if a value is not true. Let’s look at an example.
int number = 5; int numberOther = 7; bool boolCheck1 = number == 5 || numberOther == 3; bool boolCheck2 = number == 5 && numberOther == 3 bool boolCheck3 = !(number == 5); bool boolCheck4 = number != 5;
boolCheck1: This is true because at least one of these bool checks are true.
boolCheck2: This is equal to false, because not all bool checks are true.
boolCheck3: This is false, because number == 5 is true, and then we negate it.
boolCheck4: This is similar to the last one, we just check if it’s not equal to five.
String Literals and Escape Characters
Oh boy strings. Strings have some nifty things, since almost everything using strings for displaying information, or manipulating it, we need to be able to do a lot with them. Two of those important concepts are escape characters and string literals.
First escape characters. When you write a string you’ll notice it’s just a single line of text, well what if you wanted to go down to a new line or if you wanted a tab? That’s where escape characters come in. You can add these special characters to define a tab or new line in your string. Pretty cool huh? How does this work?
All the escape characters are prefaced with a \, so for a new line it’s ‘\n’, for a tab it’s ‘\t’. If you wanted to use these, just add them into your string like so:
string whatever = "Hello Sir,\n\tI wanted to say you look dashing today."
Try throwing that into your project and watch what comes out, for some more character check this out.
Now character literals are for when you don’t want escape characters, you want the string to display exactly as you type it. This is often used for file paths, since \ is used in both file paths and escape characters, Windows can be confused. If you don’t use a literal you have to use the escape character for a \, which is another slash. For example: C:\\Documents\\SomeDoc.doc
To avoid this, among other escape character issues we can simply add an @ sign in front of it, like this: string myPath = @"C:\Documents\SomeDoc.doc"
, and tada!
Constants
Don’t we all wish we had some constants in our life? So does our code, and we can give our code some consistency! We’re just so generous.
We can use constants in our code to define something that will never change, you set their value once when you define the variable. If you try to change that value elsewhere you’ll have your compiler yelling at you. Why might be wanna do this? Well it’s useful for values you know won’t change and don’t want to accidentally change (this can be especially useful in a team where there’s lots of code flying around). Also, since the compiler knows this value will never change it can do some optimizations, so it has a slight performance impact. For example the value of pi, we know that will always be the same so we could define it as such.
So how do we actually make a variable const? It’s really easy! All you do is stick the const keyword in front of the variable like this: const float PI = 3.14f
. That’s it, now that value is const!
Extra Practice
Alright I’ll be honest, I don’t have any particularly good challenges for these subjects. This tutorial was kinda a mashup of a lot of general topics, if you want to practice using them, just do it. Throw some const in there, do some conversions. More than likely you won’t use these concepts until you actually realize you’ll need them. But when you do realize.. you’ll know how!
Next Time
Next time we’re gonna go over if statements, switches, and enums!
Support
Are you having trouble with understanding this tutorial? Please feel free to contact me via email at KoseckCory@gmail.com or message me on discord at 7ark#8194.
I would love to get feedback from people so I can add and improve these tutorials overtime. Letting me know what you’re confused about will let me update the tutorials to be more concise and helpful.
If you’re interested in supporting me personally, you can follow me on Twitter or Instagram. If you want to support me financially, I have a Patreon and a Ko-fi.
1 comment on “Programming in C#, Part 2: Keywords and Logical Operators”Add yours →