simple simple design question

This is a discussion on simple simple design question within the C++ Programming forums, part of the General Programming Boards category; I'm going to cut right to the chase and show some code: Code: while(times_value < 11) { product = 7 ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    simple simple design question

    I'm going to cut right to the chase and show some code:
    Code:
    while(times_value < 11)
    {
    	product = 7 * times_value;
    	cout << "7 x " << times_value << " = " << product << endl;
    	times_value++;
    }
    Now I know this is a matter of style, but do you think it's better to get right into the processing as I do here with product being given a value at the start? Or.... do it like this...
    Code:
    while(times_value < 11)
    {
    	cout << "7 x " << times_value << " = ";
    	product = 7 * times_value;
    	cout << product << endl;
    	times_value++;
    }
    Where the info is printed and processed in the same way you would think it out? I know this is a stupid question, I'm just curious what other people think about the order of statements.
    -Chap

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Code:
    while(times_value < 11)
    {
        cout << "7 x " << times_value << " = " << 7 * times_value << endl;
        times_value++;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Or:
    Code:
    for(; times_value < 11; ++times_value)
    {
       cout << "7 x " << times_value << " = " << 7 * times_value << endl;
    }

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    4
    while(times_value < 11)
    cout << "7 x " << times_value << " = " << 7 * times_value++ << endl;
    Last edited by ksarkar; 05-31-2005 at 02:29 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    for(int t=0;t<11;cout<<"7 x "<<t<<" = "<<7*++t<<'\n');
    Last edited by 7stud; 05-31-2005 at 03:21 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    BTW, it's fun to come up with shorter and shorter code, but in actual code I'd prefer my version - it is cleanest and clearest, IMO. Between your original two versions, the first is better, keep similar stuff together - first the processing, then the output. If later you decide to skip output if the value is not appropriate, then the second will be harder to update because you do partial output before you compute the value.

  7. #7
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by 7stud
    Code:
    for(int t=0;t<11;cout<<"7 x "<<t<<" = "<<7*++t<<'\n');
    nice one... but probably endl would be better...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 07:58 AM
  4. a simple question
    By totymido in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2003, 11:44 AM
  5. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21