Switch and loops in Java programming?

 

Hello everyone Today we will read about switch and loop statement in java.

If you haven't seen Java last post, read that too first. here is the link click here






What is switch statement in java?

In Java, a switch statement is used to execute different code blocks based on the value of a variable or expression. It is similar to a series of if statements but can be more efficient and easier to read in some cases.

Syntax for switch statement

switch (expression) {

case value1:
    // code to execute if expression equals value1
    break;
    case value2:
    // code to execute if expression equals value2
    break;
    // more cases can be added here
    default:
    // code to execute if expression does not match any case
    break;
}

Here switch is java keyword. Expression is a variable whose value will decide which case statement will run.

Case is also a keyword. Here we will put different values of variables.

Default is a keyword which is used when no case value matches with variable value.

what is a good example of switch statement in java?

switch.png

Output:

Here days, case 3 will run and the rest of the cases will not. Thus dayName value will be Wednesday.

So in output, Wednesday will be printed.OfWeek=3

One more thing here we have used a break statements. Why?

What is the use of a break statement?

In Java, the break statement is used to exit a loop or switch statement. When a break statement is encountered inside a loop or switch statement, control is immediately transferred to the statement immediately following the loop or switch block.

In this example, the switch statement sets the dayName variable based on the value of dayOfWeek. When dayOfWeek is 3, the case 3 block is executed and sets dayName to "Wednesday". The break statement then exits the switch statement, and control is transferred to the statement immediately following the switch block, which prints "Today is Wednesday". If the break statements were not included, the switch statement would continue executing the code in the subsequent case labels and will print the rest of the cases as well.


what are loops in java?

In Java, loops are used to execute a block of code repeatedly while a certain condition is true.

There are three types of loops in Java:

  • for loop:A for loop is used to iterate over a range of values. It has three parts: initialization, condition, and iteration. Here's an example:
for-loop.png

In this example, the loop initializes the i variable to 0, checks if i is less than 10, and increments i by 1 after each iteration. The loop continues to execute as long as the condition (i < 10) is true.

  • while loop: A while loop is used to run a piece of code continuously while a condition is true. It has only one part: the condition. Here's an example:A while loop is used to repeatedly run a block of code while a condition is true. It just has one component: the condition. Here's an illustration:
while-loop.png

In this example, the loop checks if i is less than 10 before each iteration. The loop continues to execute as long as the condition (i < 10) is true. I variable is incremented by 1 after each iteration.

  • do-while loop: A do-while loop is similar to a while loop, but it guarantees that the loop body is executed at least once before checking the condition. Here's an example.
do-while-loop.png

In this example, the loop initializes the i variable to 0 and executes the loop body at least once. The loop checks if i is less than 10 after each iteration. The loop continues to execute as long as the condition (i < 10) is true.

All three types of loops can be used to execute a block of code repeatedly, but they differ in how the condition is checked and when the loop body is executed. It's important to choose the appropriate loop type for the task at hand to ensure efficient and correct code execution.

Code to find factorial of a number:
example.png

Output:

output.png


what is Java continue statement?

In Java, the continue statement is used to skip the current iteration of a loop and continue with the next iteration. When the continue statement is encountered within a loop, the remaining statements in the loop body are skipped and the loop control passes to the next iteration.

Here's an example that uses the continue statement to skip even numbers in a loop:

continue-statement.png

Output:

output.png

As you can see, the even numbers (2, 4, 6, 8, 10) are skipped due to the continued statement.

So that’s it for today guys if you want to learn more about javascript follow and stay tuned with guerillateck.com

Thanks for learning give us your feedback?



Question for practice:

1. find the sum of two 3*3 matrices.

2. Find compound interest without using direct formula?


Thanks for Reading…


No comments:

Post a Comment