1. Apex練習問題

Exercise 2: Conditional Branching and Variable Manipulation

Write the code according to the following conditions

Declare a variable score of type Integer and assign it a value of 75.

If the score is 80 or higher, assign “Passed” to the String variable “result”; otherwise, assign “Failed”.

Finally, output the value of “result” to the console using System.debug().

Sample Answers

// Declare a variable score of type Integer and assign 75
        Integer score = 75; 
        
// Declare a variable “result” of String type and assign “Passed” if score is 80 or more, otherwise assign “Failed        
  String result; if (score >= 80) { if (score >= 80)
        if (score >= 80) {
            result = 'Passed'; }
        } else {
            result = 'Failed'; }
        }
        
// output the value of result to the console using System.debug()
        System.debug('Result: ' + result);

Explanation

1. declaring variables

First, let’s review variables. Variables are “boxes” for storing data and are given specific names.

Declare a variable score of type Integer and assign 75 to it

Integer score = 75;
  • Integer is a type for handling integers.
  • Create a box named score and put the value 75 in it.

2. determining the result

Next, determine if the value of score is 80 or more. In this part, conditional branching is used. Conditional branching is a type of branching that performs different processing depending on whether a condition is true or false.

If score is 80 or higher, the result is “Passed”; otherwise, it is “Failed.

String result;.
if (score >= 80) {
    result = 'Passed'; }
} else {
    result = 'Failed'; }
}
  • Declare a variable result of type String. This box stores a string (text).
  • if (score >= 80) checks if score is greater than 80.
  • If score is greater than 80, assign ‘Passed’ to result.
  • Otherwise (score less than 80), ‘Failed’ is assigned to result.

3. Display the results on the console

Finally, the evaluation results are output to the console.

System.debug('Result: ' + result);
  • System.debug() is an instruction to display the contents of a variable.
  • The message ‘Result: ‘ and the value contained in result are displayed together.

Result displayed in the console

When this program is executed, the following message is displayed

Result: Failed

This indicates that the result was determined to be “Failed” because the SCORE is 75 and less than 80.


Summary of the entire process

  • We created a box score to store the score and put 75 in it.
  • We checked if score was over 80 and saved the result in a box called result.
  • Finally, we used System.debug() to display the result on the console.

This is the entire flow of this code.

Apex練習問題 recent post

  1. Exercise 2: Conditional Branching and Variabl…

  2. Exercise 1: Variable Declaration and Manipula…

関連記事

プロフィール

●氏名:ARAKAN
●年齢:32歳
●居住地:九州
●勤務先:情報システム部
●一言:とある企業でSalesforce管理者として勤務しているARAKANです。業務の中での気づきやTipsを投稿しています。
<保有資格>
・Salesforce認定 アドミニストレーター
・Salesforce認定 上級アドミニストレーター
・Salesforce認定 Sales Cloudコンサルタント
・Salesforce認定 Service Cloudコンサルタント
・Salesforce認定Marketing Cloud Account Engagement コンサルタント
・Salesforce認定Marketing Cloud Account Engagement スペシャリスト
●Name: ARAKAN
●Age: 32 years old
●Place of residence: Kyushu
●Workplace: Information Systems Department
●One word: My name is ARAKAN and I work as a Salesforce administrator at a certain company. I post my observations and tips during my work.
Qualifications held
・Salesforce Certified Administrator
・Salesforce Certified Senior Administrator
・Salesforce Certified Sales Cloud Consultant
・Salesforce certified Service Cloud consultant
・Salesforce Certified Marketing Cloud Account Engagement Consultant

カレンダー

October 2024
MTWTFSS
 123456
78910111213
14151617181920
21222324252627
28293031 

アーカイブ

Language Switcher

PAGE TOP