educative.io

Exrcicses 3 on control flow

I really don’t understand exrcicses 3 on control flow, the code says o - 6 because i is 0 at first and 6 is n. But then there is only 5 spaces printed out? why?


Course: Learn C from Scratch - Learn Interactively
Lesson: Exercises on Control Flow - Learn C from Scratch

Hi @A_Huang, Thanks for reaching out to us.
The following for loop and variable j is responsible for printing the space:

for (j=0; j<(n-i); j++)
  { 
      printf(" "); 
  }

Now let’s consider the 6th iteration and value of n=6 when space is not printed

j<(n-i) -----> 6< 6-6 -----> 6<0 -----> False

The spaces are not printed at the 6th iteration because the condition j<(n-i) in the for loop is false and print(" ") not executes at this time.

Hope it will clear your confusion, Happy Learning :slight_smile:

So it’s 0< 6, 1 < 6, 2 <6, 3 < 6, 4 < 6, 5 < 6, 6 <6(false). But then there are still 6? I’m happy to see replys but I still don’t understand


Course: Learn C from Scratch - Learn Interactively
Lesson: Exercises on Control Flow - Learn C from Scratch

1 Like

for (j=0; j<(n-i); j++) { printf(" "); }

Hi @A_Huang, Thanks for reaching out. Each line of the pattern carries spaces, if we consider the first line there are 5 spaces.
Condition for first-line: 0<6, ––> 5 spaces
Condition for second-line: 0<5, ––> 4 spaces
Condition for third-line:0<4, ––> 3 spaces
Condition for fourth-line:0<3, ––> 2 spaces
Condition for fifth-line:0<2, ––> 1 spaces
Condition for sixth-line:0<1, ––> 0 spaces
0<0 ––> Condition false