Operators � There are a few basic math operators that you will use in almost every program you ever write. These are: +-*/ or , in writing, plus, minus, times and divide. � So how do you use them in your program? Let's find out. � Lets start with a variable: � int myNumber = 0; � � So we have a variable with the number 0 in it. Now lets add 5 to it. � myNumber = myNumber + 5; Console.WriteLine(myNumber); Console.ReadLine(); � Now when you run the program the number 5 will show on the screen. Was that simple or what? And it's the same for any of these operators. � But that looks a bit ugly doesn't it? Stating the variable twice. Here's a way of making it look better. � myNumber += 5 � Now what we are saying is "take the variable myNumber and add 5 to it." Looks better doesn't it? Either way works and will remain up to you which method you prefer. � � Displaying Multiple Variables � Ok so now we'll see how to display several variables at once. Lets start with 3 strings � string myString1 = "Hello"; string myString2 = "World"; string space = " "; � Now look at how simple it is to display them together: � Console.WriteLine(myString1 + space + myString2); � That will output "Hello World" � But if you don't want to use a variable to hold the space then thats easy to do as well. � Console.WriteLine(myString1 + " " + myString2); � This will give us the exact same result while taking up less memory. � � So finally in the next chapter we're going to see how to accept user input and use it in an output. So hop on over and I'll see you there. �
|