Im trying to figure out how the first star in this program gets printed. When the loop that controls the stars is first encountered the value of stars should be 1 due to the initialization withing the loop, the value of light should be zero because it was assigned zero in the declaration of light. The condition to print out the first star is (stars < light) which means ( 1 < 0 ) well thats false right? But the program prints out a star anyways! Whats going on?
#include <iostream>
using namespace std;
int main()
{
int empty = 10; // stars intial value
int light = 0; // space inital value
int lines; // number of lines on screen
int stars; // number of stars each iteration
int spaces; // number of spaces each iteration
for(lines = 0; lines <= 10; lines++) // number of lines
{
for(spaces = empty; spaces > 0; spaces--) // number of spaces
{
cout << " ";
}
for(stars = 1; stars < light; stars++) // number of stars
{
cout << "*";
}
cout << endl;
light += 2;
empty--;
}
}
It’s printing it on the second iteration.
When light is equal to 2, it will print 1 star.