Control Statements — Part 5
This is the continuation of the previous chapter.
Requirements of the program we are solving:
- 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.
Following is the main program:
In the previous chapter we solved the requirement 7. Now are going to solve requirement 1 and 6. Here is the program where we starts:
We know that if you input score smaller than 0 and greater than 100 then the message “Incorrect score!, Please give correct score” is shown to the output. Otherwise, we have a valid score. This sounds like a we have to introduce else for the if-then statement at line 9 and make it if-then-else statement like following:
if(mathScore < 0 || mathScore > 100) {
System.out.println("Incorrect score!, Please give correct score.");
}
else ; //we weill write code for requirements 1 and 6 here
You might be wondering what the hell is the single semicolon is doing there and what it is? The single ‘;’ semicolon is called empty statement. Empty statement is also an statement which does nothing.
“//” denotes single line comment in java. Comments are ignored by the java compiler. So, it is not basically part of the code but helps in understanding the purpose of the code.
Now lets focus only on the requirements 1 and 6, following is the code we would have write:
if(mathScore >= 60 && mathScore <= 100) {
System.out.println("You have passed the course.");
}
else {
System.out.println("You have failed the course");
} Lets analyze the above code. we introduced 3 new operators: greater than or equal to “>=”, smaller than or equal to “<=” and logical AND “&&” operator in the expression “mathScore >= 60 && mathScore <= 100”.
Get rakibul rushel’s stories in your inbox
Join Medium for free to get updates from this writer.
The “>=” and “<=” operators are part of the relational operator and have higher precedence than the logical AND operator. All 3 operators mentioned here has left to right associativity. I wont go on and explain how expressions evaluated. You can read previous chapter for this.
The logical AND format is following:
expression1 && expression2
The expression “expression1 && expression2” will be evluated to true if
expression1 and expression2 both evaluated to true otherwise the expression evaluated to false.
So if the mathScore is is smaller than 30 then 60 then
“You have failed the course” is shown as output in the console. Otherwise if mathScore is between 60 and 100 inclusive then output
“You have passed the course.” is shown.
Now lets modify the following code:
if(mathScore < 0 || mathScore > 100) {
System.out.println("Incorrect score!, Please give correct score.");
}
else ; //we weill write code for requirements 1 and 6 hereto the following one:
if(mathScore < 0 || mathScore > 100) {
System.out.println("Incorrect score!, Please give correct score.");
}
else if(mathScore >= 60 && mathScore <= 100) {
System.out.println("You have passed the course.");
}
else {
System.out.println("You have failed the course");
}Look in place of empty statement (which is also an single statement) we put a if-then-else statement (which is also a single statement). Following image highlighted the if-then-else statement.
What should we do if the user enters valid score. There are two cases one is he/she passed the course or failed to pass the the course.
Now we can re-write the above program and remove an unnecessary expression which is mathScore ≤ 100. Lets modify the program once again:
if(mathScore < 0 || mathScore > 100) {
System.out.println("Incorrect score!, Please give correct score.");
}
else if(mathScore >= 60) {
System.out.println("You have passed the course.");
}
else {
System.out.println("You have failed the course");
}Removing that wont change anything. Can you find the reason why? The reason is that we have already made sure that mathScore is between 0 and 100 (inclusive) by writing if (mathScore < 0 || mathScore > 100). If the expression “mathScore < 0 || mathScore > 100” is evaulated to false then we have mathScore is between 0 and 100 (inclusive). Please read the
previous chapter to understand the evaulation of the expression “mathScore < 0 || mathScore > 100”.
Below is the program which meet requirement 1, 6 and 7:
If you have a look at the lines from 16 to 19 it contains two statements one at line 16 and another at line 17 (if-then statement). These two statements are enclosed by open curly ‘{‘ braces at the end of the line 16 and closing curly braces ‘}’ at the end of the line 20. As those statements inside opening and closing curly braces as a whole it is called block statement.
This is it for this chapter and also for this example. Remaining Things should be very easy to understand. You have seen lots of new operators along the way and that is enough for now. We need to see other Control statements also.





