• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 06:44
CET 12:44
KST 20:44
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
BGE Stara Zagora 2026 announced2[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3RSL Season 3: RO16 results & RO8 bracket13Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge2
StarCraft 2
General
BGE Stara Zagora 2026 announced SC: Evo Complete - Ranked Ladder OPEN ALPHA When will we find out if there are more tournament Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge
Tourneys
Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle [Alpha Pro Series] Nice vs Cure RSL Revival: Season 3 $5,000+ WardiTV 2025 Championship
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death
Brood War
General
Which season is the best in ASL? soO on: FanTaSy's Potential Return to StarCraft BGH Auto Balance -> http://bghmmr.eu/ Data analysis on 70 million replays sas.vorti stream
Tourneys
[Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL21] RO16 Tie Breaker - Group B - Sun 21:00 CET [BSL21] GosuLeague T1 Ro16 - Tue & Thu 22:00 CET
Strategy
Game Theory for Starcraft How to stay on top of macro? Current Meta PvZ map balance
Other Games
General Games
Nintendo Switch Thread The Perfect Game Stormgate/Frost Giant Megathread Beyond All Reason Should offensive tower rushing be viable in RTS games?
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Mafia Game Mode Feedback/Ideas
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread Artificial Intelligence Thread YouTube Thread Things Aren’t Peaceful in Palestine
Fan Clubs
White-Ra Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
Esports Earnings: Bigger Pri…
TrAiDoS
Thanks for the RSL
Hildegard
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1581 users

The Big Programming Thread - Page 202

Forum Index > General Forum
Post a Reply
Prev 1 200 201 202 203 204 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Snuggles
Profile Blog Joined May 2010
United States1865 Posts
November 26 2012 04:00 GMT
#4021
Did my professor just indirectly call my code a piece of crap? It was just a math computation code to figure out the calculation of a bunch of square roots to 625, you'll see what I mean when you see the code (python btw).

My writing
# TN
# 5.37

import math

# set variables
uno = 1
dos = 2

i = math.sqrt(uno)
k = math.sqrt(dos)
counter = 625

total = 1 / (i / k)

# loop

while counter > 0 :
uno += 1
dos += 1
total += 1 / (i / k)
counter -= 1

print('The sum of the computation is','%.01f' % total)



Professor's code

# DZ
# Summation

# SETUP
import math
total = 0

# NO INPUT, JUST COMPUTE
for i in range (1, 625):
total += 1 / (math.sqrt (i) + math.sqrt (i + 1))

# OUTPUT
print (total)


"(5.37) Everything but the loop is incorrect." is the exact e-mail I got from him.
Craton
Profile Blog Joined December 2009
United States17274 Posts
Last Edited: 2012-11-26 04:22:44
November 26 2012 04:10 GMT
#4022
There's a difference between writing good code and writing code that works. Python especially tends to be about succinctness and simplicity, whereas your code has many unnecessary variables and lines.

You've over-complicated something that should be a simple bit of code and made it harder to read and maintain than necessary. Additionally, you don't gain any forward-looking functionality/expandability out of your solution that would warrant the maintainability/readability cost.

All of your variables are poorly named, to boot. They all have no meaning about how they're used or what they contain. i and k are typically used as increment variables, but you've halfway used them as temporary storage for calculated values.

Also, while I don't know Python, I don't believe your logic inside the loop would work. The first time you use i and k you call the math.sqrt of the function on them, but every subsequent time inside the loop you're simply using the same i and k (look at your code and tell me where you do anything to i or k inside your loop or where you use uno or dos after incrementing).

Basically, your loop doesn't do what you want.
twitch.tv/cratonz
frogmelter
Profile Blog Joined April 2009
United States971 Posts
November 26 2012 04:33 GMT
#4023
Snuggles:

Variables i and k are never updated within the loop. Therefore the same value is being added to total at every iteration.

Your professor's code has a for i in range (1, 625) loop, where i will increase by one after every iteration. Therefore, the value that will be added to total will be different every iteration.

Those are the main differences between your code and his code.

If you moved

i = math.sqrt(uno)
k = math.sqrt(dos)


into the loop it would probably work.

However, you wrote a lot of code to emulate the style of a for loop. Why not just use a for loop directly?
TL+ Member
Lukaskr
Profile Joined December 2011
Poland9 Posts
Last Edited: 2012-11-26 06:58:02
November 26 2012 05:09 GMT
#4024
Was wondering if anyone could help me with HW on discrete math. My professor is seriously on crack, HW averages are like 55%.

Ok, so first of all if someone could explain what does it mean to use a combinatorial proof to prove something. I googled about it, but honestly it was really hard to understand and see how it applies to my problem.

In first part of her question it is like this,
Use binomial theorem to prove:
C(n,0)+C(n,1)2+C(n,2)2^2+...+C(n,n)2^n=3^n

So I recognized it as binomial theorem, of a=2,b=1, substitued left side of the equation for
(2+1)^n and said (2+1)^n=3^n

How can I prove the same thing using combinatorial counting argument?



Not rly programming but Discrete Math is at the very foundation of computing...
If you do not think there is anything worth dying for, you have nothing to live for.
Craton
Profile Blog Joined December 2009
United States17274 Posts
November 26 2012 05:30 GMT
#4025
This is explicitly a thread where you don't ask "here's my homework, help"

If you want help, figure out things yourself and then ask for help on specific sticking points.
twitch.tv/cratonz
AmericanUmlaut
Profile Blog Joined November 2010
Germany2581 Posts
Last Edited: 2012-11-26 13:09:27
November 26 2012 13:09 GMT
#4026
On November 26 2012 13:00 Snuggles wrote:
Did my professor just indirectly call my code a piece of crap?

Yes, and you should write him a thank you note for letting you know in such a friendly manner. Computer programming is really hard, and if you're going to succeed at it, you need a much better developed sense of perfectionism than that function demonstrates. I can see that it's just a beginner project, so it's completely fine that your code is awful, but it's also important that you understand that the code is wrong both logically and stylistically, and demonstrates that you're not thinking about how to write a program the right way.

So the first problem is, your program is wrong. What was your testing process? I'm guessing you didn't test your result for correctness at all, or you would have noticed the error. Testing the actual calculation would be a lot of work, so my testing process would have been something like: Replace the 625 with 5, work out the answer by hand, see if your code produces the right answer for 5. Once you get it working for 5, test it again with another value. Once you know that the function generates the correct values for a set of testable numbers, you can deduce that it should work for larger numbers, as well.

The second problem was already pretty well explained by everyone else: Your code has a whole bunch of extra bits and doodads that are completely unnecessary, and your variable names are meaningless. This sort of criticism is something that beginners in CS tend to think is just crotchety old people being crotchety, because when you're starting out you write programs that are maybe two pages of code, and you have to "maintain" them for the weekend it takes you to write them. I work on projects with lifetimes around ten years and tens to hundreds of thousands of lines of code, and I have been in the unenviable position of having to fire people who thought that writing legible and maintainable code was a waste of time, because I don't have time to figure out what "uno" meant to someone else three years ago, and he probably doesn't remember anymore, either.

Don't get discouraged, though. Everyone wrote shitty code in the beginning. The important thing is that you understand what it is that makes your code bad, and focus on not making the same mistakes in your next project.
The frumious Bandersnatch
heishe
Profile Blog Joined June 2009
Germany2284 Posts
Last Edited: 2012-11-26 14:10:11
November 26 2012 14:06 GMT
#4027
On November 26 2012 22:09 AmericanUmlaut wrote:

Don't get discouraged, though. Everyone wrote writes shitty code in the beginning. The important thing is that you understand what it is that makes your code bad, and focus on not making the same mistakes in your next project.


I fixed this statement, because I think it's important for a beginner to know that there is probably not a single programmer in the world, no matter how experienced or intelligent, that doesn't write shitty code sometimes for legitimate reasons (e.g. they just can't think of better ways to do it at that specific moment).

Programming is just too complex for that. Arguably it's one, if the not the, most complex engineering field out there. The degrees of freedom of any software application is just larger than those that you'd potentially find in other fields, and to make it worse, math doesn't help to provide clear cut answers most of the time.
If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse.
AmericanUmlaut
Profile Blog Joined November 2010
Germany2581 Posts
November 26 2012 14:38 GMT
#4028
On November 26 2012 23:06 heishe wrote:
Show nested quote +
On November 26 2012 22:09 AmericanUmlaut wrote:

Don't get discouraged, though. Everyone wrote writes shitty code in the beginning. The important thing is that you understand what it is that makes your code bad, and focus on not making the same mistakes in your next project.


I fixed this statement, because I think it's important for a beginner to know that there is probably not a single programmer in the world, no matter how experienced or intelligent, that doesn't write shitty code sometimes for legitimate reasons (e.g. they just can't think of better ways to do it at that specific moment).

Programming is just too complex for that. Arguably it's one, if the not the, most complex engineering field out there. The degrees of freedom of any software application is just larger than those that you'd potentially find in other fields, and to make it worse, math doesn't help to provide clear cut answers most of the time.

I salute you, sir, for you are very, very correct. I have, in fact, not yet reached a point where I wasn't embarassed to read my own code half a year or so after I wrote it: You learn so much so quickly in this field that you're basically always discovering what an ignorant fool you are, and how badly the cutting-edge code you thought you were writing yesterday actually sucked.
The frumious Bandersnatch
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2012-11-26 15:20:56
November 26 2012 15:19 GMT
#4029
Very true on all counts. People will bash on you even if you get the problem right if it's too inefficient, if it's not maintainable, if it's not portable, if it's not scalable. Getting the right answer isn't even half the problem most of the time.


I remember writing 30 lines of really shitty, simple code in Python for an interview. I got back home, wrote it in 25 lines. 4 months later, I wrote it in 3. As you improve, you actually get to see your improvement, which is one of the best parts of programming really. You get to see yourself solve problems you had no idea how to solve an hour ago, a week ago, a month ago, a couple years ago. It's really really really satisfying.

The real objective is just to always improve yourself, constantly learn, and never think that you're definitely right about something.

When you see your code from 1 year ago, you should go, "oh damn, I'm a dumb ass for writing that." And then you should simultaneously go, "wow, I love how much improvement I'm making and how much I'm learning."
There is no one like you in the universe.
Snuggles
Profile Blog Joined May 2010
United States1865 Posts
November 26 2012 17:45 GMT
#4030
Wow thanks guys. I'm very open to criticism, especially for programming. I gotta say I have the hardest time wrapping my head around the programs our professor wants us to do for the week so I appreciate all the input from you guys to see what is wrong with the way writing my code.

So what I got from you guys so far is-
1. Make my variables more reasonable and relevant
2. Make it more readable
3. Keep it simple
4. Test my program more thoroughly

I don't know why I didn't just use a for loop rather than while... I even remember that the Professor said that you should use a for loop if you know exactly how many iterations you need and a while loop if that's a dependent variable.

So to make my code more readable does that mean just reducing the amount of unnecessary variables and simplifying the code in general? Is there any form of given etiquette in the style so that my Professor doesn't squint his eyes or something?

btw it's gonna be embarrassing as hell to face my professor now today... and we're getting exams back so I'm worried as hell lol.
Ame
Profile Joined October 2009
United States246 Posts
November 26 2012 18:36 GMT
#4031
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2012-11-26 19:07:07
November 26 2012 19:04 GMT
#4032
On November 27 2012 02:45 Snuggles wrote:
Wow thanks guys. I'm very open to criticism, especially for programming. I gotta say I have the hardest time wrapping my head around the programs our professor wants us to do for the week so I appreciate all the input from you guys to see what is wrong with the way writing my code.

So what I got from you guys so far is-
1. Make my variables more reasonable and relevant
2. Make it more readable
3. Keep it simple
4. Test my program more thoroughly

I don't know why I didn't just use a for loop rather than while... I even remember that the Professor said that you should use a for loop if you know exactly how many iterations you need and a while loop if that's a dependent variable.

So to make my code more readable does that mean just reducing the amount of unnecessary variables and simplifying the code in general? Is there any form of given etiquette in the style so that my Professor doesn't squint his eyes or something?

btw it's gonna be embarrassing as hell to face my professor now today... and we're getting exams back so I'm worried as hell lol.


Indentation (4 spaces usually or 1 'tab' if you use Notepad++) and programming conventions in general. E.g. my Java lecturer wants me to write constants like this:

private static final double PI = 3.14; (notice caps)

Read further:
http://en.wikipedia.org/wiki/Coding_conventions
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2012-11-26 19:11:50
November 26 2012 19:09 GMT
#4033
On November 27 2012 03:36 Ame wrote:
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?


I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake.

Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement.


i=0;
while(i<10) {
//do stuff
++i;
}

for(i=0;i<10;++i) {
//do stuff
}
Any sufficiently advanced technology is indistinguishable from magic
Ame
Profile Joined October 2009
United States246 Posts
November 26 2012 19:28 GMT
#4034
On November 27 2012 04:09 RoyGBiv_13 wrote:
Show nested quote +
On November 27 2012 03:36 Ame wrote:
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?


I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake.

Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement.


i=0;
while(i<10) {
//do stuff
++i;
}

for(i=0;i<10;++i) {
//do stuff
}


I guess I'm looking for... reasoning for why both For and While loops even exist, instead of just one of them. If there is actually something that can be done in one of the loops that the other cannot do without the addition with many lines of code. Maybe For and While loops hit memory in different way (noob here, no idea how heap/stack/whatnot work)?

Otherwise the answer seems to be something vestigial that is just coding conventions now?
frogmelter
Profile Blog Joined April 2009
United States971 Posts
November 26 2012 19:40 GMT
#4035
On November 27 2012 03:36 Ame wrote:
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?


For loops require less lines of code for the same thing a while loop does. Also, the counter variable can be automatically deallocated when the loop ends. But no, here is no for loop that can not be written as a while loop that I know of. A for loop is just a while loop with an initialzer and a statement that runs at the end of every iteration.
TL+ Member
Nick3
Profile Joined March 2011
513 Posts
November 26 2012 19:41 GMT
#4036
On November 27 2012 04:28 Ame wrote:
Show nested quote +
On November 27 2012 04:09 RoyGBiv_13 wrote:
On November 27 2012 03:36 Ame wrote:
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?


I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake.

Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement.


i=0;
while(i<10) {
//do stuff
++i;
}

for(i=0;i<10;++i) {
//do stuff
}


I guess I'm looking for... reasoning for why both For and While loops even exist, instead of just one of them. If there is actually something that can be done in one of the loops that the other cannot do without the addition with many lines of code. Maybe For and While loops hit memory in different way (noob here, no idea how heap/stack/whatnot work)?

Otherwise the answer seems to be something vestigial that is just coding conventions now?


A for and while loop that does the same thing will in most compilers get the same representation when its compiled to bytecode, machine code etc. Essentially for loops can be viewed as special case syntactic sugar for while loops.
WhuazGoodJaggah
Profile Blog Joined January 2009
Lesotho777 Posts
Last Edited: 2012-11-26 19:59:09
November 26 2012 19:58 GMT
#4037
for loops are traditionally for things like a defined number of iterations (walking through an array) where while loops are used for indefinit number of iterations (reading lines of a file until there is nothing left)

so you get:

for(int i=0; i<arrayElements;i++){
take element doggy style;
}

while(NOT EOF){
read that dirty next line;
}


this is why I personally FUCKING HATE endless for loops, it's butt ugly.
small dicks have great firepower
AmericanUmlaut
Profile Blog Joined November 2010
Germany2581 Posts
Last Edited: 2012-11-26 20:13:03
November 26 2012 20:08 GMT
#4038
On November 27 2012 04:28 Ame wrote:
Show nested quote +
On November 27 2012 04:09 RoyGBiv_13 wrote:
On November 27 2012 03:36 Ame wrote:
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?


I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake.

Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement.


i=0;
while(i<10) {
//do stuff
++i;
}

for(i=0;i<10;++i) {
//do stuff
}


I guess I'm looking for... reasoning for why both For and While loops even exist, instead of just one of them. If there is actually something that can be done in one of the loops that the other cannot do without the addition with many lines of code. Maybe For and While loops hit memory in different way (noob here, no idea how heap/stack/whatnot work)?

Otherwise the answer seems to be something vestigial that is just coding conventions now?

It's less a question of convention than a question of readability. If you see for(i=0; i<10; i++){...}, you instantly know what's intended without having to give it a lot of thought, whereas it takes a bit more thinking to parse a while loop that does the same thing (you have to go through the body of the loop and see everything that can happen to your i variable). While loops remain useful, though, because there are a lot of times when you don't have a number of loops you want to do, but rather you just want to loop until you encounter some stop condition, for example looping through a string and doing something with each character:

while(null !== curChar) {
curChar = myString.getNextChar();
doSomething(curChar);
}

You could technically do the same thing with a for loop, but it would be really unreadable. In fact, in terms of making a program work for and while loops could both be replaced by GOTO, which is what they devolve to at the machine code level in any case. We have these control structures not because they work better logically, but because they make code a lot more intuitive to read and write for humans. One of the most important concepts in programming as far as I'm concerned is understanding that writing code that a computer can understand (what 99% of people think we do for a living) is only the first part of the job, and is often relatively easy*. When you're good, you give as much consideration to making sure that you're writing code that a person can understand.

*Except for pointers and the Javascript SelectionRange object.

Edit to add: Another point is that for() gives you a really simple syntax for something that you do over and over and over again as a programmer. A while loop requires several lines of code to do the same thing, which multiplies your opportunites for typos, forgetting to initialize or increment your counter, and so on. One of the reasons that "elegance" is held holy by computer programmers is that if you can figure out a way to solve a problem with 3 lines of code rather than 20, then even if your 3 lines are no faster and no more logically correct than my 20, your code has only 3 things that can be wrong, and mine has 20. At the same time, if a bug in your code does pop up, the guy who gets the job of fixing it only has to understand 3 lines of code, so he'll probably be able to instantly or very quickly grasp what's being done, which might take significantly more time in a function that's much longer.
The frumious Bandersnatch
sAsThark
Profile Joined September 2011
France27 Posts
Last Edited: 2012-11-26 20:13:03
November 26 2012 20:10 GMT
#4039
On November 27 2012 04:09 RoyGBiv_13 wrote:
Show nested quote +
On November 27 2012 03:36 Ame wrote:
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.

Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?


I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake.

Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement.


i=0;
while(i<10) {
//do stuff
++i;
}

for(i=0;i<10;++i) {
//do stuff
}


Yes, and in Java, you can also do a for each like things on an iterable class (your own wich return an iterator or one from Java API, for instance ArrayList).

for(Element e : myArrayList)
{
/*I do stuff with my e*/
}
while(myArrayList.hasNext())
{
Element e = mayArrayList.next()
/*GoTo previus comment*/
}
http://fedoraproject.org/
Ame
Profile Joined October 2009
United States246 Posts
Last Edited: 2012-11-26 20:28:17
November 26 2012 20:11 GMT
#4040
Haha, yes, I get the traditional/conventional reason. >_>

Didn't know the auto deallocate variable part, so I guess that's an additional line of code I didn't think of. Pah, noobs like me don't even deallocate anything to begin with. ... ;;

At the 'less lines of code bit', is it pretty much 3 less lines? Initialization, increment/change, deallocation?

Gotta run, thanks for the replies all. o/

*edit

Ahh, I'll probably take 'readability' as the biggest selling point.

I really need to read up on the stuff such as "for(Element e : myArrayList)". I see that : everywhereeee.

~~

@combinatorial problem: Well, despite it being discouraged but since I don't actually see the 'dont ask about homework' bit in the thread rules... here's in return for people having answered my questions. A guess because it's been forever since I've taken Discrete Math, but I'd wager it has something to do with writing out all the C(n,n) in equation forms and then canceling a lot of things out. Or some crafty substitutions.
Prev 1 200 201 202 203 204 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 3h 16m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 190
Harstem 163
ProTech119
StarCraft: Brood War
Britney 31970
Sea 4801
Horang2 1731
Rain 1681
Shuttle 1130
Hyuk 468
Mini 463
BeSt 362
firebathero 304
Larva 235
[ Show more ]
EffOrt 210
Light 175
Backho 174
Soulkey 123
Soma 108
ZerO 102
Barracks 92
Leta 88
Rush 75
hero 65
ToSsGirL 57
Mong 44
soO 43
Aegong 41
Sharp 31
sorry 21
Free 20
Terrorterran 20
Noble 20
Sacsri 18
Icarus 13
Bale 10
Dota 2
XcaliburYe187
singsing148
NeuroSwarm87
League of Legends
JimRising 350
Reynor115
Counter-Strike
olofmeister2296
allub265
Other Games
B2W.Neo713
ceh9427
Pyrionflax359
Fuzer 321
Mew2King85
nookyyy 32
QueenE31
MindelVK12
ZerO(Twitch)11
Organizations
Dota 2
PGL Dota 2 - Main Stream244
StarCraft: Brood War
lovetv 12
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 4
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos2327
Other Games
• WagamamaTV281
Upcoming Events
WardiTV Korean Royale
3h 16m
ByuN vs herO
ByuN vs Classic
OSC
5h 16m
LAN Event
6h 16m
Replay Cast
11h 16m
Replay Cast
21h 16m
WardiTV Korean Royale
1d
Sparkling Tuna Cup
1d 22h
WardiTV Korean Royale
2 days
Replay Cast
2 days
Wardi Open
3 days
[ Show More ]
Monday Night Weeklies
3 days
StarCraft2.fi
3 days
Replay Cast
3 days
Wardi Open
4 days
StarCraft2.fi
4 days
Wardi Open
5 days
StarCraft2.fi
5 days
Replay Cast
5 days
The PondCast
5 days
Replay Cast
6 days
Liquipedia Results

Completed

SOOP Univ League 2025
RSL Revival: Season 3
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.