Illustration of for loop, MATLAB Programming

illustration of for loop:

illustration, to print a column of numbers from 1 to 5:

for i = 1:5

fprintf('%d\n',i)

end

 

This loop can be entered in the Command Window, though like if and switch statements; loops will make more sense in the scripts and functions. In the Command Window, the outcome would appear after the for loop:

>> for i = 1:5

fprintf('%d\n',i)

end

1

2

3

4

5

What the for statement accomplished was to print the value of i and then the newline character for each value of i, from 1 through 5 in steps of 1. The first thing which happens is that i is initialized to have the value 1. Then, the action of loop is executed, that is the fprintf statement which prints the value of i (1), and then the newline character to move the cursor down. Then, i is incremented having the value of 2. Later, the action of the loop is executed, that prints 2 and the newline. Then, i is incremented to 3 and which is printed, then i is incremented to 4 and which is printed, and then finally i is incremented to 5 and which is printed. The final value of i is 5; this value can be used once the loop has completed.

 

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







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

Write discussion on Illustration of for loop
Your posts are moderated
Related Questions

Non-Ideal Gas (van der Waals equation):  An equation of state for a non-ideal which is commonly used is the van der Waals equation for 1 mol of gas  P = (R*T)/(V-b) - a/(V^2)

Illustration of script: For illustration, we will now generate a script known as script1.m which calculates area of the circle. It assigns a value for the radius, and then com

Problem: A function is given by Y= 2x3 + 3 x2 -12x + 5 a determine the finite values of x at which any local maximum of minimum occur and determine the corresponding val

Types: Each and every expression, or variable, has a type related with it. The MATLAB supports numerous types of values that are known as classes. A class is necessarily a com

RECOGNIZER and TRANSLATOR: i want to recognized devanagari word using matlab. I have done it using template matching. but i am not get devanagari word at the output. But get speci

A fence enclosure consists of a rectangle of length L and width 2 R , and a semicircle of radius R , as shown in Figure 1. The enclosure is to be built to have an area of 1600 m

Vectors and Matrices: The Vectors and matrices are used to store sets of values, all of which are similar type. A vector can either be a column vector or a row vector. The mat

Output Statements: disp and fprintf: The Output statements display strings and the answers of expressions and can permit for formatting or customizing how they are exhibited.

How do you write a programme that asks the user for a number between 1 and 1000 and calculates the sum of its digits?