I have for loop doubt that I need to ask .
once i saw in coding something like
for(i = 0; i<10; i+)
My doubt is why &when in for loop we use say i+ or i- rather than i++ or i--
Thanks in advance
It won't work, doesn't the compiler return an error if you do? ( or atleast a warning.. )
Just use ++i or i++
Using i+ instead of i++ should not work. As I think you know, i++ increases the value of i by one. When the compiler sees i+, it is expecting something after the +, which causes it to not compile.
The line in question is not valid C
for(i = 0; i<10; i+)
Some valid, equivalent (between themselves) options are
for(i = 0; i < 10; i++)
for(i = 0; i < 10; ++i)
for(i = 0; i < 10; i += 1)
for(i = 0; i < 10; ) { /*...*/ i++; }
You cannot use i+ or i- because if doesn’t work i.e doesn’t increase or decrease value of i for increment you have may use i++,++i,i+=1,i=i+1 and for decrement you can use these by changing sign to negtive
Related
for (int i = 0; i < n+1; ++i)
{
sum = sum + i;
}
for (int i = 0; i < n+1; i++)
{
sum = sum + i;
}
Two paragraphs are different because of ++i and i++ in function call argument.
but it works like i only starts with 0. Why does even ++i starts with 0?
There are absolutely no difference between these two snippets. i++ vs ++i only matters when mixed with other operators in the same expression. Which is a bad idea most of the time, since i++/++i comes with a side effect.
A generic for loop like
for (a; b; c)
{
d
}
is equivalent to
{
a;
while (b)
{
d;
c;
}
}
Note how the "increment" expression c is after the main statement of the loop body.
For your loops that means they will be equivalent to:
{
int i = 0;
while (i < n+1)
{
sum = sum + i;
i++; // or ++i
}
}
Since the increment of i doesn't happen until after you calculate sum there's no practical difference between the loops. Both will lead to the exact same result.
On a side-note: Remember to explicitly initialize sum to zero before the loop, or it might have an indeterminate value (that could be seen as garbage).
The two for loops you posted will behave exactly the same. The reason they are equivalent, which also answers your question of why both loops start from 0, is that the increment (i++ or ++i) only happens after the {expression} in {} has executed, and after every time it has executed.
Your code would produce identical results.
The following code would produce different results.
int i = 0;
while (i < 5) {
printf("%d\n", ++i);
}
// 1
// 2
// 3
// 4
// 5
i = 0;
while (i < 5) {
printf("%d\n", i++);
}
// 0
// 1
// 2
// 3
// 4
In my example the reason the bottom for-loop prints out 0 and the top for-loop doesn't is because printf and i++/++i form a combined expression where in the top i is incremented and then accessed from memory whereas in the bottom loop i is access from memory and then incremented.
The i++ and ++i expressions in the last clause of these for loops both increment i as a side effect and since the value of the expression is not used, they have exactly the same effect, namely the side effect.
Here are other alternative, all of which behave the same and should produce the same code:
for (int i = 0; i < n+1; i += 1) {
sum = sum + i;
}
and
for (int i = 0; i < n+1; i = i + 1) {
sum = sum + i;
}
Note however that if n is an int with the value INT_MAX, n+1 has undefined behavior. A safer way to write this loop is:
for (int i = 1; i <= n; i++) {
sum = sum + i;
}
But the <= operator has the same problem when n is INT_MAX: the undefined behavior would occur on the i++ part when i reaches INT_MAX...
Here is an alternative that avoids this corner case:
if (n > 0) {
for (int i = 1; i < n; i++) {
sum = sum + i;
}
sum = sum + n;
}
I'm just wondering how does boundary checking works in the following case.
#include <stdio.h>
#include <stdint.h>
int main(void)
{
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (int x = 0; x < 3; x++) {
printf("%d < %d < %d is %d\n", i, x, j, i < x < j);
}
}
}
return 0;
}
I tried right-to-left/left-to-right precedence but it doesn't seem to work. (not too sure about it though, maybe it does?)
I think this is undefined behaviour and using && would probably be easier than figuring this out but I'm quite interested in how the logic behind this works. Would appreciate if anyone could help explain this or point me towards the right direction
edit: Thanks for all the help! got the answer I needed. This must seem completely trivial to all of you but it really made many things I thought about in the past few days clicked. Really appreciate the time you guys took to help me out. Thanks again!
i < x < j is perfectly well-defined, but it doesn't do what you think it does.
It's equivalent to (i < x) < j, due to the associativity of <.
But (i < x) is either 0 or 1, as that's how the relational operators are defined in C.
So, for example, if j is greater than 1, then i < x < j is always 1.
Consider writing i < x && x < j instead.
In this loop,:
for( i = 0; i < N>>1; i++)
does the N value shift throughout every repetition? i.e. if N=1024, then the first loop does
for( i = 0; i < 512; i++)
and the next loop does
for( i = 0; i < 256; i++)
It does not. If you want to do that, then a construct like the following would do that:
for( i = 0; i < N; N >>= 1, i++)
Or put the shift inside of your loop. The shift operator by itself doesn't change the value of the operand without an assignment statement.
No, you're shifting the value ofN, but you aren't assigning it a new value, so it doesn't change. Every iteration would be equivalent to this:
for( i = 0; i < 512; i++)
When iterating through an array how do you compare the current element to the previous one? This is easy except for the caveat the first element has no previous one. Is the best solution
for(i = 0; i < arrLen; i++)
{
arr[i] = process(i, someArg);
if(i > 0)
someFunc(arr[i], arr[i-1]);
}
This is one more comparison that needs to be performed for each element of the array which seems wasteful.
Sorry I forgot to say the array is being populated at the same time. So starting the loop at 1 would mean the first element is left empty.
Just start the loop at 1:
for (int i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1]);
Edit: Given your new loop, I think your code is OK. Your optimizer will likely handle it just fine. If you're really concerned, just do the first assignment outside the loop:
arr[0] = process(0, someArg);
for(i = 1; i < arrLen; i++)
{
arr[i] = process(i, someArg);
someFunc(arr[i], arr[i-1]);
}
If you are going to do that, just start the counter at 1.
for (int i = 1 ...)
If you are doing it the other way, with an i+1, end the counter 1 sooner
for (int i = 1; i < arrLen -1 ; ++i)
i quess you have for in actual code
for( int i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1]);
or
for( int i = 0; i < arrLen -1; i++)
someFunc(arr[i], arr[i+1]);
You just need to start iteration not from 0 but from 1. It is because 0-1=-1 (illegal array index)
for (int i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1]);
And assignment of zeroth element should be done before loop:
arr[0] = process(0, someArg);
for(i = 1; i < arrLen; i++)
{
arr[i] = process(i, someArg);
someFunc(arr[i], arr[i-1]);
}
And also you may need to check if there is need to assign zeroth element so code should be:
if (arrLen)
{
arr[0] = process(0, someArg);
for(i = 1; i < arrLen; i++)
{
arr[i] = process(i, someArg);
someFunc(arr[i], arr[i-1]);
}
}
NOTE: in this way you don't need to use if condition because i always positive.
Start your loop at 1 instead of zero, then you will always have an i-1
for(i = 1; i < arrLen; i++)
someFunc(arr[i], arr[i-1])
EDIT:
Given your new information, I'd say just leave it with the if statement as you have it. I don't think there is a particularly better way. You could initialize a prevVal variable outside of the loop, but I don't think you'd really gain a lot over the if i > 0 statement you have.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I found a site with some complicated C puzzles. Right now I'm dealing with this:
The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.
#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}
Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.
I cannot figure out how to solve. I know that it can be fixed by changing -- to ++, but I can't figure out what single character to change to make it work.
Here is one solution:
for( i = 0; -i < n; i-- )
printf("-");
Here is a second one, thanks to Mark for helping me!
for( i = 0; i + n; i-- )
printf("-");
And Mark also had the third one which is
for( i = 0; i < n; n-- )
printf("-");
Change i-- to n-- is another.
Okay - Gab made the fix, so I removed the other solution. He wins!
Third answer:
for( i = 0; i + n; i-- )
printf("-");
Thanks to Gab Royer for inspiration.
Explanation: Eventually , i + n will result in -20 + 20 = 0 which is false.
for( i = 0; i < n; n-- )
printf("-");
Changed i-- to n--
Here's one of them, I think:
for( i = 0; i < n; n-- )
The comparison in the for loop can be any expression - you can negate i.
for (i = 0; -i < n ; i--)
Solution 1
#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; n-- ) // Change i-- to n--
printf("-");
return 0;
}
Solution 2
#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; -i < n; i-- ) // Compare to -i
printf("-");
return 0;
}
Haven't figured a third.
Here is another one:
#include <stdio.h>
int main()
{
int i;
int n = -20; //make n negative
for( i = 0; i < n; i-- )
printf("-");
return 0;
}