How to for loop iterate for two variables? [duplicate] - c

This question already has answers here:
What does the comma operator , do?
(8 answers)
Double For Loop syntax in C
(3 answers)
Closed 5 years ago.
I want to know how for loop is processed in below condition.
void main()
{
int i,j;
For(i=1,j=1;i<=5,j<=10,i++,j++)
{
printf("%d%d",i,j);
}
}
sorry for typo mistake I correct my syntax here
For(i=1,j=1;i<=5,j<=10;i++,j++)
answer of this -1122334455667788991010
How's that possible because loop for I will be iterate for only 5 times how's that possible ?
I want to know how loop will be executed ?

This won't compile, there's only one ; in the for which is a syntax error.
I'll assume it should read like this:
for(i=1, j=1; i<=5, j<=10; i++, j++)
then it would step both i and j to 10.
This is because the for-loop's middle part, the condition, reads i<=5,j<=10 which is a use of the comma operator where perhaps a boolean and (&&) would be better.
It will evaluate i<=5, throw away that result, and then evaluate j<=10, running the loop for as long as that value is non-zero.

#include <stdio.h>
int main(int argc, char** args){
for(int i = 0, j=0; i<10&&j<10; i++, j++){
printf("%d, %d\n", i, j);
}
}
The semi colon's seperate the terms of the for statement. (intializer; condition; action at end of loop) You can do what you like for the sections.

Related

--a vs a--, operator precedence [duplicate]

This question already has answers here:
What is the difference between ++i and i++?
(20 answers)
Closed 5 years ago.
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
void recursion (int a) {
if (a != 0) {
recursion(--a); //works
recursion(a--); //does not work
printf("%d\n", a);
}
}
int main (int argc, char *argv[]) {
printf("start\n");
recursion(10);
printf("finished\n");
return 0;
}
Why is there a segmentation fault when I recurse (a--) but works fine when I recurse (--a)?
I don't think recursion(a--) is wrong due to undefined behavior because there is only one side effect, which is to decrease a by 1. This side effect is exactly what I wanted. Thanks.
Both --a and a-- have the side effect of incrementing a. The difference is that the value of the expression --a is the value of a after decrementing, while the value of a-- is the value of a before decrementing.
So in the latter case the same value of a is passed recursively to the function. As a result, you have an infinite recursive loop which causes a stack overflow.
You need to use recursion(--a) for the recursive call in order for the decremented value of a to be passed to the function.

Why doesn't my program print the right value after 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 7 years ago.
Improve this question
I am trying the below C code:
#include <stdio.h>
int main()
{
int i=10;
int start=25;
int end = 30;
for(i = start; i < end; i++);
{
printf("%d\n", i);
}
}
I know there is a semicolon at the end of for loop which stops the for loop iterating more than once. But I get the output '30', instead of '25'. Why am I getting this result? Variable i should retain its start value, right?
for(i = start; i < end; i++);
That code means that i is 25. The loop continues while i<30.
Then after the loop, i will be, as you wrote, 30.
Putting a semicolon at the end of loop does not stop the loop: it is executed.
In your case {} only open a different scope inside the main function.
The snippet
for(i = start; i < end; i++);
{
printf("%d\n", i);
}
is equivalent to
for(i = start; i < end; i++);
printf("%d\n", i);
Expanding further
for(i = start; i < end;)
{
i++; // This statement will execute till i < 30
}
printf("%d\n", i);
The condition i < end will evaluate to false when i will be incremented to 30 by the execution of the statement i++ inside the loop body.
Now, the statement printf("%d\n", i); will be executed and will print the value of i which is 30.
I know there is a semicolon at the end of for loop which stops the for loop iterating more than once.
That is not exactly right: semicolon after for loop becomes loop's empty body. The loop iterates five times, incrementing i in each iteration. The statement in curly braces gets executed only when the loop is over.
At that point i becomes 30, because that is your loop's post-condition (i.e. the condition that must be true in order for the loop to finish). That's what gets printed by the printf.
for(i = start; i < end; i++);
This loop will iterate for 5 times , until i becomes equal to end i.e 30. It will not stop after 1st iteration .
Due to ; the following printf is not part of loop body , but ; doesn't mean that loop will iterate for 1 time . It will iterate til condition is true but loop's body does not contain anything other than ;.
Because of the colon your program is now equivalent to:
#include <stdio.h>
int main()
{
int i=10;
int start=25;
int end = 30;
for(i = start; i < end; i++)
{
//do nothing
}
printf("%d\n", i);
}
Your loop moves i to 30 and then prints it.
The code within the braces (below for) is not part of your for loop.
In other words your loop does not have a body due to the inclusion of the semicolon at the end of the for statement.

What for (;;) and while(); mean in C [duplicate]

This question already has answers here:
What does "for(;;)" mean?
(5 answers)
Closed 4 years ago.
I am looking at some example codes and I saw someone did this
for (;;) {
// ...
}
Is that equivalent to while(1) { } ?
And what does while(condition); do? I don't get the reason behind putting ';' instead of {}
yes,
for(;;){}
is an infinite loop
And what does while(condition); do? I don't get the reason behind putting ';' instead of {}
Well, your question is what happens if you put or you do not put a semicolon after that while condition? The computer identifies the semicolon as an empty statement.
Try this:
#include<stdio.h>
int main(void){
int a = 5, b = 10;
if (a < b){
printf("True");
}
while (a < b); /* infinite loop */
printf("This print will never execute\n");
return 0;
}
for(;;) and while(1) are both infinite loops, and compile to the same opcodes:
L2:
jmp L2
Which means there is no speed difference, as the disassembly is exactly the same.
while just loops though a single statement until the condition is false. It doesn't have to be a compound statement (this thing: {}), it can be any statement. ; is a statement that does nothing.
while(getchar() != '\n');
will loop until you hit enter, for example. Though, this is bad practice since it will hog the thread; adding a call to a sleep method in the loop is better.

Why is the compiler showing warning of following if condition to be always true? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
#include<stdio.h>
int main()
{
int a=2;
if(a==3,4)
printf("hello");
return 0;
}
Warning: Condition is always true
Why is it always true??
The , doesn't work like you think it does.
What a , does is evaluate all the expressions that are separated by the , in order, then return the last.
So what your if statement is actually doing is checking a==3 which returns false, but it discards this result. Then it checks if(4), which returns true.
Essentially your code is:
#include<stdio.h>
int main()
{
int a=2;
if(4)
printf("hello");
return 0;
}
a==3,4
should be
a==3.4
Decimal are symbolised with a dot (.) and not a comma(,).
A comma here splits instructions, just as it does in a for statement:
for(int a=0, int b=10 ; b<=0 ; a++,b--)

C-Array output is different than expected for same printf() [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I'm attempting to do a "find the bug" assignment, and this small bit of C code (not runnable as is) is supposed to have a bug in it. Unforunately I can't find one. I put it into runnable code and it seems to run as expected...is this a trick question or am I missing something? Any help is greatly appreciated.
Code snippit:
void example(int a[10]){
int i = 0;
do {
a[i] = i++;
}while (i < 10);
}
My runnable code:
#include <stdio.h>
#include <string.h>
example(int a[10]){
int i = 0;
do {
a[i] = i++;
printf("%i", a[i-1]); //decremented by 1 to offset i
}while (i < 10);
}
int main(void)
{
int arr[10];
example(arr);
return 0;
}
Output:
0123456789
This is wrong and invokes undefined behavior:
a[i] = i++;
There is no sequence point specified for the assignment, increment or index operators, you don't know when the effect of the increment on i occurs.
Take a look to Question 3.1 of C-FAQ

Resources