playing with for loops

playing with for loops

Post by pretentious on Mon Oct 21, 2013 1:18 am
([msg=77796]see playing with for loops[/msg])

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 ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 667
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: playing with for loops

Post by -Ninjex- on Mon Oct 21, 2013 2:44 am
([msg=77799]see Re: playing with for loops[/msg])

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.
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1336
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: playing with for loops

Post by centip3de on Mon Oct 21, 2013 10:49 am
([msg=77804]see Re: playing with for loops[/msg])

-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
User avatar
centip3de
Moderator
Moderator
 
Posts: 1426
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: playing with for loops

Post by -Ninjex- on Mon Oct 21, 2013 4:21 pm
([msg=77814]see Re: playing with for loops[/msg])

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.
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1336
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: playing with for loops

Post by pretentious on Tue Oct 22, 2013 2:23 am
([msg=77818]see Re: playing with for loops[/msg])

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 ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 667
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: playing with for loops

Post by -Ninjex- on Tue Oct 22, 2013 7:02 am
([msg=77827]see Re: playing with for loops[/msg])

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 :P
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
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1336
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: playing with for loops

Post by pretentious on Tue Oct 22, 2013 11:56 pm
([msg=77838]see Re: playing with for loops[/msg])

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 ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 667
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests