본문 바로가기
Programming/Java

5 If & Switch (feat. break, continue)

by Dowon Kang 2024. 1. 7.

1) if Statement

The if statement is used for making decisions based on a condition. It allows you to execute a block of code if a specified condition evaluates to true. 

 



2) Switch Statement

The switch statement is used to select one of many code blocks to be executed based on the value of an expression. It provides a more concise way to handle multiple possible conditions compared to a series of if-else if statements. 

 

 


 

3) break Statement

The break statement is used to exit a loop prematurely.

In this example, the loop will print numbers from 0 to 4, and when i becomes 5, the break statement is encountered, and the loop is terminated.

 


 

4) Continue Statement


The continue statement is used to skip the rest of the code inside a loop for the current iteration and proceed to the next iteration of the loop.

In this example, the loop will print only odd numbers (1, 3, 5, 7, 9) because the continue statement skips the System.out.println(i) statement for even values of i.

 

'Programming > Java' 카테고리의 다른 글

7 while loop (feat. do-while)  (0) 2024.01.07
6 for loop (feat. Nested, foreach)  (0) 2024.01.07
4 Operation (연산)  (1) 2023.12.30
3 Casting  (0) 2023.12.29
2 Variable, Constant and Primitive Type in Java (Feat. Literal)  (0) 2023.12.29

댓글