// Print 1 to nfor (int i = 1; i <= n; i++) { cout << i << " ";}// Print n to 1for (int i = n; i >= 1; i--) { cout << i << " ";}// Step by 2for (int i = 0; i < n; i += 2) { cout << i << " ";}
// Break: exit the loop earlyfor (int i = 1; i <= 100; i++) { if (i * i > n) break; cout << i << " ";}// Continue: skip to next iterationfor (int i = 1; i <= n; i++) { if (i % 3 == 0) continue; // Skip multiples of 3 cout << i << " ";}
int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a;}
Most brute-force solutions use nested loops. If the constraints are small (n ≤ 1000), an O(n²) solution with nested loops will work. If n ≤ 10⁷, you need O(n) or O(n log n).