Illustration of nested loops, MATLAB in Statistics

Illustration of Nested loops:

Running the script shows the output:

>> printstars

*****

*****

*****

The variable rows identifies the number of rows to print, and the variable columns identifies how many *'s to print in each row. There are 2 loop variables: i is the loop variable for the rows and j is the loop variable for columns. As the number of rows and columns are well-known, the for loops are used. There is one for loop to loop over the rows, and the other to print the needed number of *'s. The values of the loop variables are not used within the loops, but are used easily to iterate the right number of times. The first for loop identifies that the action will be repeated rows times. The action of this loop is to print *'s and then the newline character. Particularly, the action is to loop to print the columns *'s across on one line. The newline character is then printed after all five stars to move the cursor down for the next line.

The first for loop is known as the outer loop; the second for loop is known as the inner loop. Therefore, the outer loop is over the rows, and the inner loop over the columns. The outer loop should be over the rows as the program is printing a certain number of rows of output. For each row, a loop is essential to print the needed number of *'s; this is the inner for loop.

When this script is executed, initially the outer loop variable i is initialized to 1.And then action is executed. The action contains of the inner loop, and printing the newline character. Therefore, while the outer loop variable has value 1, the inner loop variable j iterates through its all values. As the value of columns is 5, the inner loop will print a * 5 times. Then, the newline character is printed and the outer loop variable i is incremented to 2. The action of outer loop is then executed once again, means that the inner loop will print five *'s, and then the newline character will be printed. This persists, and in all, the action of the outer loop will be executed rows times.

Note that the action of the outer loop consists of the two statements (that is the for loop and an fprintf statement). The action of the inner loop, though, is only a single statement.

The fprintf statement to print the newline character should be separate from the other fprintf statement which prints the *. If we simply had

fprintf('*\n')

As the action of inner loop, this would print a long column of 15 *'s, but not a box.

Posted Date: 10/19/2012 3:47:19 AM | Location : United States







Related Discussions:- Illustration of nested loops, Assignment Help, Ask Question on Illustration of nested loops, Get Answer, Expert's Help, Illustration of nested loops Discussions

Write discussion on Illustration of nested loops
Your posts are moderated
Related Questions
Operation on file: We concentrate first on the fgetl function that reads strings from the file one line at a time. The fgetl functions afford more control over how the data is

Index vector: The index vector is then used as the indices for original vector. To get grades vector in ascending order, the indices used would be grades (2), grades (5), and

Variable packages: Variable packages are now the vector of structures, hence each and every element in the vector is a structure. To show one element in the vector, an index i

Program of Persistent variables: The program below elaborates this. The script calls a function func1 that initializes a variable count to 0, then increment it, and print the

Working of Editor and debugger: Editor/Debugger, or commands can be typed from Command Window. For illustration, the dbstop command below will set a breakpoint in the fifth li

Example of customizing plots: As the other illustration of customizing plots, the pieces of a pie chart can be "exploded" from the rest. In this situation, the two vectors are

Debugging Techniques: Any error in the computer program is called a bug. This word is thought to date back to the 1940s, whenever a problem with an early computer was found to

Illustration of Indexing into vectors of structures: For illustration, the function to print the information from packages has been modified therefore in addition to the vecto

Illustration of Input in a for loop: In this illustration, the loop variable iv iterates through the values 1 through 3, therefore the action is repeated three times. The acti

Illustration of Mode When there is more than one value with similar (highest) frequency, then the smaller value is mode. In the case below, as 3 and 8 appear twice in the vecto