JAVA – codewindow.in

Related Topics

JAVA Programming

  1. 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:

  1. switch statement: This control statement is used to execute different blocks of code based on the value of an expression.

Example:

  1. for loop: This control statement is used to execute a set of statements repeatedly for a specified number of times.

Example:

  1. while loop: This control statement is used to execute a set of statements repeatedly as long as a certain condition is true.

Example:

  1. 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:

  1. break statement: This control statement is used to terminate the execution of a loop or a switch statement.

Example:

  1. 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.

Categories