0 Members and 3 Guests are viewing this topic.
Quote from: bb010g on December 24, 2014, 12:34:22 amQuote from: Matrefeytontias on November 10, 2014, 02:30:36 pmCode: [Select]while(oreo) oreo--;Looking though this again, you could optimize:Code: [Select]while(oreo--);I think the compiler does that for you.In both cases the outcome is:while:branch if oreo is zero: endwhileoreo --branch to whileendwhile:
Quote from: Matrefeytontias on November 10, 2014, 02:30:36 pmCode: [Select]while(oreo) oreo--;Looking though this again, you could optimize:Code: [Select]while(oreo--);
Code: [Select]while(oreo) oreo--;
while(oreo) oreo--;
while(oreo--);
while(--oreo);
Most other languages too, but you can fix it:Code: [Select]while(--oreo);'You're in trouble if you start with zero though.
Today I learnt something : life is made of algorithms. I discovered one today, being a student in my own appartment :Code: [Select]while(oreo) oreo--;Did you discover things like this ?
package Matrix.AIs.Matrefeytontias;public class Matrefeytontias { private OreoBox oreoBox; public static OreoStore oreoStore; public static void main(String... args) { oreoStore = new OreoStore(); Matrefeytontias matrefeytontias = new Matrefeytontias(); matrefeytontias.live(); } private void live() { while (oreoStore.hasOreos()) { buyOreos(); while (oreoBox.hasOreos()) { oreoBox.eatOreos(); } } //die } private void buyOreos() { oreoBox = oreoStore.buyBox(); } static class OreoStore { int boxes; OreoStore() { boxes = 100; } OreoBox buyBox() { boxes--; return new OreoBox(); } boolean hasOreos() { return boxes != 0; } } static class OreoBox { int numberOfOreos; OreoBox() { numberOfOreos = 50; //Correct me here please } void eatOreos() { numberOfOreos--; } boolean hasOreos() { return numberOfOreos != 0; } }}
Hmm, seems like too much work to just eat Oreos.