How to write for a loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cool17
    New Member
    • Oct 2006
    • 24

    How to write for a loop?

    printf("\nEnter the number of terms:\n");
    scanf("%i",&j);
    printf("\nPleas e enter expression info as follow:\n");

    {
    double value;
    double power;
    double times;
    int pos;
    int isX;
    int KnowX;


    printf("-------- expression info:--------\n");
    printf("What is the single value? =");
    scanf("%f",&val ue);

    printf("How many numbers of xtimes? =");
    scanf("%f",&pow er);

    printf("What is the power value? =");
    scanf("%f",&tim es);

    printf("What is the sign ? =");
    scanf("%i\n",&p os);

    }

    with the above code how do i add the loop?Thank guys for the help
  • sircool
    New Member
    • Oct 2006
    • 12

    #2
    Originally posted by cool17
    printf("\nEnter the number of terms:\n");
    scanf("%i",&j);
    printf("\nPleas e enter expression info as follow:\n");

    {
    double value;
    double power;
    double times;
    int pos;
    int isX;
    int KnowX;

    do
    {
    printf("-------- expression info:--------\n");
    printf("What is the single value? =");
    scanf("%f",&val ue);

    printf("How many numbers of xtimes? =");
    scanf("%f",&pow er);

    printf("What is the power value? =");
    scanf("%f",&tim es);

    printf("What is the sign ? =");
    scanf("%i\n",&p os);
    function(); // that will be calculate the power
    printf("if you want to calculate another one type 'e' otherwise program will close");

    while(getche()= ='e' );
    }
    return 0;
    i add do while loop to your code
    getche() function takes a character that will ask users request if user types e it loops again otherwise program will end...
    i hope it was helpfull

    Comment

    • cool17
      New Member
      • Oct 2006
      • 24

      #3
      thank for the help. But on the above of my code i ask user for the number of terms needed , i will den prodced the data as required... So is there any other ways of writting it?

      Comment

      • iknc4miles
        New Member
        • Oct 2006
        • 32

        #4
        for loops - general form


        for( 'StartingCondit ion'; 'ConditionConst raint'; 'AdvanceConditi on)
        {

        // Do stuff while ConditionConstr aint is not met

        }

        Usually you write them in a form similar to below

        int i;
        for( i = startingvalue; i < maxvalue; i++)
        {
        cout << i << endl;
        }

        This states that I want to print all the integers from startingvale to maxvalue-1 because each time I run the loop, I increase i by 1 (i++).

        Comment

        Working...