Weird For Loop !

Feb 27, 2016 at 2:18am

for ( y = 7/2;.....;y++) {
......

}
How this loop would work? This remains fractions!.
Feb 27, 2016 at 3:18am
what data type is y?
Feb 27, 2016 at 3:19am
you have to tell what data type y is either before or in the ()
like
int y;
for(y=...

or
for(int y=...
Feb 27, 2016 at 10:13am
Y is int!
Feb 27, 2016 at 10:17am
7/2 is integer division, It gives an integer result 3 regardless of the type of y.
Feb 27, 2016 at 10:31am
7/2 when division equal to 3.5 ???
Feb 27, 2016 at 10:37am
int x;

for ( x = - 5; ..;..x++ ) {
}
Feb 27, 2016 at 10:42am
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << 7/2;
}

Output:
3


Change line 5 to std::cout << 7.0/2.0; for floating-point divide.
Feb 27, 2016 at 10:50am
1
2
3
4
int x;

for ( x = - 5; ..;..x++ ) {
}

Do you have a question? By the way, you can read more about loops here:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on Feb 27, 2016 at 11:15am
Feb 27, 2016 at 11:35am
what is for negative loop variable ?
Like this :

int X;
for ( X = -5;...;X++) {
....

}
Feb 27, 2016 at 11:44am
what is for negative loop variable ?
It's just a variable which contains a value which is valid for that type of variable.

I suggest you read the description of the for loop here:
http://www.cplusplus.com/doc/tutorial/control/

Quote from that page:
for (initialization; condition; increase) statement;
In the case you mention, X = -5 is the initialization stage.
Last edited on Feb 27, 2016 at 1:02pm
Feb 27, 2016 at 1:03pm
for ( x = -5; x <= 10;x++ ) {
.........
}
what's this gong to be ?
Feb 27, 2016 at 1:32pm
Why don't you try it and see?

Then after you tried it and are not sure what it did or how or why, then feel free to ask. But you need both theory and practice here. Theory is essential. But on it's own it is a bit like the sound made by one hand clapping. Run the code... Write your own as well.
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    for ( int x = -5; x <= 10;x++ ) {
        std::cout << x << ' ';
    }
}
Feb 27, 2016 at 1:43pm
I do apologize. i will try it now! thanks.
Feb 27, 2016 at 1:48pm
I got the output from compilation.
But i have a real question now.
I have seen in a code snippet using this negative use of initialization loop.
But what the purpose of using negative value ?
Feb 27, 2016 at 1:55pm
A program is written to fulfil some need. Say you wanted to calculate using some temperature values from -5 to +10 degrees, you might do something like that. Or maybe in calculating the position or velocity of something. But those are purposes based on some real-world requirement. The purpose can be anything you want it to be.

I have seen in a code snippet using this negative use of initialization loop.
Then it depends on the context within that particular program.

If you pull a phrase or sentence from a novel and then ask what it means, it might mean nothing at all unless one had the rest of the book.
Last edited on Feb 27, 2016 at 1:57pm
Feb 27, 2016 at 2:29pm
Thanks for you reply. I appreciate it!
Feb 27, 2016 at 4:21pm
Keep in mind that in C++, the for loop is extremely generic. It can be used for lots of things, not just counting from 0 to N. The construct
1
2
3
for (expr1; expr2; expr3) {
   statements;
}

is almost identical to
1
2
3
4
5
6
7
{
    expr1;
    while (expr2) {
        statements;
        expr3;
    }
}

If I recall correctly, the only difference is that a continue statement in the for loop will execute expr3 whereis in the while loop it will not.
Feb 27, 2016 at 10:02pm
Thank you guys all for helping helping.
You are awesome. problem is solved.
Topic archived. No new replies allowed.