## playing with for loops

### playing with for loops

I was looking over some code that I’ve been pulling out of my ass for an assignment and for some reason this idea jumped out at me me.
Try and guess what this code does in 5 seconds
Code: Select all
int k = 50, i;
for(i = 0; i <= --k; i ++){}
std::cout << i;
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
My crime is that of outsmarting you, something that you will never forgive me for.
Damn kid.

pretentious
Poster

Posts: 396
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)

### Re: playing with for loops

pretentious wrote:
Code: Select all
int k = 50, i;
for(i = 0; i <= --k; i ++){}
std::cout << i;

Well, you already have k defined at 50, i will be defined at 0.
Each time the loop executes, you will be subtracting 1 from k, and also adding 1 to i.

This would leave the conclusion that it would add up to half of the given number of k rounded down.

i = 0, i < (k) 50 echo 0
i = 1, i < (k) 49 echo 1
i = 2, i < (k) 48 echo 2
This process will happen until k finally reaches 25, and i will be equal to k, the for loop then stops executing.

Pretty simple, but at first look, I didn't figure this out in 5 seconds.
Spreading knowledge just once a day, can help keep the script kiddies away ⠠⠵
The mystery is intensified by the facts.

-Ninjex-
Contributor

Posts: 951
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: playing with for loops

-Ninjex- wrote:
Well, you already have k defined at 50, i will be defined at 0.
Each time the loop executes, you will be subtracting 1 from k, and also adding 1 to i.

This would leave the conclusion that it would add up to half of the given number of k rounded down.

i = 0, i < (k) 50 echo 0
i = 1, i < (k) 49 echo 1
i = 2, i < (k) 48 echo 2
This process will happen until k finally reaches 25, and i will be equal to k, the for loop then stops executing.

Pretty simple, but at first look, I didn't figure this out in 5 seconds.

Actually, if you'll notice, there is nothing in the braces, so it will only echo out the end result of i (25).

And yes, I figured it out in 5 seconds.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook

centip3de
Moderator

Posts: 1358
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)

### Re: playing with for loops

centip3de wrote:Actually, if you'll notice, there is nothing in the braces

See, that's why I shouldn't be trying to do shit in five seconds.
Spreading knowledge just once a day, can help keep the script kiddies away ⠠⠵
The mystery is intensified by the facts.

-Ninjex-
Contributor

Posts: 951
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: playing with for loops

I suppose no one else had an aha moment. I've been working with c style languages for years and never considered the idea of having a changing comparison variable in a loop.
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
My crime is that of outsmarting you, something that you will never forgive me for.
Damn kid.

pretentious
Poster

Posts: 396
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)

### Re: playing with for loops

pretentious wrote:I suppose no one else had an aha moment. I've been working with c style languages for years and never considered the idea of having a changing comparison variable in a loop.

Yeah, not really
Figure this out in 5 seconds

Code: Select all
#!/usr/bin/ruby
@x = 1
@i = 0
while(@x > @i)
while(@i < @x)
@i = @x+1
puts "inner"
end
@i = @x-1
puts "outter"
end
Spreading knowledge just once a day, can help keep the script kiddies away ⠠⠵
The mystery is intensified by the facts.

-Ninjex-
Contributor

Posts: 951
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: playing with for loops

for the first 5 seconds, i though it was perl and was like, Arrays? man, what are you trying to do to me?
I ran the code and am still not really sure what to make of it haha
Off topic, who's idea was it to make '@' required for identifying variables?
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
My crime is that of outsmarting you, something that you will never forgive me for.
Damn kid.

pretentious
Poster

Posts: 396
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)