Help with if/else if

How would this code look like if it was in if/else if?

1
2
3
4
5
6
7
  if (hours <= 40)
     {gross = hours * payrate;
      ovthours = 0;}
  if (hours > 40)
     {ovthours = hours - 40;
      gross = 40 * payrate + 1.5 * payrate * (ovthours);
      hours = 40;}

Would this be a valid code?
1
2
3
4
5
6
7
  if (hours <= 40)
     gross = hours * payrate,
     ovthours = 0;
       else if (hours > 40)
              ovthours = hours - 40,
              gross = 40 * payrate + 1.5 * payrate * (ovthours),
              hours = 40;
Last edited on
1
2
3
4
5
6
7
8
9
if (hours <= 40){
     gross = hours * payrate;
     ovthours = 0;

}else {
              ovthours = hours - 40;
              gross = 40 * payrate + 1.5 * payrate * (ovthours);
              hours = 40;
      }//end if..else 
Last edited on
Thanks a bunch
It will, but do not overuse comma operator (this can lead to unexpected behavior here), you even doesn't need an elseif actually:
1
2
3
4
5
6
7
8
if (hours <= 40) {
    gross = hours * payrate;
    ovthours = 0;
} else { //You do not need a condition (if hours not less or equal than 40, it will guaranteed larger than 40)
    ovthours = hours - 40;
    gross = 40 * payrate + 1.5 * payrate * (ovthours);
    hours = 40;
}
Topic archived. No new replies allowed.