Skip to content Skip to navigation

Connexions

You are here: Home » Content » Programming in LabVIEW MathScript-Simple For Loop Exercises

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • NSF Partnership display tagshide tags

    This module is included inLens: NSF Partnership in Signal Processing
    By: Sidney BurrusAs a part of collection: "Introduction to LabVIEW MathScript"

    Click the "NSF Partnership" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • National Instruments display tagshide tags

    This module is included in aLens by: National InstrumentsAs a part of collection: "Introduction to LabVIEW MathScript"

    Comments:

    "This course provides a brief introduction to LabVIEW MathScript, the textual math componenet of LabVIEW. The modules for this course include typical syntax and programming methods commonly used […]"

    Click the "National Instruments" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

  • NI Signal Processing display tagshide tags

    This module is included inLens: Digital Signal Processing with NI LabVIEW and the National Instruments Platform
    By: Sam ShearmanAs a part of collection: "Introduction to LabVIEW MathScript"

    Comments:

    "Tutorial / Introduction to LabVIEW MathScript, a text-based component of National Instruments LabVIEW that allows you to run your .m file scripts in LabVIEW Virtual Instruments."

    Click the "NI Signal Processing" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Programming in LabVIEW MathScript-Simple For Loop Exercises

Module by: Anthony Antonacci, Darryl Morrell. E-mail the authors

Based on: Programming in MATLAB-Simple For Loop Exercises by Darryl Morrell

Summary: This module provides several simple exercises designed to test and increase your understanding of for loops in LabVIEW MathScript.

Some For Loop Exercises

Exercise 1

Loop Indices

How many times will this program print "Hello World"?


for a=0:50
    disp('Hello World')
end

Solution

The code 0:50 creates a vector of integers starting at 0 and going to 50; this vector has 51 elements. "Hello World" will be printed once for each element in the vector (51 times).

Exercise 2

Loop Indices II

How many times will this program print "Guten Tag Welt"?


for a=-1:-1:-50
    disp('Guten Tag Welt')
end

Solution

The code -1:-1:-50 creates a vector of integers starting at -1 and going backward to -50; this vector has 50 elements. "Guten Tag Welt" will be printed once for each element in the vector (50 times).

Exercise 3

Loop Indices III

How many times will this program print "Bonjour Monde"?


for a=-1:1:-50
    disp('Bonjour Monde')
end

Solution

The code -1:1:-50 creates an empty vector with no elements. Since this is an empty vector the following error occurs: "Error in function range at line 1. You cannot specify a step size of zero for a range."

Exercise 4

Nested Loops

How many times will this program print "Hola Mundo"?


for a=10:10:50
    for b=0:0.1:1
        disp('Hola Mundo')
    end
end

Solution

The outer loop (the loop with a) will be executed five times. Each time the outer loop is executed, the inner loop (the loop with b) will be executed eleven times, since 0:0.1:1 creates a vector with 11 elements. "Hola Mundo" will be printed 55 times.

Exercise 5

A tricky loop

What sequence of numbers will the following for loop print?


n = 10;
for j = 1:n
    n = n-1;
    j
end
Explain why this code does what it does.

Solution

In the first line, the value of n is set to 10. The code 1:n creates a vector of integers from 1 to 10. Each iteration through the loop sets j to the next element of this vector, so j will be sent to each value 1 through 10 in succession, and this sequence of values will be printed. Note that each time through the loop, the value of n is decreased by 1; the final value of n will be 0. Even though the value of n is changed in the loop, the number of iterations through the loop is not affected, because the vector of integers is computed once before the loop is executed and does not depend on subsequent values of n.

Exercise 6

Nested Loops II

What value will the following program print?


count = 0;
for d = 1:7
   for h = 1:24
      for m = 1:60
         for s = 1:60
            count = count + 1;
         end
      end
   end
end
count
What is a simpler way to achieve the same results?

Solution

The d loop will be executed seven times. In each iteration of the d loop, the h loop will be executed 24 times. In each iteration of the h loop, the m loop will be executed 60 times. In each iteration of the m loop, the s loop will be executed 60 times. So the variable count will be incremented 7 × 24 × 60 × 60 = 604800 7 24 60 60 604800 times.

A simpler way to achieve the same results is the command


7*24*60*60

Exercise 7

Multiple Hypotenuses

Figure 1: Sides of a right triangle.
Figure 1 (RT.png)
Consider the right triangle shown in Figure 1. Suppose you wish to find the length of the hypotenuse c c of this triangle for several combinations of side lengths a a and b b ; the specific combinations of a a and b b are given in Table 1. Write a LABVIEW MATHSCRIPT's program to do this.
Table 1: Side Lengths
a a b b
1 1
1 2
2 3
4 1
2 2

Solution

This solution was created by Heidi Zipperian:


a=[1 1 2 4 2]
b=[1 2 3 1 2]
for j=1:5
    c=sqrt(a(j)^2+b(j)^2)
end
A solution that does not use a for loop was also created by Heidi:

a=[1 1 2 4 2]
b=[1 2 3 1 2]
c=sqrt(a.^2+b.^2)

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks