Does for loop in c only can have one statement? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Ruby's for/in loop can have many statement:
for a in 1..2 do
expression 1
expression 2
..
end
But it seems for loop in C can only have one:
for (a = 0; a < 10; a ++) expression 1;
Is there any way to make multiply statement in the for loop in C?

Yes, formally speaking all loop statements in C take only one statement as the loop body. The same is true for branching statements (like if) and virtually all other statements in C.
However, that one statement can be a compound one. A compound statement begins with {, ends with } and contains an arbitrary number of nested statements inside. (Note that there's no ; at the end of compound statement.)

Use braces for the body of the loop:
for (a = 0; a < 10; a++)
{
doSomething();
doSomethingElse();
}
This concept extends to other things, like if, as well. This should be mentioned right alongside the if and for themselves in any book, etc.

You need to learn C syntax. You put them in a block
for (...) {
expression 1;
expression 2;
}

Related

I can't figure out why this code works fine [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
C newbie here. Was wondering why doesn't this code throw and error. It has missing parentheses and comparison between characters.
#include <stdio.h>
int main(){
while ('a'<'b')
printf("-");
return 0;
}
'a' and 'b' are character integer constants (6.4.4.4) and comparable like any other pair of integers. That comparison is probably evaluated at compile-time leaving you with an infinite loop:
while(1) print("-");
The answer is simple, if you are not using the curly brackets to create a block of code you if's while's etc... will execute the first line of code after the condition\loop etc...
while( condition )
func1();
But this is bad practice, you can fool yourself thinking that the example below will execute in a loop the 3 lines of code, in reality it will execute inside the while just the first one:
while( condition )
func1(); //<- just this line will be executed inside the while loop.
func2();
func3();
Correct way:
while( condition )
{
func1();
func2();
func3();
}

C - struggling to understand how to use int function within if/ while loops [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I'm doing CS50 and struggling with an error that occurs when I include the following two lines:
if (int i < (number -1))
if (int i =< number)
when compiling, 'error: expected expression' comes under 'int'. Am I using it incorrectly within while if statements?
No you can not declare variable in an if statement. But if you want your variable in a local scope you can create a block and declare your variable, initialize it and use. We have to use <= for checking less than or equal to instead of =<
example:
{
int i =5,number=10;
if(i < number-1)
{
//Add your code
}
if(i <= number)
{
//Add your code
}
}
int i; is a variable declaration/definition.
That confuses your compiler when it tries to understand your if condition.
I have to guess a little, but am confident. You probably want
if (i < (number -1)){ /* assuming code here */ }
if (i <= number) { /* assuming code here */ }
Note that is also changed =< to <=.

Why not use a while loop for FIZZBUZZ in C? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Only been coding in C for about a month, but wondered why the while loop wasn't used more often for FIZZBUZZ. Is it not as clean or some other reason? Here's what I came up with:
int main()
{
int num=1;
while(num<=100)
{
if (num%3==0 && num%5==0)
{
printf("FIZZBUZZ!!!\n");
}
else if (num%3==0)
{
printf("FIZZ!!!\n");
}
else if (num%5==0)
{
printf("BUZZ!!!\n");
}
else
{
printf("%d\n", num);
}
num++;
}
return 0;
}
Your loop can be neatly folded into a for loop:
for(int num = 1; num <= 100; ++num)
There are two advantages here:
num is scoped inside the loop, when before it was bleeding into whatever scope followed the while. Restricting variables to the minimum possible scope is a good rule of thumb, because it minimizes the amount of variables to think about at any given point.
The range over which your program will operate is now summarized in a single place: you can see the bounds (1 to 100) and the step (1) inside the for. That kind of ranges is pretty well balanced to be read and memorized quickly before reading the body of the loop. For example, if you now wanted to only check odd numbers, it would be immediately clear from reading the num += 2 in the for header, rather than stumbling upon it at the very end of the loop's body.

How two individual operators are parsed/evaluated while written together? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What's really happening behind?
int a=10,b=5,c=3;
b!=!a;
c=!!a;
Why the values of b and c are 5 and 1 respectively?
b is 5 because you've assigned it that value and never changed it, since
b!=!a;
...is just a condition that you don't do anything with, not any form of assignment.
c is 1 because a is 10 and !10 is 0, and !0 is 1, thus !!a is 1 (a is 10).
You already have got the answer to your question, but just to have a deeper look into why it happens that way, let me add my two cents here.
An expression like
b!=!a;
is same as
b != !a;
because of the maximal munch rule used in the translation phase. Basically, this says that the longest possible token should be selected from the input while creating a construct (obviously, valid/ meaningful).
Following this principle the ! and = are considered together to form the perfectly valid operator != and the expression is parsed like
b != !a;
over
b ! = !a; //or anything else.
That is why, there is no assignment, as you might have thought.
That said, ! is an unary operator, which does not change the value of the operand. So, taken together, your code is essentially same as
int a=10,b=5,c=3;
c=!!a; //double negation
so, a and b are unchanged, and c is 1 (because !!10 == !0 == 1 )

Can someone help me understand how this "for" loop works? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
int main(void) {
long fall, n, k, p, i, j, r;
long long x, y, a[110][110];
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y)) {
for(i=!!scanf("%ld%ld%ld",&n,&k,&p);i<=k+1;i++)
for(j=0;++j<=i;a[i][j]=(a[i-1][j-1]+j*a[i-1][j])%p)
;
for(y=!(j=1);j<=k+1;y=(y+a[k+1][j++]*x)%p)
for(x=!((r=n%j)*!(i=-1));++i<j;x=x*(n-i)/((i==r)?j:1)%p)
;
}
return 0;
}
How does for loop work here? It doesn't follow the syntax as I see.
for loops have the following pattern:
for(initial expression; conditional expr; afterthought)
I'll break down the first loop for you, you should be able to do the rest on your own.
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y))
The initialization part of this loop is a[0][0]=scanf("%ld",&fall).
scanf is used for reading input and returns the number of input values. In this case, it will be 1 and it gets assigned to a[0][0].
fall-- is the conditional expression. In C, positive numbers are evaluated as true. So this loop will run until fall == 0.
printf("%lld\n",y) is the afterthought. It gets run after each loop iteration. In this case, it will simply print the value.
Unraveling obfuscated code can be a good learning exercise though you must obviously never use it in practice.
This code abuses the fact that the first and third conditions of the for loop does not necessarily need to have anything to do with the loop itself. At its core, the for loop simply executes an initial expression, performs the conditional check and executes the afterthought after every iteration.

Resources