I need to do a for
loop in this way: from 1 to 3 and from 6 to 9. How to do it? I can only separately
for(i=1; i=3; i++) ... for(i=6; i=9; i++) ...
Is it possible to connect them somehow?
I need to do a for
loop in this way: from 1 to 3 and from 6 to 9. How to do it? I can only separately
for(i=1; i=3; i++) ... for(i=6; i=9; i++) ...
Is it possible to connect them somehow?
First, your code is not correct.
for (int i = 1; i < 10; i++) { // во-вторых if (i == 4) { i = 6; } cout << i; }
for(i = 1; i < 13; i++){if (pervoechislo == vtoroe[i]){...}
, but the first number is in the array of the other 12 numbers, but I need this number to be skipped ie pervoechislo=vtoroe[3]
. Then I need to compare the first number so for (i = 1; i < 2; i++){...}
, for (i = 4; i < 13; i++){...}
- GALIAF95First way: skip iterations
for (i = 1; i <= 9; i++) { if (4 <= i && i <= 5) continue; // do something }
The second way: do a "jump" during the change step. (ternary operator)
for (i = 1; i <= 9; i = (i == 3) ? 6 : (i + 1)) { // do something }
UPD: As already noted here, your code is incorrectly written.
for (initial values; condition under which the cycle works; setting variable changes)
UPD2: how the ternary operator works:
A=(B)?C:D;
If condition B is true, then the value C is written to the variable A, otherwise the value D. is written there.
As always, I post the original way without branches:
for(int i=1; i<10; i++) { i+=(i==4)*2; ... }
The most original way:
for(int i=1; i<10; i+=1+(i==4)*2) ...
Not original, but more understandable way:
for(int i=1; i<10; i++) { if(i==4) i=6; ... }
something like this
for (int i = 1; i <= 9; i++) { if (i = >4 && i <= 6) { printf("Ничего не делаем"); } printf("%d", i); }
Source: https://ru.stackoverflow.com/questions/113649/
All Articles