目次
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.