16 Questions
What are the three main control structures in any programming language?
Sequence, Selection, Iteration
Which control structure is used to choose between alternative courses of action in a Java program?
if
In a selection structure, what follows the keyword 'if'?
An expression in parentheses
What is the purpose of the 'else' statement in the 'if/else' selection structure?
To execute when the 'if' condition is false
Which control structure is used for executing instructions repeatedly until a logical condition is satisfied?
Iteration
In a Java program, what determines the price of a cinema ticket based on the user's age?
'if' selection structure
What is the syntax of the if statement in Java?
if (expression) statement;
Which operator is used to check if a value is greater than or equal to another value in Java?
>=
What does the following code do in Java? if(length == width) { System.out.print("it is square"); }
Prints 'it is square' if length is equal to width
What would be the output of the following code in Java? if(hrsWked > 38) { System.out.print("Overtime hours: " +(hrsWked - 38)); }
Error
What is the purpose of indentation in the if statement?
To indicate the block of code that should be executed if the condition is true
What happens if the expression in an if statement is false?
The statements following the if are not executed
What does the following code do in Java? if(totalPrice >= 100) { disc = totalPrice *.1; totalPrice = totalPrice - disc; }
Applies a 10% discount if totalPrice is more than or equal to €100
What type of comparison does the operator '==' represent in Java?
Not equal to
What would be the output of the following code in Java? if(balance > 0) { fee = 0; }
'fee' is assigned a value of 0 if 'balance' is greater than 0
What does the operator '>=' represent in Java?
'≥' Greater than or equal to
Study Notes
Control Structures in Java
- The three main control structures in any programming language are selection, repetition, and sequence.
Selection Structures
- Selection structures are used to choose between alternative courses of action in a Java program.
- The keyword 'if' is used in selection structures, followed by a boolean expression.
- The 'else' statement is used in the 'if/else' selection structure to specify an alternative course of action when the condition is false.
If Statement in Java
- The syntax of the if statement in Java is
if (boolean expression) { statements }
. - The
>=
operator is used to check if a value is greater than or equal to another value in Java. - The
==
operator represents equality comparison in Java, checking if two values are equal.
If Statement Examples
-
if (length == width) { System.out.print("it is square"); }
: This code checks if the length is equal to the width, and prints "it is square" if true. -
if (hrsWked > 38) { System.out.print("Overtime hours: " + (hrsWked - 38)); }
: This code checks if the hours worked is greater than 38, and prints the overtime hours if true. -
if (totalPrice >= 100) { disc = totalPrice *.1; totalPrice = totalPrice - disc; }
: This code checks if the total price is greater than or equal to 100, and applies a 10% discount if true. -
if (balance > 0) { fee = 0; }
: This code checks if the balance is greater than 0, and sets the fee to 0 if true.
If Statement Rules
- Indentation is used to denote the block of code within the if statement.
- If the expression in an if statement is false, the code within the if block is skipped.
- The
>=
operator represents "greater than or equal to" in Java.
Test your knowledge of Java control structures with this quiz. Learn about sequence, selection, and repetition/iteration in Java programming, including if, if/else, switch, for, while, and do/while statements.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free