c - Do and While Loop -


in program, suppose let user input data, , program calculate need , show it. suppose use while,for, , loop program.

  • so far have been successful loop, having problem how use while , loop program.

  • can give me advice?

here codes :

#define _crt_secure_no_warnings #include<stdio.h> #include<stdlib.h>  float load() {     float sal = 0.0;         printf("enter salary\n");     scanf("%f", &sal);     return sal; }  float calcrate(float s) {     if (s > 40000)         return 4.0;     if (s >= 30000 && s <= 40000)         return 5.5;     if (s < 30000)         return 7.0;  } void calcraise(float sal, float rate, float *raise, float *totraise) {     *raise = (sal*rate) / (float)100;     *totraise = *totraise + *raise;  } void calcnewsal(float sal, float raise, float *newsal, float *totnewsal) {     *newsal = sal + raise;     *totnewsal = *totnewsal + *newsal;  } void calctotsal(float *sal, float *totsal) {     *totsal = *totsal + *sal; }  void print(float sal, float rate, float raise, float newsal, float totnewsal, float totraise, float totsal) {      printf("     %0.2f  %0.2f  %0.2f  %0.2f\n", sal,rate,raise,newsal);   }  void main() {     float sal = 0.0;     float rate, raise, newsal;     float totraise = 0;     float totnewsal = 0;     float totsal = 0;     printf("     salary  rate %%  raise  new salary\n");           (int i=1;i<=7;i++)         {         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);          }     printf("total: %0.2f %0.2f %0.2f  \n", totsal, totraise, totnewsal);     system("pause");  } 

you can use while loop 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 do-while follows:

int i=1;  {         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 above have same effect loop you've written. number of iterations hardcoded here there shouldn't apparent difference between while & do-while. however, if initialize 8 instead of 1 notice while loop block not execute @ do-while executes once.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -