The Student Room Group

Need help understanding this Nested Loop in C

#include <stdio.h>
#include <stdlib.h>
int main(){

int i, j;

for (i=1; i<=3; i++){ /* This is the outer loop*/

printf("The start of the iteration %d \n", i);

for (j=1; j<=4; j++)

printf("iteration %d of the inner loop. \n", j)
printf("The end of the iteration %d of out loop \n", i)


}
return(0);
}

I dont understand how this code is suppose to run?
First i is = 1, it should print (Start of the iteration 1)
Then moves to j=1, it should print (Iteration 1 of inner loop)
Why does this repeat 4 times before printing (The end of the iteration 1 of out loop)?
Wouldn't the inner loop, loop these two lines 4 times?
printf("iteration %d of the inner loop. \n", j)
printf("The end of the iteration %d of out loop \n", i)
Reply 1
Hey,
It's due to omitting the braces in the inner loop. It is of my understanding that if curly braces are omitted in C then only the first statement following the branch/loop is executed.

Example:

for (j = 1; j <= 4; j++)
{
printf("iteration %d of the inner loop. \n", j);
}


Will return Start Iteration 1... 4 Inner ... Start Iteration 2... 4 Inner.. Start Iteration 3.... 4 Inner

The same is true for

for (j = 1; j <= 4; j++)
printf("iteration %d of the inner loop. \n", j);

As, like I said previously only one statement after the branch/loop condition is executed on omitted braces.

--
To achieve both statements being executed
Wouldn't the inner loop, loop these two lines 4 times?


In which case you'll need to enclose both statements in the curly braces for both of them to be executed in the inner loop, and this unlikely the intention of the author.

for (j = 1; j <= 4; j++)
{
printf("iteration %d of the inner loop. \n", j);
printf("The end of the iteration %d of out loop \n", i);
}


--

As a side note, it is the programmer's personal choice whether or not they include braces if they intended only one statement for execution afterward

if (x == 1)
{
return true;
}

is the exact same as

if (x == 1)
return true;


But note, doing so can and has (google "apple ssl bug missing braces" for a very good example of this) cause unnoticed bugs or cause painful debugging :tongue:
(edited 8 years ago)
Original post by Zenarthra
#include <stdio.h>
#include <stdlib.h>
int main(){

int i, j;

for (i=1; i<=3; i++){ /* This is the outer loop*/

printf("The start of the iteration %d \n", i);

for (j=1; j<=4; j++)

printf("iteration %d of the inner loop. \n", j) <- need semicolon here
printf("The end of the iteration %d of out loop \n", i) <- need semicolon here


}
return(0);
}



The guy above is correct - note you're missing a couple of semicolons and also that it helps readability of code posted on the forum to use the code format button (which looks like a #)
Reply 3
Original post by ChazUK
Hey,
It's due to omitting the braces in the inner loop. It is of my understanding that if curly braces are omitted in C then only the first statement following the branch/loop is executed.

Example:
for (j = 1; j <= 4; j++)
{
printf("iteration %d of the inner loop. \n", j);
}
Will return Start Iteration 1... 4 Inner ... Start Iteration 2... 4 Inner.. Start Iteration 3.... 4 Inner

The same is true for
for (j = 1; j <= 4; j++)
printf("iteration %d of the inner loop. \n", j);
As, like I said previously only one statement after the branch/loop condition is executed on omitted braces.

--
To achieve both statements being executed


In which case you'll need to enclose both statements in the curly braces for both of them to be executed in the inner loop, and this unlikely the intention of the author.
for (j = 1; j <= 4; j++)
{
printf("iteration %d of the inner loop. \n", j);
printf("The end of the iteration %d of out loop \n", i);
}
--

As a side note, it is the programmer's personal choice whether or not they include braces if they intended only one statement for execution afterward
if (x == 1)
{
return true;
}
is the exact same as
if (x == 1)
return true;
But note, doing so can and has (google "apple ssl bug missing braces" for a very good example of this) cause unnoticed bugs or cause painful debugging :tongue:


Original post by Joinedup
The guy above is correct - note you're missing a couple of semicolons and also that it helps readability of code posted on the forum to use the code format button (which looks like a #)


thanks, i noticed after i posted that i missed some semi colons.
I also wanted to ask, what the difference is between say assigning values.
e.g a = 5; or a = 'a';
Or in a switch statement


int i;
switch(i){

case 1:

}

I know this is missing a lot of things, but why does it need to be case '1': and not just case 1:

Thanks.
Original post by Zenarthra
thanks, i noticed after i posted that i missed some semi colons.
I also wanted to ask, what the difference is between say assigning values.
e.g a = 5; or a = 'a';
Or in a switch statement


int i;
switch(i){

case 1:

}


Thanks.


a = 5; /* assigns the decimal number 5 to variable a */
a='a'; /* assigns the ASCII value of the character lower case a to variable a... this is decimal number 97 /*





I know this is missing a lot of things, but why does it need to be case '1': and not just case 1:


depends whether you want to test for a variable containing the decimal number 1 or the ascii code for the character '1' (which is decimal number 49)

if you've prompted the user to type a digit, what your code reads from stdin is the ascii code for the character they typed and not the decimal number 1

fwiw I like this stripped down tutorial... http://www.iu.hio.no/~mark/CTutorial/CTutorial.html
even includes an ASCII table http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Character%20Conversion%20Table
(edited 8 years ago)

Quick Reply

Latest