I still do not understand how the while loop works, where do the developers get the formulas that are written in brackets after the while and for loops?

    2 answers 2

    All cycles can be written through the conditional operator and unconditional transition.

    For example,

    for (A; B; C) {D;} can be rewritten as A; while (B) {D; C;} A; while (B) {D; C;}

    while (A) {B;} - as a: if (A) {B; goto a;} a: if (A) {B; goto a;}

    Well, and further in the same vein. It is highly recommended to compare the results obtained with the flowcharts depicting them. And already in such a visual form you can easily figure out what condition you need to enter. The main thing to remember is that the value "FALSE" in C is taken to be 0, and any other value is "TRUE." Those. if (0) {A;} - the block of instructions will not be executed, and if (1) {A;} - will always be executed. Also note that different operators have return values. This allows you to make constructions of the form a=b=0; because b=0 returns 0 , and then assigns it to a . And in such cases, newcomers are usually confused and make mistakes in the conditions of the if, while and do-while statements.

    • one
      Good old way to calculate the i-th number from the Fibbonacci series. <pre> int i = 8, a1, a2; for (a1 = a2 = 1; i> 2; a1 = (a2 + = a1) -a1) i--; </ pre> try to write to other cycles with an equivalent amount of code. - Specter

    You will not believe it, but the developers come up with them themselves to set the necessary cycle action! How the loops work is described in each C / C ++ tutorial.