10+ Continue 使い方 References
What is Continue?
Continue is a popular programming keyword used to resume the execution of a loop or a function from where it was paused. It allows the program to skip a certain part of the code and continue executing from the next line. In simpler terms, it helps the program to keep running without getting stuck in a loop.
Using Continue in Loops
The most common use of continue is in loops. It helps to skip a certain iteration of the loop and continue with the next one. For example, let's say you want to print all the even numbers from 1 to 10. You can use continue to skip the odd numbers and print only the even ones:
for (int i = 1; i <= 10; i++) { if (i % 2 != 0) { continue; } System.out.println(i); }
This code will print:
2 4 6 8 10
Using Continue in Functions
Continue can also be used in functions to skip a certain part of the code and continue with the rest. For example, let's say you have a function that checks if a number is prime:
public static boolean isPrime(int n) { for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; }
This function checks if a number is divisible by any number between 2 and n/2. If it is, it returns false, indicating that the number is not prime. However, if the number is not divisible by any of these numbers, the function returns true, indicating that the number is prime. We can use continue to skip the even numbers, as they are not prime:
public static boolean isPrime(int n) { if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= n / 2; i += 2) { if (n % i == 0) { return false; } } return true; }
This code will skip all even numbers and only check the odd ones. This will make the function faster and more efficient.
Using Continue in Switch Statements
Continue can also be used in switch statements to skip a certain case and continue with the rest. For example, let's say you have a switch statement that checks the day of the week:
switch (day) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); break; }
If the day is 1, the code will print "Sunday". If the day is 2, the code will print "Monday", and so on. However, what if you want to skip Saturday and Sunday? You can use continue to do that:
switch (day) { case 1: System.out.println("Sunday"); continue; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; case 7: System.out.println("Saturday"); continue; }
This code will skip Sunday and Saturday and print only the weekdays.
Conclusion
Continue is a powerful keyword that can help you write more efficient and faster code. It allows you to skip a certain part of the code and continue with the rest. Whether you are using it in loops, functions, or switch statements, continue can help you write cleaner and more readable code. So, the next time you are stuck in a loop, remember to use continue!
0 Response to "10+ Continue 使い方 References"
Posting Komentar