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
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
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
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
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
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
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
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
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
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
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
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.The While Loop
The while loop is similar to the for loop in that it allows the repeated execution of LABVIEW MATHSCRIPT statements. Unlike the for loop, the number of times that the LABVIEW MATHSCRIPT statements in the body of the loop are executed can depend on variable values that are computed in the loop. The syntax of the while loop has the following form:
while expression
% LABVIEW MATHSCRIPT command 1
% LABVIEW MATHSCRIPT command 2
% More commands to execute repeatedly until expression is not true
end
expression
is a logical expression that is either true or false. (Information about logical expressions is available in Programming in LABVIEW MATHSCRIPT-Logical Expressions.) For example, consider the following while loop:
n = 1
while n < 3
n = n+1
end
n =
1
n =
2
n =
3
Note that in all of this example, the LABVIEW MATHSCRIPT commands inside the while loop are indented relative to the
while
and end
statements. This is not required by LABVIEW MATHSCRIPT but is common practice and makes the code much more readable.
No comments:
Post a Comment