I’m Learning C# – Part 5

In the precious post, we had used if, else if and else in the code for for loop.  What it does is it states under this condition, you execute the following statements.

Another similar statement is the switch.

We are using the same code used for fibo nacci series

static void Main(string[] args)
{

Console.WriteLine(“enter the number of Fibonacci numbers required”);
int x = Convert.ToInt32(Console.ReadLine());
switch (x)
{
case 0: Console.WriteLine(“no numbers are printed”);
break;
case 1: Console.WriteLine(“The number is 0”); ;
break;
case 2: Console.WriteLine(“The numbers are 0 1 “);
break;
case 3: Console.Write(“The numbers are 0 1 1 “);
break;
default:
{
int y = 1;
int z = 1;
int sum = 0;
Console.Write(“The numbers are 0 1 1 “);
for (; x > 3; x -= 1)
{
sum = y + z;
Console.Write(” {0} “, sum);
z = y;
y = sum;

}
break;
}
}

}

We need to know what break is. Break is used to break free from the current execution statement and move on to the next. So if the break statement is found inside a loop, the execution inside the loop gets terminated and any remaining code outside the loop is executed next.

Another important terminology is continue. Continue skips just the current iteration of a loop but resumes with the rest of the loop iteration.

Now some logical operators:

&& – AND  (LHS and RHS should be True for the condition to be True)

|| – OR (either LHS or RHS should be True for the condition to be True)

! – NOT (works on single operand; if operand is false, the condition is True and vice versa)

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