Thread: So another simple program just help me with this logic

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    Question So another simple program just help me with this logic

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        int day = 1;
        float amount = .01;
    
    
        while (day <= 31){
            printf ("Day:%d \t Amount:$%.2f\n", day, amount);
            amount *= 2;
            day++;
        }
        printf ("-----------------------------------------------------------\n");
    
    
        day = 1;
        amount = .01;
        while (day <= 31){
            printf ("Day:%d \t Amount:$%.2f\n", day, amount);
            amount * 2;
            day++;
        }
        return 0;
    }
    So yeah it's two kind of examples. Just curious about the logic behind

    Code:
    amount *= 2;
    and

    Code:
    amount * 2;
    Why did it not multiply in the 2nd while loop while in the first it multiplied. I mean it's somewhat the same right? What amount *= 2 is saying to amount * 2? Just confused.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that:
    Code:
    amount *= 2;
    is logically equivalent to:
    Code:
    amount = amount * 2;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Recall that:
    Code:
    amount *= 2;
    is logically equivalent to:
    Code:
    amount = amount * 2;
    So amount * 2 is just multiplying the variable right? Or no? I jsut dont get it.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    Code:
    amount * 2;
    The result of this will be amount multiplied by 2 but it won't be assigned to anything. If you want the value of amount to be multiplied by two then you need to assign the result to amount which you could do with
    Code:
    amount = amount * 2;
    In this case though you are assigning to the same variable you are multiplying so you can instead use
    Code:
    amount *= 2;
    Last edited by Golf7; 09-16-2015 at 07:56 AM.

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Thanks that helped bro! Alright new thread XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple logic problem
    By madjm in forum C Programming
    Replies: 4
    Last Post: 05-04-2013, 02:02 PM
  2. Replies: 16
    Last Post: 11-09-2011, 01:43 PM
  3. Simple Logic problem
    By will of fortune in forum C Programming
    Replies: 4
    Last Post: 03-06-2010, 02:25 PM
  4. Simple Bouncing Logic
    By SMurf in forum Game Programming
    Replies: 4
    Last Post: 10-27-2006, 10:37 AM
  5. Replies: 5
    Last Post: 01-31-2006, 01:54 AM

Tags for this Thread