x0r's blog

By x0r, history, 21 month(s) ago, In English

Upvote if you like 'for' otherwise downvote :D

  • Vote: I like it
  • +32
  • Vote: I do not like it

| Write comment?
»
21 month(s) ago, # |
  Vote: I like it -42 Vote: I do not like it

It's like t!ts or @ss

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +60 Vote: I do not like it

    Neither, I just want to see your smile.

»
21 month(s) ago, # |
  Vote: I like it +8 Vote: I do not like it

It depends on whether you need the variable (that we have in for loop) or not for example as a pointer to array indexes

»
21 month(s) ago, # |
  Vote: I like it +3 Vote: I do not like it

This is stupid. It's just like comparing two JS frameworks.

»
21 month(s) ago, # |
  Vote: I like it -6 Vote: I do not like it

sometimes you need to do something while some condition is true, you don't know when it will stop, in this case you better use while loop

and it's better to use for loop when you need a variable and/or you want to do something X times.

you can use while and for in any case.

This is an example.

Example.

all these codes are doing the same thing. that is while t > 0 keep going and assign t-1 to t.

»
21 month(s) ago, # |
  Vote: I like it +89 Vote: I do not like it

goto.

»
21 month(s) ago, # |
  Vote: I like it -39 Vote: I do not like it

Te rog frumos sa te civilizezi.

Fors are used when the quantity iterated after is a fix one and the While is used when the halt condition is one that may very on the mathematical factors that go into the process. If you use breaks, you are a moron. Because, breaks are from the stone age when we used goto to write loops. But we have evolved to now use while and for. The only use for breaks should be that in the switch condition. Do you want to use tools from the stone age instead of those that are new? I didn't think so

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    That was funny

    Sound logic in the 2nd paragraph, thou. Cheers

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it -19 Vote: I do not like it

      I am sorry if you only rely to nitpicking on superficial elements that my message contained as a mean to try to destabilize my message. I truly wish you would right your ways-- stopping feeling so superior; sorry for being the one with lower rating that tries to have some fun out of this "sport".

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +29 Vote: I do not like it

    I might be a moron, but I'm red.

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it -90 Vote: I do not like it

      Să recapitulăm regulile de programare. Atenție maximă vă rog, sînt cerințe ce nu apar întotdeauna la orice colt.

      Variabilele simple nu se inițializează la declarare. Ele se inițializează cît mai aproape de secțiunea de program care le folosește. Cu alte cuvinte orice variabilă se inițializează cît mai jos posibil. De ce? Pentru citibilitatea codului. Imaginați-vă că la linia 300 vedem o instrucțiune a = a + y;. Ne punem întrebarea cu ce valoare a fost inițializată a. Imaginați-vă că trebuie să mergem cîteva pagini în sus pentru a constata că variabila a fost inițializată la declarare int a = 1; Vectorii încep de la 0, nu de la 1. Este o regulă importantă, nu o preferință personală. Avantaje: cînd folosim modulo, vectori caracteristici, etc. Vectorii au dimensiunile specificate de cerinţă. Ei nu se declară int v[10010]. Aceasta se cheamă programare aproximativă şi nu este o practică acceptată la nivel de înalt. Dacă la matematică vi s-ar cere să rezolvaţi ecuaţia 2 · x = 5 şi aţi răspunde că x este un număr între doi şi trei nu cred că aţi putea fi consideraţi cei mai mari matematicieni ai ţării. Programarea aproximativă reprezintă lenea minţii. Să punem cîteva elemente în plus, în caz că depăşesc vectorul. Cine depăşeşte vectorul? Zîna memoriei? Nu, voi. Soluţia corectă este să vă corectaţi gîndirea şi să vă ascuţiţi mintea pentru ca algoritmul vostru să nu depăşească vectorul. Altfel veţi rămîne incapabili de a scrie programe în lumea reală unde aproximarea nu e acceptabilă. Reguli de indentare, exemple: Indentarea se face la două sau la patru spații. Două sînt suficiente pentru citibilitate. Atenție: setați code::blocks să nu folosească caractere TAB. Acolada deschisă se pune pe același rînd cu instrucțiunea precedentă. Acolada închisă se pune pe rîndul următor aliniată sub instrucțiunea de care aparține. Dacă după else urmează un if atunci if-ul se pune imediat după else pe aceeași linie, iar corpul de instrucțiuni aparținînd lui else se indentează cu două spații, nu cu patru. Astfel, în cazul unor instrucțiuni de genul if ... else if ... else if ... se evită migrarea codului către dreapta prin indentări inutile. Fără break/continue/return/goto în interiorul buclelor. Ele distrug programarea structurată și, implicit, zeci de ani de experiență a omenirii. Folosim for pentru bucle cu număr cunoscut de pași, while în celelalte cazuri. Aceasta este o regulă de programare ordonată, care face codul mai citibil. Nu avem voie să modificăm variabila de buclă for și nici limita ei de final. Dacă este un ciclu cu număr cunoscut de pași nu avem de ce să modificăm aceste variabile. Cînd eliminăm un element din vector micșorăm numărul lui de elemente cu 1 (n--). În felul acesta n continuă să arate numărul de elemente din vector. Nu aveți voie să aveți warnings, vă pot fi fatale: declaraţi int main() si folosiţi return 0 la final. Eliminați orice alte warnings deoarece majoritatea sînt o problemă reală. Unele concursuri compilează sursa cu opriri la warnings (adică tratează warning-urile ca erori). Unele warnings se referă la variabile neinițializate sau la număr diferit de "%d" versus variabile citite, in fscanf, deci sînt importante. Este obligatoriu să setaţi Code::Blocks să vă afişeze toate warnings. Pentru aceasta, după crearea proiectului, navigaţi în meniul Project, submeniul Options. În acea fereastră setaţi opţiunile -Wall şi -O2. Comentați codul cînd e ceva complicat, de exemplu o formulă. Scrieți ideea algoritmului, dacă nu e ceva trivial. Nu trebuie să umpleți de comentarii, dar minimal. Mă ajutați mult în corectarea temei. Denumiți variabilele mai clar. Nu lungi, dar să aibă semnificație: prod, sum, nrcf, etc. Nu abuzați stegulețele. Exemplu unde nu sînt necesare break sau stegulețe: căutarea unui element e în vectorul v: i = 0; while ( (i < n) && (v[i] != e) ) i++; Folosiți corect fișiere, le deschideți cu fopen, iar la final obligatoriu le închideți cu fclose. În nici un caz nu folosiți freopen. Folosiți doar funcțiile de intrare/ieșire C, fscanf, fprintf, fgetc, fputc, posibil fgets. Aveți aici un sumar al acestor funcții: instrucțiuni de intrare/ieșire în limbajul C Nu folosiți funcții gen sort și qsort. Aveți nevoie de dispensă specială

      In conclusion, you shouldn't be red, you are a fraud

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
          Vote: I like it +6 Vote: I do not like it

        stop using that machine language pls

        • »
          »
          »
          »
          »
          21 month(s) ago, # ^ |
            Vote: I like it +13 Vote: I do not like it

          It's not a machine language, it's the superior language of masters (or higher of course) — Romanian.

          Marinush

»
21 month(s) ago, # |
  Vote: I like it +3 Vote: I do not like it

Actually it depends on programmer's comfort. Personally I am comfortable with for. But to write an Infinity loop and Test case, I am pretty much comfortable with while. Like while(1), while(true), while(t--)

»
21 month(s) ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Deleted

»
21 month(s) ago, # |
  Vote: I like it +1 Vote: I do not like it

When it comes to Python, For is 10x faster than While.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    When it comes to Go, while doesnt exist

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

recursion? :->

»
21 month(s) ago, # |
  Vote: I like it +39 Vote: I do not like it

whatever is faster to type

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    ctrl+c ctrl+v

    haha

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    You can do #define for while and then use for(true) {...}, it's faster to type than both original for and while loop.

»
21 month(s) ago, # |
  Vote: I like it -18 Vote: I do not like it

I use whichever comes first in mind

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    for the technical "which is better" question though, for(a;b;d)c; would really just be an alias of a;while(b){c;d;} in C++ at least so you do not have to worry. (performance with avx could differ actually but we need more samples on this)

    • »
      »
      »
      19 months ago, # ^ |
      Rev. 5   Vote: I like it +8 Vote: I do not like it

      There's still a very slim difference between their effects.

      When you use continue in the body of the for(a;b;d)c; loop, it will execute d before checking b, but in a;while(b){c;d;} , d won't be executed.

      For example,

      for(;;puts("b")){
      		continue;
      		puts("a");
      	}
      

      this will continuously output b.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    lol what is non normal for downvote?

»
21 month(s) ago, # |
  Vote: I like it +79 Vote: I do not like it

Definitely it’s better to just use 10^6 if statements instead of loops. It makes your code O(1) instead of O(n), and it’s extra explicit which makes it easier to read.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +25 Vote: I do not like it

    Even better is 10^6 nested ternary statements, which is extra clear and also faster to type because the ternary statement only has two characters.

    • »
      »
      »
      21 month(s) ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      even better than the ternary statement (when you don't need the one side of the ternary statement) is the short circuiting, basically boolean&&statement; equals if(boolean)statement;, and boolean||statement; equals if(!boolean)statement;. note that this works only in situations where the statement returns a value convertible to boolean. (if it doesnt then a simple usage of the comma operator would force it to do so)

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 2   Vote: I like it +10 Vote: I do not like it

    do you have any example?

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    just use 10^6 if statements instead of loops. It makes your code O(1)
    Branch prediction laughing in the corner.

»
21 month(s) ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

Do while loop would be crying in the corner. No one uses it. :sad:

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +33 Vote: I do not like it

    I use it because next_permutation

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +23 Vote: I do not like it

    I use it when I'm generating a boolean number:

    do {
      a = rand(); 
    } while(a != 0 && a != 1);
    
    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it -10 Vote: I do not like it

      LOL, why don't you simply do rand() % 2 XD.

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
          Vote: I like it +42 Vote: I do not like it

        cause he's red

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        rand() % n is not uniformly distributed (though unnoticeable when n is very small)

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

MFW using golang

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Talents flourish because of the contribution!