Explain this for loop [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
int i; for ( i = 0; i = 3; i++ ) printf("Hello");
In this C code, the loop condition is "i = 3". What does this condition mean? And the loop runs infintely. Does that mean the condition is true?

Your test part in your for loop is i = 3 which is an assignment to the variable. After assignment it evaluates as the which has been assigned: 3 which is not 0 hence true in C.
Then the condition of your for loop is always hence you have an infinite loop.
If the condition was i == 3, it would be false at the first iteration of the loop. hence the code will never execute the body of the for block.
If the condition was i < 3, it would be true until the i variable is incremented up to the 3 value. Hence the body of the for loop would execute 3 times

Related

This code should print i=1 but it is printing i=0 Why is it so? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
#include<studio.h>
int main()
{
int a=5,i;
i!=a>10;
printf("i=%d",i);
return 0;
}
This code should print i=1 but it is printing i=0 Why is it so?
That's because you don't do anything to i.
Your "i!=a>10" evaluates to false, but the result is not stored into a variable.
As it is mentioned in the comments, you need something like this:
int a = 5;
int i = !(a > 10);
The != is mostly used in if-clauses, like
if (a != 0) {...}
I hope this helps. ;)
It's not printing because:
the variable i is not initialized
the second statement should be changed and be a variable int i = !(a > 10)
Returning a value and printing it are different things, but that's not the point of the question.

Loop program output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I got curious and experimented a little on looping and I kind of confused myself here.
Question 1:
Why does this code output 012345678910 instead of 00000000000111111111112222222222233333333333444444444445555555555566666666666777
7777777788888888888999999999991010101010101010101010 ?
for(a=0;a<=10;a++){
for(a=0;a<=10;a++){
printf("%d", a);
}
}
Question 2: For the code shown below, does taking out the first statement mean that it would start on 0? If yes, then why does it output 12345678910 instead of 01234567891001234567891001234567891001234567891001234567891001234567891001234567
8910012345678910012345678910012345678910012345678910 ?
for(a=0;a<=10;a++){
for(;b<=10;b++){
printf("%d", b);
}
}
Because when a reaches 10 in the inner loop, the outer loop also exits, so you only see
012345678910
Because after the 1st time the outer loop is executed, b is already 11 and the inner loop is no longer executed.
For your desired output, you should reset b to zero every time the outer loop is executed
for (...) {
for (b = 0; ... ; ...) {...}
// ^~~~~ This is what you should do
}
For the first one, you are not getting the answer because you use same counter variable in inner loop:
Modify it to use different variable:
for(int a=0;a<=10;a++){
for(int b=0;b<=10;b++){
printf("%d", a);
}
}
You have same counter for outer and inner loop which is in this case incorrect. Use counter a and b then loop should work.
In inner loop b stays 11 after first iteration, if you want to achieve your expected result add b = 0 in your inner loop

Stumped: for loop to build an array not working, initial condition ignored? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This one has me stumped. Here is my code to build an array, b[i] of doubles, from 0 to N where N = 126.
int N = 126;
double b[N];
int i;
for(i = 0; i < N; i++);
{
b[i] = (double)i;
printf("b[%lf] = %d\n",b[i], i);
}
For some reason, this is what I get:
b[126.000000] = 126
and nothing else. The initial condition of i being at 0 is ignored, and for some reason it sets i to be the value of N. Strange!
I'm a bit of a c novice so I must be missing something obvious. Any help greatly appreciated!
Andy.
The mistake is at you using the ; at the end of the for loop statement. That is why the program is simply executing the remaining statements as if they are in no loop, and at that time i has become 126.
Remove the ; on the end of the for loop, it is running through the loop without doing anything then executing the body for the last value of i(which is N = 126)

Output Of a While Loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
How many times the while loop is executed in the below prog if short int is of 2 bytes?
main()
{
int j = 1;
while(j <= 255);
{
printf("%d",j);
j++;
}
return 0;
}
i think it should be 255 times but its not correct. Can anyone tell me why?
You have a semicolon at the end of your while-line. The while loop, consisting of the statement ;, executes "infinitely" many times.

Loop iteration in c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i,x=10;
for(i=0;i<7;i++);
{
x++;
}
printf("%d",x);
}
Output : 11
No matter how many times the for loop iterates, the value of x stays 11. Why is that ?
Remove the semicolon from here:
for(i=0;i<7;i++);
The semicolon makes the for loop have an empty body. It makes it equivalent to
for(i=0;i<7;i++){}
Including warning flags in your compiler(-Wextra in GCC) emits a warning about these kind of issues.
Semicolon (; ) punctuation mark in C means that block of code is finished. That means if you use
for(i=0;i<7;i++);
{
x++;
}
For loop ends before it reaches brackets. Then code between your brackets runs like normal lines out of loop. If you want your loop to include brackets, get rid of the semicolon, like:
for(i=0;i<7;i++)
{
x++;
}

Resources