I'm studying Java at the moment and as a total beginner I have no idea what the last bit of the code is about. If someone can please explain how "count" ties in, and how it ties in with the "if" statement. As far as learning goes, I'm making a lot of assumptions to basically assume I'm on the right path until I read a bit that clarifies it. Thus far no explanation has been given for the specific part in the function, please help.

int count = 0;
int day;

for (day = 0; day < 365; day++) {
  if (used[day] == true)
    count++;
}
System.out.println(count);

So my question would be, does the if statement relate to count, as its after that 'condition' does count become the variable placeholder for the condition of 'if'?

2 Answers

4
SandPiper On

The ++ is an increment operator. Whatever the value of count is at that point, it will add 1 to it. In plain English, it is saying "If this condition is true, then add 1 to the value of count". Your function then loops to the next value in your for loop.

In this case, it's essentially just giving the user a visible output of how many days were used.

More information on the increment operator can be found here: https://www.dummies.com/programming/java/increment-and-decrement-operators-in-java/

And see this Stack Overflow question here: How do the post increment (i++) and pre increment (++i) operators work in Java?

0
Shehan Wisumperuma On

count variable is within if condition. If the condition is found true, count variable will get incremented by 1.

Its actually like this,

int count = 0; 
int day; 
for (day = 0; day < 365; day++) { 
    if (used[day] == true) {
         count++; 
    }
 } 
 System.out.println(count);

Its not required to use curly braces, but it's good practice.