Can someone explain why? I always encounter an infinite loop when I use a do-while loop. What am I missing?
package com.assignment2;
public class FooCorporation {
public static void main(String[] args) {
employee(9.50, 55);
employee(8.20, 47);
employee(10.00, 73);
}
public static void employee(double basePay, int hoursWorked) {
double salary = 0;
int overtimeHours = 40;
do {
if (basePay >= 8.00 || hoursWorked > 40 ) {
if (hoursWorked > 40) {
salary = basePay * hoursWorked
* ((hoursWorked - overtimeHours) * 1.5);
} else {
salary = basePay * hoursWorked;
}
}
else
System.out.println("According to law: Base Pay should be more than $8.00");
System.out.printf("Total Pay: %d %.2f\n", hoursWorked, salary);
} while (hoursWorked <= 60);
}
}
You’re never modifying
hoursWorked‘s value so the condition is always met, hence the infinite loop. You’ll want to increment it’s value at the end of each iteration, afterEdit Just as an extra mile, I’m not getting the meaning of that loop. When would you increase
hoursWorked‘s value? Shouldn’t that be something related to a particular employee? Seems like if you wanted to do that for every employe who worked 60 hours or less. In that case you should have a list ofEmployeeobjects and iterate over them, picking up only the eployees who worked the amount of hours you want.