Final exams pt 3 Flashcards

1
Q

Describe the function of an if statement

A

An if statement is used to execute code when a condition or conditions are true. This creates branches in the code where different sections are executed under different conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write an if statement that will:
print a message when a value is more than 6

A

if (value > 6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write an if statement that will:
Add 2 to a variable when a value equals 800

A

if (value == 800)
var += 2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write an if statement that will:
Multiply a value by -1 when a value equals 0 or 500

A

if (value == 0 || value == 500)
var = var * -1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write an if statement that will:
Print a message when one value is ‘y’ and another is less than 25

A

if (first == ’y’ && second < 25)
println (“The message”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write an if statement that will:
Display a different message if a value is over 400, between 300 and 400, between 100 and 300, or less than 100

A

if (value > 400)
println (“Message 1”);
else if (value < 400 && value >= 300)
println (“Message 2”);
else if (value < 300 && value >= 100)
println (“Message 3”);
else
println (“Message 4”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly