Skip to content

Latest commit

 

History

History
62 lines (62 loc) · 787 Bytes

lab4.md

File metadata and controls

62 lines (62 loc) · 787 Bytes

The while loop

The basics

while (condition) {
  // code
}

The for loop

Do something n times

Using a while loop

int i = 0;
while (i < n) {
  // code
  i++;
}

Using a for loop

for (int i = 0; i < n; i++) {
  // code
}

A for loop as a while loop

for (;condition;) {
  // code
}

The do-while loop

Do something, then do it while condition is true

Using a while loop

// code
while (condition) {
  // code
}

We can do better!

do {
  // code
} while (condition);

The infinite loop

Sometimes we want this:

while (true) {
  // code
}

Sometimes we don't

for (int i = 0; i < n; i++) {
  if (condition)
    i--;  // This could cause an infinite loop!!!
}