following block of code:
for (int i = 0; i < 9; i++) {
if (i % 2 == 0) {
//---print i if it is even---
Console.WriteLine(i);
continue;
}
//---print this when i is odd---
Console.WriteLine('******');
}
When i is an even number, this code block prints out the number and skips to the next number. Here's the result:
0
******
2
******
4
******
6
******
8
Operators
C# comes with a large set of operators that allows you to specify the operation to perform in an expression. These operators can be broadly classified into the following categories:
□ Assignment
□ Relational
□ Logical (also known as conditional)
□ Mathematical
Assignment Operators
You've already seen the use of the assignment operator (=
). It assigns the result of the expression on its left to the variable on its right:
string str = 'Hello, world!'; //---str is now 'Hello, world!'---
int num1 = 5;
int result = num1 * 6; //---result is now 30---
You can also assign a value to a variable during declaration time. However, if you are declaring multiple variables on the same line, only the variable that has the equal operator is assigned a value, as shown in the following example:
int num1, num2, num3 = 5; //---num1 and num2 are unassigned; num3 is 5---
int i, j = 5, k; //---i and k are unassigned; j is 5---
You can also use multiple assignment operators on the same line by assigning the value of one variable to two or more variables:
num1 = num2 = num3;
Console.WriteLine(num1); //---5---
Console.WriteLine(num2); //---5---
Console.WriteLine(num3); //---5---
If each variable has a unique value, it has to have its own line:
int num1 = 4
int num2 = 3
int num3 = 5
A common task in programming is to change the value of a variable and then reassign it to itself again. For example, you could use the following code to increase the salary of an employee:
double salary = 5000;
salary = salary + 1000; //---salary is now 6000---
Similarly, to decrease the salary, you can use the following:
double salary = 5000;
salary = salary - 1000; //---salary is now 4000---
To halve the salary, you can use the following:
double salary = 5000;
salary = salary / 2; //---salary is now 2500--
To double his pay, you can use the following:
double salary = 5000;
salary = salary * 2; //---salary is now 10000---
All these statements can be rewritten as follows using self-assignment operators:
salary += 1000; //---same as salary = salary + 1000---
salary -= 1000; //---same as salary = salary - 1000
salary /= 2; //---same as salary = salary / 2---
salary *= 2; //---same as salary = salary * 2---
A self-assignment operator alters its own value before assigning the altered value back to itself. In this example, +=
, -=
, /=
, and *=
are all self- assignment operators.
You can also use the modulus self-assignment operator like this:
int num = 5;
num %= 2; //---num is now 1---
The previous section described the use of the self-assignment operators. For example, to increase the value of a variable by 1, you would write the statement as follows:
int num = 5;
num += 1; //---num is now 6---
In C#, you can use the prefix or postfix operator to increment/decrement the value of a variable by 1. The preceding statement could be rewritten using the prefix operator like this:
++num;
Alternatively, it could also be rewritten using the postfix operator like this:
num++;
To decrement a variable, you can use either the prefix or postfix operator, like this:
--num;
//---or---
num--;
So what is the difference between the prefix and postfix operators? The following example makes it