Compound assignment and iteration operations in C#

✓ Compound assignment operators

answer = answer + 42;  equals to answer+= 42;
answer = answer - 42;  equals to answer-= 42;
answer = answer * 42;  equals to answer*= 42;
answer = answer / 42;  equals to answer/= 42;
answer = answer % 42;  equals to answer% = 42;

✓ While statements

int i = 0;
while (i < 10)
{
    Console.WriteLine(i);
    i++;
}
string line = reader.ReadLine();
string text;
while (line != null)
{
    text += line + '\n';
    line = reader.ReadLine();
}

✓ For statements

for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}
string text;
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
    text += line + '\n';
}

✓ do statements

int i = 0;
do
{
    Console.WriteLine(i);
    i++;
}
while (i < 10);

✓ The break and continue statements
Break statement can be used to jump out of switch or iteration statements. On the otherhad the continue statement causes the program to perform the next iteration of the loop immediately.

int i = 0;
while (true) 
{ 
    Console.WriteLine("continue " + i);
    i++; 
    if (i < 10) 
        continue; 
    else 
        break;
}

This block print the values from 0 to 9.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s