This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C# is a for(;;) safe and what does it really do?
So i recently came across something ive never seen before..
for (; ; )
{
}
What is exactly happening when the feilds are left blank like that?
It's an infinite loop.
Somewhere inside there should be a break; statement, or possibly an exception thrown in order for control to pass beyond the loop.
You could also achieve the same thing (probably more obviously) by doing
while (true)
{
// do stuff
}
This is an infinite loop, almost equivalent to a while(true) loop.
The break condition is not there in between the two semicolons, therefore, it must be there somewhere in the loop body.
That's an infinite for loop.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have the code below (which actually is much longer than you see!)
foreach (SensorPair sensor in _sensorPairs)
{
sensorByte = (byte) sensor.Sensor;
if (!packet.Contains(sensorByte))
continue;
index = packet.IndexOf(sensorByte);
byteCount = sensor.ByteCount;
switch (byteCount)
{
case 1:
try
{
switch(sensor.ValueType)
{
case SensorValueType.Unsigned:
val = (int)packet[index + 1];
if (val > 255)
//*** WHAT DOES THIS CONTINUE DO?
continue;
else //rise the event
OnSensorReport();
break;
Does the continuekeywoard you see cause the foreach loop to start itterating next item or it just passes to the next case statement?
If it does not do anything with the foreach loop, how can I force the code to exit the switch and starts the next itteration in the foreach loop?
Yes, it continues the foreach loop.
It is always useful to consult the documentation ;-)
The continue statement passes control to the next iteration of the
enclosing while, do, for, or foreach statement in which it appears.
or—more comprehensive—the C# language specification:
8.9.2 The continue statement
The continue statement starts a new iteration of the nearest enclosing
while, do, for, or foreach statement.
The target of a continue statement is the end point of the embedded
statement of the nearest enclosing while, do, for, or foreach
statement. If a continue statement is not enclosed by a while, do,
for, or foreach statement, a compile-time error occurs.
When multiple while, do, for, or foreach statements are nested within
each other, a continue statement applies only to the innermost
statement. To transfer control across multiple nesting levels, a goto
statement (§8.9.3) must be used.
A continue statement cannot exit a finally block (§8.10). When a
continue statement occurs within a finally block, the target of the
continue statement must be within the same finally block; otherwise a
compile-time error occurs.
A continue statement is executed as follows:
If the continue statement exits one or more try blocks with
associated finally blocks, control is initially transferred to the
finally block of the innermost try statement. When and if control
reaches the end point of a finally block, control is transferred to
the finally block of the next enclosing try statement. This process is
repeated until the finally blocks of all intervening try statements
have been executed.
Control is transferred to the target of the continue
statement.
Because a continue statement unconditionally transfers control
elsewhere, the end point of a continue statement is never reachable.
This question already has answers here:
Which is the correct C# infinite loop, for (;;) or while (true)? [closed]
(20 answers)
Closed 2 years ago.
I find empty "for(;;)" statement in someone else code, and I can't figure out why it is used like this.
try
{
for (;;)
{
It is an infinite loop, just like: while (true)
This question already has answers here:
The best way to get a count of IEnumerable<T>
(11 answers)
Closed 2 years ago.
I want to be able to test a count of obj below but not able to do so.
when I mouse over obj it shows a count of 3
<IEnumerable<File> obj = await _mytester.cleanup(myitems)
why cant i do this?
if(obj.count > 0)
The liine above is giving me an error
You can't do that because count doesn't exist on IEnumerable and that's by design.
You can use IEnumerable.Count() like this:
if (obj.Count() > 0)
But that's horribly inefficient since you're really just checking if there's anything there.
You're better off doing this:
if (obj.Any())
Both of these are provided by LINQ extension methods.
It's very important to note that Count() is iterating through all elements and is nowhere near as efficient as something like ICollection.Count
This question already has answers here:
pre Decrement vs. post Decrement
(2 answers)
Closed 6 years ago.
I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--. When I changed it to index - 1 it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1. I was wondering if anyone could explain the reason behind this behavior.
Post-decrement operator returns the value before decrementing, so in your case the index will never be 0 and the function won't stop calling itself and you'll get a stack overflow. You want to write --index instead. It will return the value after decrementing then.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have the code below (which actually is much longer than you see!)
foreach (SensorPair sensor in _sensorPairs)
{
sensorByte = (byte) sensor.Sensor;
if (!packet.Contains(sensorByte))
continue;
index = packet.IndexOf(sensorByte);
byteCount = sensor.ByteCount;
switch (byteCount)
{
case 1:
try
{
switch(sensor.ValueType)
{
case SensorValueType.Unsigned:
val = (int)packet[index + 1];
if (val > 255)
//*** WHAT DOES THIS CONTINUE DO?
continue;
else //rise the event
OnSensorReport();
break;
Does the continuekeywoard you see cause the foreach loop to start itterating next item or it just passes to the next case statement?
If it does not do anything with the foreach loop, how can I force the code to exit the switch and starts the next itteration in the foreach loop?
Yes, it continues the foreach loop.
It is always useful to consult the documentation ;-)
The continue statement passes control to the next iteration of the
enclosing while, do, for, or foreach statement in which it appears.
or—more comprehensive—the C# language specification:
8.9.2 The continue statement
The continue statement starts a new iteration of the nearest enclosing
while, do, for, or foreach statement.
The target of a continue statement is the end point of the embedded
statement of the nearest enclosing while, do, for, or foreach
statement. If a continue statement is not enclosed by a while, do,
for, or foreach statement, a compile-time error occurs.
When multiple while, do, for, or foreach statements are nested within
each other, a continue statement applies only to the innermost
statement. To transfer control across multiple nesting levels, a goto
statement (§8.9.3) must be used.
A continue statement cannot exit a finally block (§8.10). When a
continue statement occurs within a finally block, the target of the
continue statement must be within the same finally block; otherwise a
compile-time error occurs.
A continue statement is executed as follows:
If the continue statement exits one or more try blocks with
associated finally blocks, control is initially transferred to the
finally block of the innermost try statement. When and if control
reaches the end point of a finally block, control is transferred to
the finally block of the next enclosing try statement. This process is
repeated until the finally blocks of all intervening try statements
have been executed.
Control is transferred to the target of the continue
statement.
Because a continue statement unconditionally transfers control
elsewhere, the end point of a continue statement is never reachable.