Like Tree1Likes
  • 1 Post By laserlight

Expressions in cout statement vs variable

This is a discussion on Expressions in cout statement vs variable within the C++ Programming forums, part of the General Programming Boards category; Hello all. Is it better programming practice to 1) output the value of an expression a * b in a ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    Expressions in cout statement vs variable

    Hello all.

    Is it better programming practice to

    1) output the value of an expression a * b in a cout statement as a variable containing its value
    Code:
       total = a * b;
       cout << total << endl;
    or

    2) is printing value of the expression as calculated directly from the cout statement preferred?
    Code:
       cout << a * b;
    I searched around, but couldn't find any specific information regarding this. I've seen both in code.

    Thank you for your time!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Do you need the total besides for output? If so, then the former is appropriate, otherwise the latter is appropriate.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    The only time I need the total is for output.

    Thank you for your response!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable declarations and arithmetic expressions
    By r3zaneo in forum C Programming
    Replies: 3
    Last Post: 10-21-2012, 01:36 PM
  2. if statement with hex variable
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 04-18-2012, 03:36 AM
  3. Replies: 1
    Last Post: 07-15-2011, 10:46 AM
  4. declare variable in case statement
    By Marksman in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 02:32 AM
  5. Evaluating multiple expressions in an If statement.
    By bitslap in forum C Programming
    Replies: 4
    Last Post: 11-01-2008, 04:38 AM

Tags for this Thread


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