Introduction
There are several ways to add looping logic in your application, and depending on the context, each provides a nuanced set of features that have both pros and cons.
Suppose you're about to start working on an application that processes string and numeric data using single dimensional and multidimensional arrays. After an initial review, you realize that foreach
statements do not support looping logic that will be required in many cases. You'll need another approach for iterating through multidimensional arrays, and for situations where foreach
loops don't provide the level of iteration control that's needed. You need to gain experience using for
statements if you're going to succeed on this project.
In this module, you'll begin by writing for
statements that iterate a specific number of times. After implementing a basic for
statement, you'll learn how to implement for
statements that iterate backwards through an array, skip over array elements during an iteration, or process only specified elements of an array (by changing the for
statement's initializer, condition, and iterator).
By the end of this module, you'll be able to use for
statements to implement looping logic when foreach
statements don't support the scenario.
Learning objectives
In this module, you will:
- Use a
for
statement to loop through a block of code. - Examine the
for
statement syntax that enables you to control the iteration pattern.
Prerequisites
- Experience with the
foreach
iteration statement. - Experience working with variables.