loop breakdown

Hi there, i've been programming for 1 week now with C++. (my first ever programming language in fact).

I've gotten as far as loops, and up until now have been able to understand every concept in all lessons/chapters of the books i read. I'm having a little bit of a problem with loops though. Now i can write a loop, but i can't understand a part of it, and im just writing from memory.

Example code is at the bottom.
Now what im struggling to understand (for 2 days now) is the inner workings of that statement. those 3 lines. (well the top 2 actually). Can someone break every part between the ;'s and ()'s piece by piece for me. As im not grasping in loops and especially in the below example, how the output to console is showing me 7 *'s in the window if i type 7 in std::cin. How is that working, it looks like just 1 * in the code.

I guess what im asking is how the below works piece by piece, and when does the ++i come into it, and what happens after that to make the code print more than one *.

Thanks guys! counting on you to save my brain from meltdown


1
2
3
  for(int i = 1; i <= data1; ++i)
        cout << "*";
    cout << data1 << endl;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(/*...*/)
   cout << "*";
//Is equivalent to
for(/*...*/){
   cout << "*";
}


/*for loop*/
for(
   int i = 1; //Create a variable 1 and initialize it with 1. This will serve as a counter.
   i <= data1; //Keep running another iteration/loop while i is less than or equal to data1
   ++i //After each iteration/loop, increase i by 1
)
cout << "*"; //In each iteration/loop, print an asterisk.
/*end for loop*/


The last line is outside of the for loop.
another quick example of the same thing that i need describing just as a "second case" so im sure of the same answer in multiple instances of a similar loop

int number, factorial;
cout << "Enter a number: ";
cin >> number;
factorial = 1;
for(int i = number; i >= 1; --i)
factorial *= i;
cout << number << "! equals " << factorial << endl;
oh god thats perfect Daleth thank you so much! i really needed that info thanks alot!! wow. now i get it... damn i feel so stupid. can you do the same for the next (and last dont worry) one. cheers!
The for loop in your second code snippet counts down instead of up in a range from number to 1. And in each iteration, the counter is multiplied to the factorial variable.

This is probably to make it easier to relate this to how a factorial works:
n! = n*(n-1)*(n-2)*(n-3)...*1

However, you can modify the loop to count up instead of down and still get the same result.
you couldn't have made it easier to understand. thanks alot brother! i think im doing fine for only a week of coding for the first time ever. im just getting onto vectors once i clear this small loop issue up. then after that it's pointers.. maybe ill run into more trouble there
oh one last thing. the book keep presuming im intelligent, so i have actually one last question. we typed

1
2
int square (int number)
{            int squared = number * number;  


now the book says "we could have done this in 2 statements, but as we know about declarations and initializations work, we'll only do it with 1.

what would that 2 statement one look like? just out of interest, as i dont get what it means.

and he has this line aswell in it
std::cout << number << "squared equals " << square(number) << endl;

how on earth would i ever know to add parenthesis around the number there.
Last edited on
I think it is talking about the second line. You could, for example, write:
1
2
3
int squared;  //Declare a variable called squared
squared = number; //Assign it some number
squared *= number; //Now multiply it 


Vs.

1
2
3
   //Declare a variable squared and initialize it--immediately assign it a value--
   //   with number * number
int squared = number * number;



Extra:
And in the case of this function, you might have:
1
2
3
4
5
int square (int number)
{
   int squared = number * number;
   return squared;
}

But you can compress this even further to not create a variable:
1
2
3
4
int square (int number)
{
   return number * number;
}
Last edited on
and the *= is multiply it and give the result in 1 statement? so times then ='s it together?
Oh, sorry I assumed you already knew about those. Yes, *= is equivalent to * first then = second.
They are called compound assignment operators: +=, -=, *=, /=, %=, >>=, <<=, ^=, |=, &=.

They are used for shorthand notation, like:
1
2
3
i = i + j;
//Equivalent to
i += j;


For even shorter notations, there are the increment and decrement operators: ++ and --.
1
2
3
i = i - 1;
i -= 1;
--i;


There is also a difference between ++i (prefix) and i++ (postfix). ++i increments i by one and returns i. i++ increments i, but returns what i was before incrementing. The same is true for --i and i--.
Topic archived. No new replies allowed.