Skip to content Skip to navigation

Connexions

You are here: Home » Content » Programming in LabVIEW MathScript-Simple If Statement 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 If Statement Exercises

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

Based on: Programming in MATLAB-Simple If Statement Exercises by Darryl Morrell

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

Some If Statement Exercises

Exercise 1

What will the following LABVIEW MATHSCRIPT code print?


a = 10;
if a ~= 0
    disp('a is not equal to 0')
end

Solution

' a is not equal to 0'

Exercise 2

What will the following LABVIEW MATHSCRIPT code print?


a = 10;
if a > 0
    disp('a is positive')
else
    disp('a is not positive')
end

Solution

' a is positive'

Exercise 3

What will the following LABVIEW MATHSCRIPT code print?


a = 5;
b = 3;
c = 2;
if a < b*c
    disp('Hello world')
else
    disp('Goodbye world')
end

Solution

b*c gives a value of 6, and 5 < 6, so this code will print 'Hello world'.

Exercise 4

Suppose the code in Exercise 2 is modified by adding parentheses around a > 0. What will it print?


a = 10;
if (a > 0)
    disp('a is positive')
else
    disp('a is not positive')
end

Solution

The parentheses around the relational expression a > 0 will not change its validity, so this code will print 'a is positive'.

Exercise 5

Suppose the code in Exercise 3 is modified by adding the parentheses shown below. What will it print?


a = 5;
b = 3;
c = 2;
if (a < b)*c
    disp('Hello world')
else
    disp('Goodbye world')
end

Solution

The parentheses in this expression change its meaning completely. First, a < b is evaluated, and since it is false for the given values of a and b, it evaluates to zero. The zero is than multiplied by c, giving a value of zero which is interpreted by LABVIEW MATHSCRIPT as false. So this code prints 'Goodbye world'.

Exercise 6

What will the following LABVIEW MATHSCRIPT code print?


p1 = 3.14;
p2 = 3.14159;
if p1 == p2
    disp('p1 and p2 are equal')
else
    disp('p1 and p2 are not equal')
end

Solution

' p1 and p2 are not equal'

Exercise 7

What will the following LABVIEW MATHSCRIPT code print?


a = 5;
b = 10;
if a = b
    disp('a and b are equal')
else
    disp('a and b are not equal')
end

Solution

This code will generate an error message, since a = b assigns the value of b to a. To check if a and b are equal, use a == b.

Exercise 8

For what values of the variable a will the following LABVIEW MATHSCRIPT code print 'Hello world'?


if ~ a == 0
    disp('Hello world')
else
    disp('Goodbye world')
end

Solution

Any value that is not zero.

Exercise 9

For what values of the variable a will the following LABVIEW MATHSCRIPT code print 'Hello world'?


if a >= 0 && a < 7
    disp('Hello world')
else
    disp('Goodbye world')
end

Solution

Any value greater than or equal to 0 and less than 7.

Exercise 10

For what values of the variable a will the following LABVIEW MATHSCRIPT code print 'Hello world'?


if a < 3 || a > 10
    disp('Hello world')
else
    disp('Goodbye world')
end

Solution

Any value less than 3 or greater than 10.

Exercise 11

For what values of the variable a will the following LABVIEW MATHSCRIPT code print 'Hello world'?


if a < 7 || a >= 3
    disp('Hello world')
else
    disp('Goodbye world')
end

Solution

Every value of a will print 'Hello world'.

Exercise 12

Write an if statement that will print 'a is very close to zero' if the value of the variable a is between -0.01 and 0.01.

Solution


        
if a >= -0.01 && a <= 0.01
    disp('a is very close to zero')
end

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

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