My homework needs to print out the star pattern by using for loop as below:
!!!!!
!!!!
!!!
!!
!
Here is my loop:
int i, j;
for(i=5;i<=1;i--)
{
for(j=1;j<=i;j++)
{
printf("!");
}
printf("\n");
}
Actually I have succeeded to print out the pattern as below already:
!
!!
!!!
!!!!
!!!!!
By the following loop:
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("!");
}
printf("\n");
}
Now I am confused why pattern 1 can’t be printed, as there are no errors found.
Also the output window will get down when I go ahead to run the program.
My Xcode is Ver 4.4.1
This loop cannot execute anything :
It says “start with
i = 5, and whilei <= 1; doi--“.At the first iteration, i is already > 1, so your loop doesn’t even enter.
What you should do is reverse the condition:
This way, you’ll loop by starting with
i = 5, and performi--whilei >= 1.General rule of thinking for a
forloop :is equivalent to