Control Logic Using Iterative Statements

You need to use iterative statements when a section of code needs to execute more than once. For example, after creating an array, you may use an iterative statement to work with every member of that array. There are multiple ways to implement an iterative statement. Your choice is based on the requirements of the logic you are implementing.

There are four iteration statements to choose from: while, do, for, and foreach. With the chosen statement, you will need to create a Boolean expression that is evaluated each time the loop is executed. After choosing a statement, you will write code that is embedded within the statement.

CONTROL LOGIC USING ITERATIVE STATEMENTS

Each iterative statement handles a loop differently. The while statement will execute 0 or more times. The do statement will execute 1 or more times. The foreach statement is used for enumerating elements in a collection. The for statement has more structure than the while and do statements. You have three optional parameters used to operate loops, which are an initializer, condition, and iterator.

Within all the iterative statements, the embedded code can use either a break or continue statement. The break statement will transfer the control to the end of the iterative statement and stop the iteration. The continue statement will transfer control to the end of the iterative statement and then perform another iteration.

CONTROL LOGIC USING ITERATIVE STATEMENTS

D Open your text editor.

—0 Type using System; to import the System namespace and press Enter.

< Type the name of the class you want to create and press Enter.

H Between the curly braces, create the Main function.

-0 Declare variables for use by the case statement and the if-then logic.

Q Create a case statement.

D Open your text editor.

—0 Type using System; to import the System namespace and press Enter.

< Type the name of the class you want to create and press Enter.

—Q Type { }, placing the opening and closing curly braces on separate lines, to set off the body of the class.

H Between the curly braces, create the Main function.

-0 Declare variables for use by the case statement and the if-then logic.

Q Create a case statement.

Was this article helpful?

0 0

Post a comment