You can use the while loop as follows:
int i=1;
while(i<=7) {
sal = load();
rate = calcRate(sal);
calcRaise(sal, rate, &raise, &totraise);
calcNewSal(sal, raise, &newsal, &totnewsal);
calcTotSal(&sal, &totsal);
print(sal, rate, raise, newsal, totsal, totraise, totnewsal);
fflush(stdin);
i++;
}
And the do-while as follows:
int i=1;
do {
sal = load();
rate = calcRate(sal);
calcRaise(sal, rate, &raise, &totraise);
calcNewSal(sal, raise, &newsal, &totnewsal);
calcTotSal(&sal, &totsal);
print(sal, rate, raise, newsal, totsal, totraise, totnewsal);
fflush(stdin);
i++;
} while(i<=7);
Both of the above will have the same effect as the for loop you’ve written. The number of iterations is hardcoded here so there shouldn’t be any apparent difference between while & do-while. However, if you initialize i to 8 instead of 1 you will notice that while loop block does not execute at all but the do-while executes once.
0
solved Do and While Loop