Combining Loops and Conditionals
Combining loops with conditional statements allows you to process data repeatedly while making decisions at each step. This approach is essential for solving problems such as filtering values, searching for specific data, or performing actions only when certain criteria are met. By using both constructs together, you can build flexible and powerful logic that adapts as your program runs.
Practical Example – Filtering Values
You often need to process only specific items from a collection based on certain criteria. For instance, you might want to print only the even numbers from an array of integers. This requires combining a loop to go through each element and a conditional statement to check if the value meets your criteria.
Task
Write a program that:
- Goes through each number in the array;
- Checks if the number is even;
- Prints only the even numbers to the console.
Use a for loop and an if statement to accomplish this. Print each even number on a new line.
Main.java
1234567891011121314package com.example; public class Main { public static void main(String[] args) { int[] numbers = {3, 8, 15, 22, 7, 10, 13}; System.out.println("Even numbers in the array:"); for (int number : numbers) { if (number % 2 == 0) { System.out.println(number); } } } }
- An array called
numbersholds several integer values; - The program first prints the message "Even numbers in the array:" to indicate the output;
- A for-each loop goes through each element in the array;
- Inside the loop, an if statement checks whether the current number is divisible by 2 (
number % 2 == 0); - If the condition is true, the number is printed. If not, the loop moves on to the next element.
Mastering loops and conditionals together gives you powerful control over how your Java programs handle real-world problems.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 9.09
Combining Loops and Conditionals
Pyyhkäise näyttääksesi valikon
Combining loops with conditional statements allows you to process data repeatedly while making decisions at each step. This approach is essential for solving problems such as filtering values, searching for specific data, or performing actions only when certain criteria are met. By using both constructs together, you can build flexible and powerful logic that adapts as your program runs.
Practical Example – Filtering Values
You often need to process only specific items from a collection based on certain criteria. For instance, you might want to print only the even numbers from an array of integers. This requires combining a loop to go through each element and a conditional statement to check if the value meets your criteria.
Task
Write a program that:
- Goes through each number in the array;
- Checks if the number is even;
- Prints only the even numbers to the console.
Use a for loop and an if statement to accomplish this. Print each even number on a new line.
Main.java
1234567891011121314package com.example; public class Main { public static void main(String[] args) { int[] numbers = {3, 8, 15, 22, 7, 10, 13}; System.out.println("Even numbers in the array:"); for (int number : numbers) { if (number % 2 == 0) { System.out.println(number); } } } }
- An array called
numbersholds several integer values; - The program first prints the message "Even numbers in the array:" to indicate the output;
- A for-each loop goes through each element in the array;
- Inside the loop, an if statement checks whether the current number is divisible by 2 (
number % 2 == 0); - If the condition is true, the number is printed. If not, the loop moves on to the next element.
Mastering loops and conditionals together gives you powerful control over how your Java programs handle real-world problems.
Kiitos palautteestasi!