Conditional iteration
-
Sometimes, you want to write a loop that will skip some iterations if a certain condition is met
-
For example, you may be writing a
for
loop that iterates through an array of numbers, but you only want to use even numbers from the array -
One way to accomplish this is to nest an
if
statement inside thefor
loop that checks for the desired condition. For example:
Since the entire body of the for
loop is contained within an if
statement, the iterations where myArray[i]
is odd will skip the body
and do nothing.
Skipping iterations with continue
-
The
continue
keyword provides another way to conditionally skip an iteration of a loop -
When the computer encounters a
continue;
statement, it immediately returns to the beginning of the current loop, skipping the rest of the loop body -
Then it executes the update statement (if the loop is a
for
loop) and checks the loop condition again -
A
continue;
statement inside anif
statement will end the current iteration only if that condition is true -
For example, this code will skip the odd numbers in
myArray
and use only the even numbers:
If myArray[i]
is odd, the computer will execute the continue
statement and immediately start the next iteration of the loop. This
means that the rest of the loop body (the other two statements) only
gets executed if myArray[i]
is even.
- Using a
continue
statement instead of putting the entire body within anif
statement can reduce the amount of indentation in your code, and it can sometimes make your code’s logic clearer.
Loops with multiple end conditions
-
More advanced loops may have multiple conditions that affect whether the loop should continue
-
Attempting to combine all of these conditions in the loop condition (i.e. the expression after
while
) can make the loop more complicated -
For example, consider a loop that processes user input, which should end either when a sentinel value is encountered or when the input is invalid. This loop ends if the user enters a negative number (the sentinel value) or a non-numeric string:
- The condition
success && userNum >= 0
is true if the user entered a valid number that was not negative - In order to write this condition, we needed to declare the extra
variable
success
to keep track of the result ofint.TryParse
- We cannot use the condition
userNum > 0
, hoping to take advantage of the fact that ifTryParse
fails it assigns itsout
parameter the value 0, because 0 is a valid input the user could give
Ending the loop with break
-
The
break
keyword provides another way to write an additional end condition -
When the computer encounters a
break;
statement, it immediately ends the loop and proceeds to the next statement after the loop body -
This is the same
break
keyword we used inswitch
statements -
In both cases it has the same meaning: stop execution here and skip to the end of this code block (the ending
}
for theswitch
or the loop) -
Using a
break
statement inside anif-else
statement, we can rewrite the previouswhile
loop so that the variablesuccess
is not needed:
-
Inside the body of the loop, the return value of
TryParse
can be used directly in anif
statement instead of assigning it to thesuccess
variable -
If
TryParse
fails, thebreak
statement will end the loop, so there is no need to addsuccess
to thewhile
condition -
We can also use the
break
statement with afor
loop, if there are some cases where the loop should end before the counter reaches its last value -
For example, imagine that our program is given an
int
array that a user partially filled with numbers, and we need to find their product. The “unused” entries at the end of the array are all 0 (the default value ofint
), so thefor
loop needs to stop before the end of the array if it encounters a 0. Abreak
statement can accomplish this:
-
If
myArray[i]
is 0, the loop stops before it can multiply the product by 0 -
If all of the array entries are nonzero, though, the loop continues until
i
is equal tomyArray.Length
-
Note that in this example, we access each array element once and do not modify them, so we could also write it with a
foreach
loop: