Control Statements — Part 4
In this chapter, we are going to understand block statement with a complex program. Along the way we are going to learn two relational operator and logical OR operator.
Block Statement With If-Then and If-Else-Then Statement
A block statement is a group of statement enclosed by opening ‘{‘ and closing ‘} parenthesis. A block statement can contains single statement or multiple statement. Here are examples:
Block statement containing single statement:
{
int a = 10 + 20;
}Block statement containing multiple statement:
{
int a = 10 + 20;
if (a == 100) a++;
}Remember control statements like if-then statement or if-then-else statement are statements also, so they can occur inside block statement.
We are going to see by far the most complex example so far and analyze it. Following is the program:
As the program is quite complex for beginner and we used some new operators this needs in depth discussion. First I am going to lay out what is the program doing. Lets begin by understanding what is the program doing from high level.
High Level Overview Of The Program
Suppose you have given math exam, now the program is asking for your score. Following are the requirements of the program:
- If you have scored more than 59 out of 100 then you have managed to pass the course.
- if your score is 100 then you have achieved perfect score.
- If your score is between 95 and 99 (inclusive) then your grade is A.
- If your score is greater than or equal to 90 and smaller than 95 then your grade is A-.
- If your score is between 60 and 89 (inclusive) your grade is below A-, in this case we promote your grade to A- if your score is greater than or equal to 87.
- Now if your score is between 0 and 59 (inclusive) then you did not manage to pass the course
- If you input negative integer or integer greater than 100 as score then you have given incorrect score.
Lets develop the program step by step. First we will write the program which will meet requirement 7. Here is the program:
At line 9 we use math if-then statement to test if score is below 0 or above 100.
Get rakibul rushel’s stories in your inbox
Join Medium for free to get updates from this writer.
Let us understand a simpler if-then statement using relational operator ‘<’ named smaller than first.
if (mathScore < 0) {
System.out.println("Your math score is below 0");
}The smaller than relational operator hast two operands. The expression format of ‘<’:
expression1 < expression2
NOTE: Any expression involving relational operator, logical OR, logical AND, equality operator will be evaluated to true or false.
Evaluation of expression1 < expression2:
expression1 is left operand and expression2 is right operand of logical OR operator and its associativity is left to right. Let e1 be the value evaluated from expression1 and e2 be the value evaluated from expression2. if e1 is smaller than e2 than expression1 < expression2 is evaluated to true otherwise false.
Now if the variable mathScore contains integer smaller than 0 then the expression mathScore < 0 will be evaluated to true otherwise false.
The relational operator ‘>’ named greater than is opposite of ‘<’ operator. The mathScore > 100 will be evaluated true if the variable mathScore contains integer greater than 100 otherwise false.
We can now understand the if-then statement in the program at Fig 1.2 which is following:
if(mathScore < 0 || mathScore > 100) {
System.out.println("Incorrect score!, Please give correct score.");
} expression1 || expression2
Note: expression1 and expression2 must be evaluated to either true or false.
Due to operator precedence the relational operator will be applied first then the logical operator. Here is the documentation from the oracle on operator precedence. You also may read this chapter also.
There are two operands of the logical OR operator one is expression1 and another is expression2. As the logical OR has left to right associativity, the left operand that is expression1 is evaulated first and after that the right operand that is expression2 is evaulated. Here, expression1 is mathScore < 0 and expression2 is mathScore > 0. If one of the expression is evaluated to true then the whole expression mathScore < 0 || mathScore > 100 is evaluated true otherwise false.
This is it for this chapter. This chapter will continue…..
Next Chapter Coming Soon.






