In the previous lesson, we learned what data types are today, we will learn to add them, multiply etc., in other words do something with them
1. Arithmetic operators
We have five basic operators
1. Addition +
int a = 12;
int b = 34;
int c = a + b;
Console.WriteLine(a+"+"+b+"="+c);
Console.ReadKey();
2. Substraction –
int a = 12;
int b = 34;
int c = b-a;
Console.WriteLine(b+"-"+a+"="+c);
3. Mulitplication *
int a = 12;
int b = 34;
int c = b*a;
Console.WriteLine(b+"*"+a+"="+c);
4. Division /
int a = 12;
int b = 34;
int c = b/a;
Console.WriteLine(b+"/"+a+"="+c);
5. Modulo (the rest from division) %
int a = 12;
int b = 34;
int c = b%a;
Console.WriteLine(b+"%"+a+"="+c);
There are several additional operators improving work
+= -= *= /= %=
If we had to perform such an action, for example:
int b = 34;
int c = 43;
c =c + b;
Console.WriteLine(c+"+"+b+"="+c);
We can save it shorter in this way:
int b = 34;
int c = 43;
c += b;
Console.WriteLine(c+"+"+b+"="+c);
It works the same for other operators
There are also operators of increment and decrement, you can use them if you want to increase or decrease the variable by 1, it looks more or less like this.
int c = 43;
c++;
Console.WriteLine(c);
c--;
Console.WriteLine(c);
2. Logic operators
Another four basic operators:
AND && (i) – returns true if both conditions are true
OR || (or) – returns true if even one condition is true
NEGATE! (no) – changes condition true to false and vice versa
EXCLUSIVE-OR ^ (alternative rule) – will return true if only one condition is true
And here is an example program:
bool conditionTrue = true, conditionFalse = false;
Console.WriteLine(conditionTrue && conditionFalse);
Console.WriteLine(conditionTrue || conditionFalse);
Console.WriteLine(!conditionTrue);
Console.WriteLine(conditionTrue ^ conditionTrue);
The result of the operation:
1.false
2.true
3.false
4.false
Why so?
The first line is the && operator – it will be true if all conditions are true in our case, one condition returns false so the whole line returns false
The second line is the operator || – it returns true if at least one of the conditions is true in our case the condition has been acomplished because one variable returns true
The third line is the operator! – this operator is a negation and returns the opposite of the given condition, we gave true so it returns false
The fourth line is the ^ operator – it returns true if at least one part is true, we gave both parts true so it returns false3.
3. Comparison operators
- equal ==
- different! =
- greater than or equal to> =
- less than or equal to <=
- smaller <
- bigger >
4. Conditional operator
Unreadable so rarely using, however, I just wanted to show that something like that. It looks like this:
int variable1 = 150;
string x = variable1 > 100 ? "bigger" : "smaller";
Console.Write(x);
This program will display “bigger” on the screen. Why?
Well, this condition always returns the first value after the question mark when the condition is true, if the condition returns false, the value is returned after the colon.
This content also you can find on my blog devman.pl: http://devman.pl/csharplan/c-language-operators/
If you recognise it as useful, share it with others so that others can also use it and leave upvote and follow if you wait for next articles :) .
For today, probably enough of these operators. To see in the next lesson!