目次
Write the code according to the following conditions
Declare a variable numbers of type List and store 5 values: 10, 15, 20, 25, and 30.
For each element, if the value is greater than or equal to 20, output the value to the console using System.debug().
Solution Example
// Declare a variable numbers of type List and store the value
List<Integer> numbers = new List<Integer>{10, 15, 20, 25, 30};
// Loop processing for each element
for (Integer num : numbers) {
// output to console if value is greater than or equal to 20
if (num >= 20) {
System.debug('Value is 20 or above: ' + num);
}
}
Explanation
In this exercise, you will write code that does the following
- Create a list (of type
List
) with integer values and store 5 values. - Check each value in that list and display only those values greater than 20 on the console.
We will now explain the code step by step.
1. Creating a list ( List
)
List<Integer> numbers = new List<Integer>{10, 15, 20, 25, 30};
- List
: List stands
for “list” andInteger stands
for integer data type. In other words, this list can only contain integer values.
In Apex, a list can be thought of as “a box for holding multiple data in a single variable.
TheInteger
type is a data type that handles integers (numbers without decimal points). - new List
: When creating a new list, use new to
indicate “create a new list. - {10, 15, 20, 25, 30}: The numbers in
{}
are the data that will go into the list. The list namednumbers
will now contain five integers.
2. Extracting the contents of the list in a loop
Next, we check each of the five values in the list in turn. To do this, we use “looping”.
for (Integer num : numbers) {
// the process inside the loop is written here
}
- for (Integer num : numbers): This is called an “extended for loop.
It takes a value from a listcalled numbers
one by one and assigns the value to a variablecalled num.
For example, the first loop will place10
innum
, the second loop will place15
, and so on, until all elements in the list are placed innum in
order. - Processing inside {}: Each time the loop turns, processing inside {} is executed. In this case, we will determine if
num is
20 or more.
3. Checking if num is 20 or more with a conditional branch ( if
statement)
The following is a conditional branch (if statement) to check if num is greater than or equal to 20.
if (num >= 20) {
System.debug('Value is 20 or above: ' num);
}
- if (num >= 20): The
if
statement is used when you want to use an “if” condition.
In this case, the condition is “ifnum is
20 or above.>= is
the operator for “greater than or equal to”.- If
num is
20, 25, or 30, this condition istrue
. - If num is less than 20, such as 10 or 15, the condition is
false
.
- If
- System.debug(‘Value is 20 or above:’ num): If
num is
20 or above,use System.debug to
display a message on the console.
The string'Value is 20 or above:'
is concatenated with the value ofnum
and output. For example, ifnum is
20, the message"Value is 20 or above: 20"
is displayed on the console.
Overall code flow
- Create a list
numbers
to store 5 numbers (10, 15, 20, 25, 30). - In a loop, retrieve each number in the list in order.
- Checks with an
if
statement whether each number is greater than or equal to 20. - If 20 or more, the value is displayed on the console
with System.debug
.
Output Result
After executing this code, the console will display the following (only numbers above 20
will be displayed)
Value is 20 or above: 20
Value is 20 or above: 25
Value is 20 or above: 30
Summary
- You have learned how to create a list and store integer values in it.
- We learned how to use a
for
loop to retrieve and process the values in a list one by one. - You learned how to use
if
statements to process only those values that meet certain conditions.
Once you understand this flow, the next step is to easily handle complex conditions and processing.