Loop iteration in c [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 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++;
}

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.

How to correctly format a "for" 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 2 years ago.
Improve this question
I'm quite new to programming and I'm trying to make a simple program with a loop:
#include <stdio.h>
int a = 5;
int b = 0;
int main(void)
{
for (int i = 0; i < a, i++)
{
b++;
}
printf("%i", b);
}
However, when I try to compile I get the errors: relational comparison result unused [-Werror,-Wunused-comparison] and expected ';' in 'for' statement specifier for line 8. I've tried to look at several different sources on how to construct for loops and I just can't see what I'm doing wrong. Any help would be much appreciated.
Here's an example where you can practice interpreting the error statement. As you'll see, it says expected ';' in 'for' statement specificer. That's saying there's a place where you should have a semi-colon but you don't.
In your casse, there should be a semi-colon after the i < a. Right now, you have a comma.
I think you are using , instead of ; in the for loop after the statement.
for (int i = 0; i < a; i++)

i cannot compile my code because of this error message: mario.c:5:1: error: expected identifier or '(' { ^ [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 2 years ago.
Improve this question
The top curly brace under int main(void) is what keeps getting flagged. The curly-brace cannot be the real issue because I know the entire code needs to be in between the curly braces, so it must be something else. I have tried deleting and replacing the braces, I have checked the other code to make sure all the loops have braces as needed... I don't get it. I have gone out and back int the program a few times over several days.
Here is the part of my code near the error message:
# include <cs50.h>
int main(void);
{
int h;
// prompt user until user enters an integer from 1 to 8
do
{
h = get_int("hight: \n");
}
while (h < 1 || h > 8);
You have a semicolon on
int main(void);
Remove the semicolon and you should be golden.
int main(void) {

My c program is not giving any result.pls help everyone [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 6 years ago.
Improve this question
i have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.

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.

Resources