Salesforce関連の記事を書いています。

  1. Apex練習問題

Exercise 4: Combining conditional branches and loops

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

  1. Create a list (of type List ) with integer values and store 5 values.
  2. 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” and Integer 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.
    The Integer 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 named numbers 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 list called numbers one by one and assigns the value to a variable called num.
    For example, the first loop will place 10 in num, the second loop will place 15, and so on, until all elements in the list are placed in num 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 “if num is 20 or above. >= is the operator for “greater than or equal to”.
    • If num is 20, 25, or 30, this condition is true.
    • If num is less than 20, such as 10 or 15, the condition is false.
  • 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 of num and output. For example, if num is 20, the message "Value is 20 or above: 20" is displayed on the console.

Overall code flow

  1. Create a list numbers to store 5 numbers (10, 15, 20, 25, 30).
  2. In a loop, retrieve each number in the list in order.
  3. Checks with an if statement whether each number is greater than or equal to 20.
  4. 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.

Apex練習問題 recent post

  1. Exercise 3: Iteration and List Manipulation

  2. Exercise 1: Variable Declaration and Manipula…

  3. Exercise 2: Conditional Branching and Variabl…

  4. Exercise 4: Combining conditional branches an…

  5. Exercise 5: Filtering and Calculating Lists

関連記事

PAGE TOP