Convert a while loop to a for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Merrick
    New Member
    • Mar 2012
    • 1

    Convert a while loop to a for loop

    Hi!

    I need to convert the following while loop:
    count = 1;
    while (caloriesForIte m != 0, count != numberOfItems )
    {
    cout<<"Enter the calories: ";
    cin >> caloriesForItem ;
    totalCalories += caloriesForItem ;
    count++;
    }

    This is what I have come up with:
    totalCalories += caloriesForItem ;
    for (count = 1; caloriesForItem != 0; count != numberOfItems)
    {
    cout<<"Enter the calories: ";
    cin >> caloriesForItem ;
    count++;
    cout << "Total calories eaten today = " << totalCalories << endl;
    }

    However the output is not the same?

    any pointers perhaps?
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    First of all, in the condition for that while-loop, you're using the comma wrong:
    The C++ Specification section 5.18, page 92
    A pair of expressions separated by a comma is evaluated left-to-right and the value of the left expression is
    discarded
    . The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions
    are not applied to the left expression. All side effects (1.9) of the left expression, except for the
    destruction of temporaries (12.2), are performed before the evaluation of the right expression. The type and
    value of the result are the type and value of the right operand;
    the result is an lvalue if its right operand is.

    In contexts where comma is given a special meaning, [Example: in lists of arguments to functions (5.2.2)
    and lists of initializers (8.5) ] the comma operator as described in clause 5 can appear only in parentheses.
    [Example:
    f(a, (t=3, t+2), c);
    has three arguments, the second of which has the value 5. ]
    So for your while-loop, only count != numberOfItems is used. You should use the logical and-operator (&&) instead of the comma-operator.

    As for the for-loop, it's also a bit different from what you have. The structure for a for-loop is (also in the C++ Specification; section 6.5.3, page 99):
    for(initialisation;condition;expression)
    Where initialisation is executed before the loop starts, and expression is executed after each iteration (which makes it a good place to increment your counter). The loop stops when condition is false at the start of an iteration (just like a while loop).

    Comment

    • hellfire
      New Member
      • Mar 2012
      • 2

      #3
      Hey, whatever sir said is absolutely fine, but if you didnt get it from that, here is a code block :

      totalCalories += caloriesForItem ;
      for (count = 1; caloriesForItem != 0 && count != numberOfItems; count++)
      {
      cout<<"Enter the calories: ";
      cin >> caloriesForItem ;
      cout << "Total calories eaten today = " << totalCalories << endl;
      }

      Hope now this is clear to you!! :)

      Comment

      • rekedtechie
        New Member
        • Feb 2012
        • 51

        #4
        for(count=1;cal oriesForitem!=0 || count!=numOfIte ms;count++)
        {
        cout<<"Enter num of cals:"<<endl;
        cin>>caloriesFo ritem
        cout<<" "<<endl;
        totalCalories = totalCalories + caloriesForitem ;
        cout<<"Total cals eaten today="<<totalC alories<<endl;
        }

        //your while condition is set to terminate..
        //if one of the two condition is true..
        //meaning you need to put the logical OR in your for loop condition. =)

        Comment

        • hellfire
          New Member
          • Mar 2012
          • 2

          #5
          @rekedtechie :

          Hey, I would totally agree with you, but dear it is required to stop if either condition fails so it has to be and operator.. I hope I have made myself clear to you.. :)

          Comment

          • rekedtechie
            New Member
            • Feb 2012
            • 51

            #6
            //by the way, the correct looping process is like this..

            for(count=1;cou nt<=NumOfItems; count++)
            {
            cout<<"enter num of cals:"<>endl;
            cin>>numOfCalor ies;
            if(numOfCalorie s==0)
            {
            goto EndOfLoop;
            }
            else
            {
            totalCalories = totalCalories + numOfCalories;
            }
            }
            EndOfLoop:
            cout<<"Total Calories eaten today: "<<totalCalorie s<<endl;

            //wait, i will post the other looping formats..=)

            Comment

            • rekedtechie
              New Member
              • Feb 2012
              • 51

              #7
              please correct this part..

              <>endl;

              make these..

              <<endl;

              //maybe this note.. can help you in looping process..

              //FOR LOOP
              for(initializat ion;condition;v ariable inc/dec)
              {
              statements..
              }

              //WHILE LOOP
              varable initialization;
              while(condition )
              {
              statements..
              variable inc/dec
              }

              //DO WHILE LOOP
              variable initialization;
              Do
              {
              statements..
              variable inc/dec
              }while(conditio n);


              logical OR ||
              logical AND &&

              //these are the looping formats, and i included the commonly used logical operator..=)

              condition - if the condition is true, then the looping process will continue..

              for(i=0;i<=5;i+ +)
              i=0+1
              i=1
              condition is true..

              if i reached 5 then i++ again..
              meaning i=6, then the condition will turn into false..
              -terminating the looping process.

              you can also do multiple loops..
              prod=0;
              for(cl=0;cl<=5; cl++) {
              for(rw=0;rw<=5; rw++)
              {
              cout<<" "<<rw*cl;
              //to show the results of 5 numbers in one line of results..
              }
              cout<<endl;
              //to create another line results..
              }
              thats all..good luck. =)

              remove this code..
              prod=0;
              its nothing..
              //result looks like this..

              0 1 2 3 4 5
              0 0 0 0 0 0
              1 1 2 3 4 5
              2 2 4 6 8 10
              3 3 6 9 12 15
              4 4 8 12 16 20
              5 5 10 15 20 25
              Last edited by Niheel; Mar 28 '12, 06:57 AM.

              Comment

              Working...