Below is a piece of code to test if code following continue or break be executed and finally code be executed after break.
public class Reward {
protected int baseReward = 1000;
protected int calculateReward() {
return baseReward;
}
public Reward() {
try {
for (int i = 0; i < 100; i++) {
if (i % 5 == 0) {
continue;
} else if (i == 23) {
break;
}
int reward = calculateReward();
System.out.println("The reward is " + i + "-" + reward);
}
} finally {
System.out.println("Finished!!!");
}
}
public static void main(String[] args){
new Reward();
}
}
Output:
The reward is 1-1000
The reward is 2-1000
The reward is 3-1000
The reward is 4-1000
The reward is 6-1000
The reward is 7-1000
The reward is 8-1000
The reward is 9-1000
The reward is 11-1000
The reward is 12-1000
The reward is 13-1000
The reward is 14-1000
The reward is 16-1000
The reward is 17-1000
The reward is 18-1000
The reward is 19-1000
The reward is 21-1000
The reward is 22-1000
Finished!!!
Conclusion: When the continue or break condition is satisfied, code in the loop following them will not be executed. The finally block will be executed when the break is executed and the program exits the loop.
-----------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
public class Reward {
protected int baseReward = 1000;
protected int calculateReward() {
return baseReward;
}
public Reward() {
try {
for (int i = 0; i < 100; i++) {
if (i % 5 == 0) {
continue;
} else if (i == 23) {
break;
}
int reward = calculateReward();
System.out.println("The reward is " + i + "-" + reward);
}
} finally {
System.out.println("Finished!!!");
}
}
public static void main(String[] args){
new Reward();
}
}
Output:
The reward is 1-1000
The reward is 2-1000
The reward is 3-1000
The reward is 4-1000
The reward is 6-1000
The reward is 7-1000
The reward is 8-1000
The reward is 9-1000
The reward is 11-1000
The reward is 12-1000
The reward is 13-1000
The reward is 14-1000
The reward is 16-1000
The reward is 17-1000
The reward is 18-1000
The reward is 19-1000
The reward is 21-1000
The reward is 22-1000
Finished!!!
Conclusion: When the continue or break condition is satisfied, code in the loop following them will not be executed. The finally block will be executed when the break is executed and the program exits the loop.
-----------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
No comments:
Post a Comment