Conditional Statements Flashcards
(15 cards)
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__________.
if-statement
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!");
if-statement
3) Can we put more than one instruction inside the if-statement?
Yes!
e.g.
if (price > 100)
{
1) System.out.println(“Special Promotion: We pay half your tax!”);
2) tax = tax * 0.5;
}
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.
false
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
}
two
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”);
}
}
System.out.println(“I’m sorry, but you failed”);
7) Can we put more than one test in the if ( ) braces?
Yes!
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
(water >= 7 && water <=15)
9) What does | | mean?
or
10) What does ! mean?
not
11) Nested if-else statements
Would you use a nested if-else statement if you had two conditions?
No, you would use a simple if-else statement for 2 conditions.
11) How many conditions would there be if we had;
if ( )
else if ( )
else if ( )
else ( )
4
if ( 1 )
else if ( 2 )
else if ( 3 )
else ( 4 )
12) Can you have an unlimited amount of nested if-else statements?
No, this will be difficult to read.
You would have to use a different form of control known as the ‘switch’ statement.
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”);
}
It is error checking, telling the student that they have not entered a valid character.
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"); }
Yes! You can!