Conditional Statements Flashcards

(15 cards)

1
Q

1) Selection

Selection is a method of program control in which a choice can be made about which instructions to execute.

Let’s say we ask a user to input their age, and we have three choices of response, e.g.

“You are young.” “You are middle-aged.” “You are old.
(for people 1-45) (for people 45-65) (for people 65+)

to make that choice(selection) we need to use an i___ s__________.

A

if-statement

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

2) Making a single choice

use an i____ s___________ to make a single choice in a program;

e.g. System.out,println(“How old are you?);
age = keyboard.nextInt( );

if (age < 13) {
System.out.println(“Hello Junior’);
}

         System.out.println("Enjoy your ride!");
A

if-statement

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

3) Can we put more than one instruction inside the if-statement?

A

Yes!

e.g.

if (price > 100)
{
1) System.out.println(“Special Promotion: We pay half your tax!”);
2) tax = tax * 0.5;
}

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

4) Single-Branched Selection

The if-statement made one of two choices before continuing with the remaining instructions in the program:

• execute the conditional instructions, or
• do not execute the conditional instructions.

so, if the test gives a f__________ result, Java does nothing and continues on with the rest of the program.

A

false

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

5) Double-Branched selection

The if…else statement is often referred to as a double-branched selection as there are t____
alternative groups of instructions

e.g.
if ( /* test goes here */ )
{
// instruction(s) if test is true go here
}
else
{
// instruction(s) if test is false go here
}

A

two

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

6) What would be the output if the following code was run and the user input 38?

int mark;
Scanner keyboard = new Scanner(System.in);
System.out.println(“What exam mark did you get? “);
mark = keyboard.nextInt();
if (mark >= 40)
{
System.out.println(“Congratulations, you passed”);
}
else
{
System.out.println(“I’m sorry, but you failed”);
}

System.out.println(“Good luck with your other exams”);
}
}

A

System.out.println(“I’m sorry, but you failed”);

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

7) Can we put more than one test in the if ( ) braces?

A

Yes!

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

8) Logical Operators

The water temperature needs to be higher than 7 degrees but lower than 15.
Complete the code in the if braces to demonstrate this.

int water;
if ( )
{
System.out.println(“The water temperature is correct.”
}
else
{
System.out.println(“The water temperature is not correct.”);
}

//Program continues

A

(water >= 7 && water <=15)

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

9) What does | | mean?

A

or

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

10) What does ! mean?

A

not

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

11) Nested if-else statements

Would you use a nested if-else statement if you had two conditions?

A

No, you would use a simple if-else statement for 2 conditions.

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

11) How many conditions would there be if we had;

if ( )
else if ( )
else if ( )
else ( )

A

4

if ( 1 )
else if ( 2 )
else if ( 3 )
else ( 4 )

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

12) Can you have an unlimited amount of nested if-else statements?

A

No, this will be difficult to read.

You would have to use a different form of control known as the ‘switch’ statement.

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

13) What is the ‘else’ statement doing in this code?

if (group == ‘A’)
{
System.out.println(“10.00 a.m”);
}
else if (group == ‘B’)
{
System.out.println(“1.00 p.m”);
}
else if(group == ‘C’)
{
System.out.println(“11.00 a.m”);
}
else
{
System.out.println(“No such group”);
}

A

It is error checking, telling the student that they have not entered a valid character.

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

14) Nested if-statements

Can you nest an if-statement within an if-statement?

e.g.

if(x>5)
{
if(y>5)
{
System.out.println(“x and y are greater than 5”);

  } } else  System.out.println("x is <=5"); }
A

Yes! You can!

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