Java Reference
In-Depth Information
If then else clause
Now it is possible that the leader may not be above the age of 30 years. The leader may be
below 30 years of age. So now the problem statement says compute total number of chocol-
ates owned by people when their leader is above the age of 30 and if the leader is below the
age of 30 then display a message “Computation not possible as the leader is below the age
of 30”.
Let us write the computation program now.
private int a;
private int b.
private int c;
private int d;
a = 5;
b = 6.
c = 26;
If c >= 30 {
d = a*b,
system.out.println (“Total chocolates” + d);
Else {
System.out.println( “age of the leader is below 30 and so computation not possible”);
Now we are telling the computer to compute 5 multiplied by 6 only when c (age of the lead-
er) is at least 30. If the leader's age is below 30 then the computer will display the message
“age of the leader is below 30 and so computation not possible”.
Here we are testing a condition and telling the computer what to do if a condition is not met.
The “Else” clause does this task. Figure 1.6 depicts this idea to test the condition. If a con-
dition is found to be true then perform one set of computation else perform another set of
computation. You can also notice that we have used condition evaluation result in terms of
true and false. When a condition is met then the condition results in true and if the condition
is not met then it results in false.
Search WWH ::

Custom Search