# Connecting loops

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?

• ie skip 4,5,6? or how? - Gorets
• Yes, exactly - GALIAF95

First, your code is not correct.

` `for (int i = 1; i < 10; i++) { // во-вторых if (i == 4) { i = 6; } cout << i; }` `
• So I will miss 4 and 5? - GALIAF95
• Yes. Uvas tick: 1236789 - silksofthesoul
• The meaning of what I need: you need to compare 1 number with the other 12. For example, `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` . Then I need to compare the first number so `for (i = 1; i < 2; i++){...}` , `for (i = 4; i < 13; i++){...}` - GALIAF95
• I recommend you to add this in the update to the question. - silksofthesoul
• for (i = 1; i = 3; i ++) i = 3 - there is no logical condition here. Here, the operation assigns the variable i a value of 3. What makes the cycle work is not obvious, and the most interesting thing is that it is quite possible that when running or compiling, there will be no error, and the programmer will think why this is not how it works. - silksofthesoul

First 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 }` `

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.

• can the second method be explained as he jumps? - GALIAF95
• @ GALIAF95, on the right-hand side of for (;;), changes i by the value of the ternary operator (i == 3)? 6: (i + 1). Those. if i is 3, then assign i 6 (let's make your "jump"), otherwise we will increase i by 1. - avp
• UNDERSTOOD THANKS - GALIAF95

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); }` `
• @johniek_comp, in C ++ (C) there is no operation => !!! And those who plyus - do you read the text? - avp
• I don't know crosses, I wrote about it, if a person (TS) thinks that he will rewrite under his own language I think ... the main thing to push is johniek_comp
• @johniek_comp, IMHO HashCode will strongly disagree with this approach to the answers . - avp