Jump to content

C For loops, help tracing through code

Go to solution Solved by andrew_on_tech,

Program executes in this order.

  • So the program enters the Main method. 
  • Allocates a int and points it "i"
  • Allocates an array of INTs at size 10 with garbled data(for the most part, since your not initializing it)
  • Program steps into for loop 1 
for (i = 0; i < 10; i++)
		array[i] = i;
  • The Loop will run 10 times assigning ints 0-9 into array index 0-9 respectively. 
  • After "i" is no longer less than 10 the loop will exit and the program will enter the second loop
for (i = 0; i < 10; i++)
		printf("%d\n", array[i]);
  • The Loop will run 10 times printing out the value stored at array index "i"
  • Output will be:
    • 0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • Then "i" will no longer be less than 10 so it will exit the loop, hit your return and you will finish your method. 

Hopefully this is what you were looking for. 

I'm trying to get to grips with for loops and I've just understand how nested loops work, the inner loop keep iterating until the condition becomes false.

 

However what if you have two seperate for loops? Does each for loop get excuted one after the other or does one keep looping round until the condition is false then moves onto the next for loop? I'm confused.

 

For example I have this code:

#include <stdio.h>

int main()
{
    int i;
	int array[10];
	
	for (i = 0; i < 10; i++)
		array[i] = i;
	
	for (i = 0; i < 10; i++)
		printf("%d\n", array[i]);
	return 0;
}

 

Does the first for loop iterate until it becomes false? Or does it loop once then go onto the second for loop, and repeats until both conditions on both loops becomes false?

 

Can anyone trace through this code one step at a time so I can see the order it's supposed to be?

   
   
Link to comment
https://linustechtips.com/topic/546979-c-for-loops-help-tracing-through-code/
Share on other sites

Link to post
Share on other sites

The first loop would run till false, then the second loop would run. In C based languages everything in the same scope of a method will run top down. 

 

So this will run 10 times. 

for (i = 0; i < 10; i++)
		array[i] = i;

Then this will run 10 times.

for (i = 0; i < 10; i++)
		printf("%d\n", array[i]);

If you need more help I can further explain or answer any other questions you may have.

Link to post
Share on other sites

2 minutes ago, andrew_on_tech said:

The first loop would run till false, then the second loop would run. In C based languages everything in the same scope of a method will run top down. 

 

So this will run 10 times. 


for (i = 0; i < 10; i++)
		array[i] = i;

Then this will run 10 times.


for (i = 0; i < 10; i++)
		printf("%d\n", array[i]);

If you need more help I can further explain or answer any other questions you may have.

Ok i think I get it, but to be 100% sure could you trace through the example code if it's not too much hassle? I'm trying myself right now but I jsut want to be sure i'm right.

   
   
Link to post
Share on other sites

Program executes in this order.

  • So the program enters the Main method. 
  • Allocates a int and points it "i"
  • Allocates an array of INTs at size 10 with garbled data(for the most part, since your not initializing it)
  • Program steps into for loop 1 
for (i = 0; i < 10; i++)
		array[i] = i;
  • The Loop will run 10 times assigning ints 0-9 into array index 0-9 respectively. 
  • After "i" is no longer less than 10 the loop will exit and the program will enter the second loop
for (i = 0; i < 10; i++)
		printf("%d\n", array[i]);
  • The Loop will run 10 times printing out the value stored at array index "i"
  • Output will be:
    • 0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • Then "i" will no longer be less than 10 so it will exit the loop, hit your return and you will finish your method. 

Hopefully this is what you were looking for. 

Link to post
Share on other sites

7 minutes ago, andrew_on_tech said:

Program executes in this order.

  • So the program enters the Main method. 
  • Allocates a int and points it "i"
  • Allocates an array of INTs at size 10 with garbled data(for the most part, since your not initializing it)
  • Program steps into for loop 1 

for (i = 0; i < 10; i++)
		array[i] = i;
  • The Loop will run 10 times assigning ints 0-9 into array index 0-9 respectively. 
  • After "i" is no longer less than 10 the loop will exit and the program will enter the second loop

for (i = 0; i < 10; i++)
		printf("%d\n", array[i]);
  • The Loop will run 10 times printing out the value stored at array index "i"
  • Output will be:
    • 0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • Then "i" will no longer be less than 10 so it will exit the loop, hit your return and you will finish your method. 

Hopefully this is what you were looking for. 

Right I think thats clear now, thanks.

   
   
Link to post
Share on other sites

3 minutes ago, GR412 said:

Right I think thats clear now, thanks.

Glad I could help you out, If you have any more questions you can PM me, check out my website [andrew-on.tech], post to my [subreddit], or message me on twitter: [@andrew_on_tech] also subscribe to my [YouTube Channel] for more computer related things :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×