Thread: simple simple design question

  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,366
    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,663
    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,366
    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
    Registered User
    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, 12:15 PM
  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, 08: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, 06:41 PM