Zenarthra
Badges: 3
Rep:
?
#1
Report Thread starter 4 years ago
#1
#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)
0
reply
ChazUK
Badges: 0
Rep:
?
#2
Report 4 years ago
#2
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:
Code:
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
Code:
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.
Code:
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
Code:
if (x == 1)
{ 
return true;
}
is the exact same as
Code:
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
0
reply
Joinedup
Badges: 20
Rep:
?
#3
Report 4 years ago
#3
(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 #)
0
reply
Zenarthra
Badges: 3
Rep:
?
#4
Report Thread starter 4 years ago
#4
(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:
Code:
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
Code:
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.
Code:
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
Code:
if (x == 1)
{ 
return true;
}
is the exact same as
Code:
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
(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.
0
reply
Joinedup
Badges: 20
Rep:
?
#5
Report 4 years ago
#5
(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.
Code:
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...ersion%20Table
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • The University of Law
    Open Day – BPC, GDL, LPC and MA Law - Bristol campus Postgraduate
    Sat, 18 Jan '20
  • Norwich University of the Arts
    Postgraduate Open Day Postgraduate
    Thu, 23 Jan '20
  • SOAS University of London
    Development Studies, Interdisciplinary Studies, Anthropology and Sociology, Languages, Cultures and Linguistics, Arts, Economics, Law, History, Religions and Philosophies, Politics and International Studies, Finance and Management, East Asian Languages & Cultures Postgraduate
    Sat, 25 Jan '20

How many universities have you heard back from?

0 (67)
15.16%
1 (59)
13.35%
2 (57)
12.9%
3 (91)
20.59%
4 (90)
20.36%
5 (78)
17.65%

Watched Threads

View All