It’s been a while since my last post but life occurred. The sort of fun life and work life you can’t avoid. And a certain xbox game came out too… I’ve been working on this post off and on. Writing it all down is taking longer than I thought it would. Maybe I’ll speed up later.

This is where things start to get complicated and my previous method of explaining what I’m seeing in the book doesn’t really work. From here on out the book presents you with code, asks you to type and compile it, then it explains what happened. I’m not going to paste the code in here, only what I write for any relevant exercises, so it may be a little disjointed.

Displaying Information

Built in to C# there are two ways of displaying information:

  1. System.Console.WriteLine()
  2. System.Console.Write()

The Write and WriteLine are the parts that do the displaying, the System.Console part is how your program finds these. Think of it as a file path on your computer: C:\Program Files would take you to where all your installed programs are.

The difference between these two routines is that one will give you a line break, the other won’t. So if I wrote:

System.Console.WriteLine("I'm learning to code. ");
System.Console.WriteLine("Isn't that cool? ");
System.Console.Write("I'm learning to code. ");
System.Console.Write("Isn't that cool? ");

I’d get the following:

I'm learning to code.

Isn't that cool?

 

I'm learning to code. Isn't that cool?

You can also put information that your program has stored into these routines.

int newNumber = 1234;
System.Console.WriteLine("The new number is {0}", newNumber);

I’d get:
The new number is 1234
You can add multiple values in the routine if you like, just add more numbers in swirly brackets and more variables.

int newNumber = 1234;
int secondNumber = 9876;
int thirdNumber = 5678;
System.Console.WriteLine("The new number is {0}, the second number is {1} and the final number is {2}", newNumber, secondNumber, thirdNumber);

I’d then get:
The new number is 1234, the second number is 9876 and the final number is 5678

Punctuators

Punctuators are how you break up your code. In C# you have the following:

Semicolon – This ends all C# statements. It tells your program that the code you were writing for that particular statement has finished.
Comma – “Stacks” multiple commands. So above when I used WriteLine, I “stacked” 3 variables for use with the routine.
Parentheses – Used to force the order you code will execute. This is the same as their use in maths for complicated equations. For example (2+3)*8 would add 2 and 3 together first, and multiply that answer by 8.
Braces – Group pieces of code. I call these swirly brackets.

Operators

  1. Urnary – Operators that affect a single variable. So the use of – to make a negative number is urnary.
  2. Binary – Operators that affect two variables. The use of – to subtract one number from another is binary.
  3. Ternary – Operators that affect three variables. There’s only one of these in C# and it’s called the conditional operator. You have a statement and provide a value for if that statement is true, and one for if it is false. So, “x=3” is the statement, “X is 3” can be the value for true, and “X is not 3” can be the value for false.
  4. Compound – These are quicker ways of writing binary operators where you update one of the values you are using (see the table below).
  5. Relational – These are used to compare values. x=3 is relational, it compares 3 to the letter x and says they are equal.
Operator Use Description
Compound Operators
+= x += 4 x = x + 4
-= x -= 4 x = x – 4
*= x *= 4 x = x * 4
/= x / = 4 x = x / 4
%= x %= 4 x = x % 4
Unary Operators
++ x++ x + 1
x– x – 1
Relational Operators
> x>3 x is greater than 3 (not equal to 3)
< x<3 x is less than 3 (not equal to 3)
== x==3 x equals 3
!= x!=3 x is not equal to 3
>= x>=3 x is greater than or equal to 3
<= x<=3 x is less than or equal to 3

Note that with unary operators you can have post-increment and pre-increment. x++ is post-increment; the number is incremented by 1 after everything else in the statement has occurred. If you wrote it as ++x (a pre-increment) x would be increased by 1 before everything else.

So, we have:

number = 10;
newNumber = ++number;

In this case both number and newNumber would be 11. This is because the statement increases number by 1, making it 11, and then makes newNumber equal to it. If you did:

number = 10;
newNumber = number++;

Only number would be equal to 11, because newNumber is set to the value of number first, which is 10, before it is incremented by 1.

Turns out my dabbling in code before has always been wrong when it comes to this so there’s my new thing for today learnt.

The If Statement

This is then only way I can code so far.

The if statement is a way of checking something and then performing an appropriate action depending on the result. If I’m hungry I should eat.

If (hungry == "yes") {
System.Console.WriteLine("You should eat");
}

With the if statement you can use any of the relational operators mentioned above. If we wanted to tell people over the age of 21 that they were old:

If (age => 21) {
System.Console.WriteLine("You are old, dude");
}

You can also use things like AND && and OR || to get more complicated if statements. If, for some strange reason, I wanted to check if anyone else has the same name as me, I ‘d use:

If (name == "Michelle-Louise" && surname == "Janion") {
System.Console.WriteLine("You have a cool name");
}

And if I wanted to see if you were roughly the same age as me (older than 25 but younger than 30):

If (age > 25 || age < 30) {
System.Console.WriteLine("You are roughly the same age as Misky");
}

You can use AND or OR together too, if you like. Just think about how you would get your answer in English, then swap in the terms you’ve learnt above.

I want to look for people between the ages of 25 and 30, inclusive, that have the name “Ashley”.

  • I said people, so that’s male OR female.
  • I said between a set of ages so that’s an OR there.
  • I said inclusive so you’ll need >= and <= rather than > or <.
  • As I’m looking for people with the name Ashley that match the other items I’ll need an AND for that part, but an OR for the others.
If (name == "Ashley" && (age >= 20 || age <=30 || gender == "male" || gender == "female")) {
System.Console.WriteLine("This is one of the people you are looking for");
}

Note that to make this work for a big list of people you’d need some other code. Also note that I put some extra brackets in there. This makes sure that all the ORs are kept together so you don’t accidentally check for the wrong things. If I wrote:

If (name == "Ashley"  && age => 20 || gender == "male" || gender == "female") {

It would list people who were over 20 and named Ashley, and all people who were female, and all people who were male. If the only two genders are male and female, everyone would be listed and the name and age parts wouldn’t count toward anything.

The reason this wouldn’t have worked is because of precedence.

Precedence

This is something you’ll have been taught when you did basic maths. In code, the same rules apply for precedence, there are just some extra bits. I have not added all of the ones listed in the book. These are apparently mentioned later.

Level Operator Types Operators
1 Primary operators () . [] x++ x–  
2 Unary operators + ! ~ ++x –x
3 Multiplicative * / %      
4 Additive +        
5 Shift >> <<        
6 Relational >= <= > <    
7 Equality == !=        
8 Logical AND &          
9 Logical XOR ^          
10 Logical OR |          
11 Conditional AND &&          
12 Conditional OR ||          
13 Conditional ?:          

Converting Data Types

This is how you change what you are storing. For example, you want to store a number in a smaller data type because you know for sure you don’t need a variable that big. You have to make sure that the variable you are wanting to change your data to can accept it though, which is what this section is about. For example, you can’t put “Hello world” into an int because it isn’t a number.

From To
sbyte byte short ushort int uint long ulong char float Double Decimal
sbyte              
byte                    
short            
ushort                
int          
uint            
long        
ulong        
char                  
float    
double  
decimal  

You should also be aware that when you do maths on two different variable types, that they will be converted to the same type before the maths occurs. Whichever item is smaller, is “promoted” to the other.

Terms

These terms are written as I understand them.

https://jambonium.co.uk/glossary/binary-operator/
https://jambonium.co.uk/glossary/compound-operator/
https://jambonium.co.uk/glossary/data-type/
https://jambonium.co.uk/glossary/if-statement/
https://jambonium.co.uk/glossary/precedence/
https://jambonium.co.uk/glossary/punctuator/
https://jambonium.co.uk/glossary/relational-operator/
https://jambonium.co.uk/glossary/routine/
https://jambonium.co.uk/glossary/ternary-operator/
https://jambonium.co.uk/glossary/urnary-operator/