#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    [C++] need someone to explain the code inside!


    Hello,

    I'm new to c++ and i need someone to help me explain the code below;

    Code:
    #include <iostream>
    using namespace std;
    int main()
    { 
    int num =6;
    cout << "Enter an integer number: ";
    cin >> num;
    for(int p=1; p<=num; p = p + 3)
    { 
    cout << p << endl;
    } 
    }
    i know whats the output when i enter any int number.. but i just wanted to know .. the way it work..

    for example.. when i enter 12 i will get numbers as result.. but i need to know whats happen inside the code..

    also if i change this code below;

    Code:
    for(int p=1; p<=num; p = p + 3)
    to
    Code:
    for(int p=num; p > 0; p = p - 4)
    I'll get different answers for sure.. but wanted to know the way this program works and how the program manage the user inputs..

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Use your debugger and follow your code running and your variables changing statement by statement.
    Have fun!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by lie_soul
    Hello,

    I'm new to c++ and i need someone to help me explain the code below;

    Code:
    #include <iostream>
    using namespace std;
    int main()
    { 
    int num =6;
    cout << "Enter an integer number: ";
    cin >> num;
    for(int p=1; p<=num; p = p + 3)
    { 
    cout << p << endl;
    } 
    }
    i know whats the output when i enter any int number.. but i just wanted to know .. the way it work..

    for example.. when i enter 12 i will get numbers as result.. but i need to know whats happen inside the code..

    also if i change this code below;

    Code:
    for(int p=1; p<=num; p = p + 3)
    to
    Code:
    for(int p=num; p > 0; p = p - 4)
    I'll get different answers for sure.. but wanted to know the way this program works and how the program manage the user inputs..

    Thanks
    The second one means if p is greater than 0, p is equal to p minus 4. If you enter 10, which is greater than 0, p=10-4, or 6. Is that what you are asking?

IMN logo majestic logo threadwatch logo seochat tools logo