Skip to content

Instantly share code, notes, and snippets.

@codistwa
Last active February 23, 2022 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codistwa/0a0f72b44caec5a632ea86d76dc1e207 to your computer and use it in GitHub Desktop.
Save codistwa/0a0f72b44caec5a632ea86d76dc1e207 to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/loops. More courses on https://codistwa.com
// ============================================================
// Simple for loop
// ============================================================
int fruits [3] = {1, 2, 3};
for (int i = 0; i < 3; i++)
printf("%d\n", fruits[i] * 2);
// ============================================================
// parameters
// ============================================================
int fruits [3] = {1, 2, 3};
for (int i = 1; i < 3; i++)
printf("%d\n", fruits[i] * 2);
// ============================================================
// decrementation
// ============================================================
int fruits [5] = {1, 2, 3, 4, 5};
for (int i = 5; i >= 2; i--)
printf("%d\n", i);
// ============================================================
// nested
// ============================================================
int cars[5] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
printf ("%d %d\n", i, j);
}
printf ("");
}
// ============================================================
// While
// ============================================================
int fruits[3] = { 1, 2, 3 };
int i = 0;
while (i < 3) {
printf ("%d\n", fruits[i] * 2);
i++;
}
// ============================================================
// Do while
// ============================================================
int fruits[3] = { 1, 2, 3 };
int i = 0;
do {
printf ("%d\n", fruits[i] * 2);
i++;
}
while (i < 3);
// ============================================================
// Break
// ============================================================
int fruits[3] = { 1, 2, 3 };
int i = 0;
while (1 < 3) {
i++;
if (i == 2)
break;
}
printf ("%d\n", i);
// ============================================================
// Simple for loop
// ============================================================
int fruits [3] = {1, 2, 3};
for (int i = 0; i < 3; i++)
cout << fruits[i] * 2 << endl;
// ============================================================
// parameters
// ============================================================
int fruits [3] = {1, 2, 3};
for (int i = 1; i < 3; i++)
cout << fruits[i] * 2 << endl;
// ============================================================
// decrementation
// ============================================================
int fruits [5] = {1, 2, 3, 4, 5};
for (int i = 5; i >= 2; i--)
cout << i << endl;
// ============================================================
// nested
// ============================================================
int cars[5] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
cout << i << " " << j << endl;
}
cout << "" << endl;
}
// ============================================================
// While
// ============================================================
int fruits[3] = { 1, 2, 3 };
int i = 0;
while (i < 3) {
cout << fruits[i] * 2 << endl;
i++;
}
// ============================================================
// Do while
// ============================================================
int fruits[3] = { 1, 2, 3 };
int i = 0;
do {
cout << fruits[i] * 2 << endl;
i++;
}
while (i < 3);
// ============================================================
// Break
// ============================================================
int fruits[3] = { 1, 2, 3 };
int i = 0;
while (1 < 3) {
i++;
if (i == 2)
break;
}
cout << i << endl;
// ============================================================
// Simple for loop
// ============================================================
const fruits = [1, 2, 3];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i] * 2);
}
// ============================================================
// parameters
// ============================================================
const fruits = [1, 2, 3];
for (let i = 1; i < fruits.length; i++) {
console.log(fruits[i] * 2);
}
// ============================================================
// decrementation
// ============================================================
const fruits = [1, 2, 3, 4, 5];
for (let i = fruits.length; i >= 2; i--) {
console.log(i); // 5 4 3 2
}
// ============================================================
// nested
// ============================================================
const cars = [1, 2, 3, 4, 5];
for (let i = 0; i < cars.length; i++) {
for (let j = 0; j < i; j++) {
console.log(i, j);
}
console.log('');
}
// ============================================================
// While
// ============================================================
const fruits = [1, 2, 3];
let i = 0;
while (i < fruits.length) {
console.log(fruits[i] * 2);
i++;
}
// ============================================================
// Do while
// ============================================================
const fruits = [1, 2, 3];
let i = 0;
do {
console.log(fruits[i] * 2);
i++;
}
while (i < fruits.length);
// ============================================================
// Break
// ============================================================
const fruits = [1, 2, 3];
let i = 0;
while (i < fruits.length) {
i++;
if (i === 2) {
break;
}
}
console.log(i);
# ============================================================
# Simple for loop
# ============================================================
fruits = [1, 2, 3]
for x in fruits:
print(x * 2)
# ============================================================
# nested
# ============================================================
cars = [1, 2, 3, 4, 5]
for i in cars:
for j in cars:
print(i, j)
print('')
# ============================================================
# While
# ============================================================
fruits = [1, 2, 3]
i = 0
while i < len(fruits):
print(fruits[i] * 2)
i += 1
# ============================================================
# Break
# ============================================================
fruits = [1, 2, 3]
i = 0
while i < len(fruits):
if i == 2:
break
i += 1
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment