-2

I am new to programming and whenever I have used a for loop it has consisted of the following

 1. Initialize
 2. Condition 
 3. Body 
 4. Update 
 5. Go to 2
 6. Quit.

However in the for loop before what is cin>>number doing in place of the update?

int number = 0;
cout<<"please enter a number that is greater than 15"<<endl;
for (cin>>number; number <= 15; cin>>number)
{
    cout<<" Please enter a number that is greater than 15"<<endl;
}
mmmmmm
  • 32,227
  • 27
  • 88
  • 117

4 Answers4

5
  1. Initialize

    cin>>number

    User is asked to provide the initial value.

  2. Condition

    number<=15

    Number is checked against condition.

    If number is less than 15, go to 3, else go to 6.

  3. Body

    cout<<" Please enter a number that is greater than 15"<< endl;

    This forms the body, which is executed every time.

  4. Update

    cin>>number

    User is again asked to input a number.

  5. Go to 2

  6. Quit


Also, this a very odd coding style. Looping constructs are usually not used in this form. They have the elements that you mentioned but in a more clearer way.

If your reference has code like this, you should probably refer to something else. Its probably not the best book to begin studying C++ with. See some of the books here.

Community
  • 1
  • 1
asheeshr
  • 4,088
  • 6
  • 31
  • 50
4

It is a wierd style, but essentially it prompts the user for a number each iteration (instead of the typical initialization or loop counter update steps ) and checks that against 15. As long as user enters < 15, it keeps looping.

This is not how I would write it if readability is high on my priority list (it should be #1 or close). This kind of code might be better expressed as.

cin>>number;
while (number <= 15){
    cout<<" Please enter a number that is greater than 15"<<endl;
    cin>>number;
}
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • This is the right answer, but you should probably mention that code like this is not recommended; as it's non-idiomatic, and therefore confusing! – Oliver Charlesworth Jan 03 '13 at 10:31
  • @OliCharlesworth I thought I covered that in "It is a wierd style" – Karthik T Jan 03 '13 at 10:32
  • @Oli: I don't think using for loops "non-idiomatically" should be avoided because it's "confusing", although the fact that `cin>>number` is duplicated here is a bit ugly. Would you also ban for loops that, say, navigate tree structures because they don't have a familiar `int i`? – Matti Virkkunen Jan 03 '13 at 10:34
  • @MattiVirkkunen: Well the ugly duplication is one thing, but also the fact that stuff with complex side effects is hidden inside the for construct. This could be handled idiomatically with a do-while loop or something, and therefore should be avoided, IMO. Obviously, it's a style thing, though... – Oliver Charlesworth Jan 03 '13 at 10:35
1

The update stage is the 2nd cin >> number in the for statement.

This is a somewhat unusual way of doing this, but it is a perfectly valid update of number. Although ++ and -- are somewhat more common, this clearly does the job of updating number each time round the loop. People often go to quite an amount of effort to avoid having the initialisation part the same as the update part, though I'm not sure why.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
-1

"cin>>number" accepts the number "number <= 15" checks ths condition is number is greater or equal to 15 then again

rahul yadav
  • 23
  • 1
  • 7