Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do i make i incremented along with j... only j is getting incremented and not i?

What I have tried:

#include <stdio.h>

int main()
{
    int array[5] = {2, 3, 4, 5, 6};
    int i, *ptr, j;
    for (i = j = 0; i = j < 5; j++, i++)
    {
        printf("the %d element of the array will be :%d\n", j, array[i]);
    }

    return 0;
}
Posted
Updated 11-Aug-22 5:30am

"=" is an assignment operator, not a comparison.
So quite what this is meant to do, I'm not sure:
for (...; i = j < 5; ...)

Because assignment takes precedence over greater than, that assigns the result of the comparison between j and 5 to i each time it checks if it should go round the loop.
Did you mean this?
for (i = j = 0; i < 5; j++, i++)
 
Share this answer
 
Your for statement is incorrect:
C++
for (i = j = 0; i = j < 5; j++, i++)

The expression i = j < 5 equates to:
If j is less than 5 then set i = 1
else set i = 0

So for each iteration of the loop you will print the same member of the array, i.e. array[1].

[edit]
And, of course, you do not need two pointers since you are only accessing a single array. All you need is:
C++
for (i = 0; i < 5; i++)
{
    printf("the %d element of the array will be :%d\n", i, array[i]);
}

[/edit]
 
Share this answer
 
v2
Quote:
Not able to increment both I and j

Note that your code do not need 2 variables
C++
#include <stdio.h>

int main()
{
    int array[5] = {2, 3, 4, 5, 6};
    int i;
    for (i = 0; i < 5; i++) {
        printf("the %d element of the array will be :%d\n", i, array[i]);
    }
    return 0;
}


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
i=j<5 results in i set to 1 for each loop

first: j<5 results in true
second: i = true results in i set to 1
note precedence of < is greater than precedence of =
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900