Related Topics

JAVA Programming
if-else
statement: This control statement is used to execute one block of code if a condition is true, and a different block of code if the condition is false.
Example:
switch
statement: This control statement is used to execute different blocks of code based on the value of an expression.
Example:
for
loop: This control statement is used to execute a set of statements repeatedly for a specified number of times.
Example:
while
loop: This control statement is used to execute a set of statements repeatedly as long as a certain condition is true.
Example:
do-while
loop: This control statement is used to execute a set of statements repeatedly at least once, and then as long as a certain condition is true.
Example:
break
statement: This control statement is used to terminate the execution of a loop or a switch statement.
Example:
continue
statement: This control statement is used to skip the current iteration of a loop and move on to the next iteration.
Example:
In this syntax, the if
keyword is used to begin the statement, followed by the condition to be evaluated in parentheses. If the condition is true, the code within the first set of curly braces will be executed. If the condition is false, the code within the second set of curly braces (the else
block) will be executed instead.
Here is an example:
In this example, the condition age >= 18
is evaluated. Since the value of age
is 18, which is greater than or equal to 18, the code within the first set of curly braces is executed, and the output “You are old enough to vote.” is printed to the console.
In this example, the switch
statement evaluates the value of the dayOfWeek
variable. Depending on the value, the code within the corresponding case
block will be executed. The break
statement is used to prevent execution from falling through to the next case
block.
If the value of dayOfWeek
does not match any of the case
values, the code within the default
block will be executed. In this example, if dayOfWeek
had a value other than 1 through 7, the value of dayName
would be set to “Invalid day”.
In the end, the program prints the value of dayName
, which is set based on the value of dayOfWeek
. In this example, the output would be “Today is Wednesday”.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.