While, Do-While
Imagine a scenario where you need to repeatedly execute certain tasks, like reading data from a sensor, attempting password entries, or counting words in a sentence. In these situations, loops come into play.
Loops enable you to run specific blocks of code multiple times, be it tens, hundreds, or even thousands of times. Grasping the concept of loops is vital in programming. This course delves into the foundational loops: the while loop, do-while loop, and for loop.
While Loop
This loop continues running as long as a specific condition is met. Once the condition isn't satisfied, the loop stops.
main.c
123while (condition) { // Loop body }
A basic use of a loop is to display the count of its repetitions:
Main.c
1234567891011121314#include <stdio.h> int main() { int iterations = 1; while (iterations <= 10) { printf("%d\n", iterations); iterations++; // `iterations = iterations + 1;` } return 0; }
To halt this loop, a terminating condition is essential. A straightforward approach is using a counter to track the number of times the loop runs.
An iteration refers to a single cycle within a loop. So, if the loop runs the code block 10 times, it has completed 10 iterations.
The line iterations++; is crucial as it increments the counter (int iterations) with each pass. The counter then sets the conditions to terminate the loop.
It's imperative to establish conditions to exit the loop. Failing to do so will result in an endless loop.
Main.c
12345678910111213141516#include <stdio.h> int main() { int array[] = { 3, 6, 2, 134, 45, 2, 564, 8, 3, 531 }; int i = 0; // Index of array while (i < 10 ) { printf("Index of element: %d\tValue of element: %d\n", i, array[i]); i++; // `i = i + 1` } return 0; }
Focus on the expression array[i].
Here, the variable i denotes the index of the array[] elements.
With every cycle, the variable i increases by 1. This means that during each cycle, the expression array[i] accesses the subsequent array element:
The key distinction between the do-while and while loops is that the former guarantees at least one execution, even if its condition is initially false.
main.c
123do { // do_something } while (condition);
This type of loop is handy for crafting basic user interfaces. For instance, when prompting for a password, because it lets you keep asking until the input is valid. A do-while style loop is perfect here since the prompt must appear at least once, while a while loop works when you only enter if a precondition is met.
Main.c
12345678910111213#include <stdio.h> int main() { int i = 0; do { printf("Condition `i == 1` is false, because i = 0, but loop is working...\n"); } while (i == 1); printf("Loop is over\n"); return 0; }
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain the difference between a while loop and a do-while loop with examples?
How does the counter help in terminating a loop?
Can you show how to use a loop to read data from a sensor?
Awesome!
Completion rate improved to 2.63
While, Do-While
Stryg for at vise menuen
Imagine a scenario where you need to repeatedly execute certain tasks, like reading data from a sensor, attempting password entries, or counting words in a sentence. In these situations, loops come into play.
Loops enable you to run specific blocks of code multiple times, be it tens, hundreds, or even thousands of times. Grasping the concept of loops is vital in programming. This course delves into the foundational loops: the while loop, do-while loop, and for loop.
While Loop
This loop continues running as long as a specific condition is met. Once the condition isn't satisfied, the loop stops.
main.c
123while (condition) { // Loop body }
A basic use of a loop is to display the count of its repetitions:
Main.c
1234567891011121314#include <stdio.h> int main() { int iterations = 1; while (iterations <= 10) { printf("%d\n", iterations); iterations++; // `iterations = iterations + 1;` } return 0; }
To halt this loop, a terminating condition is essential. A straightforward approach is using a counter to track the number of times the loop runs.
An iteration refers to a single cycle within a loop. So, if the loop runs the code block 10 times, it has completed 10 iterations.
The line iterations++; is crucial as it increments the counter (int iterations) with each pass. The counter then sets the conditions to terminate the loop.
It's imperative to establish conditions to exit the loop. Failing to do so will result in an endless loop.
Main.c
12345678910111213141516#include <stdio.h> int main() { int array[] = { 3, 6, 2, 134, 45, 2, 564, 8, 3, 531 }; int i = 0; // Index of array while (i < 10 ) { printf("Index of element: %d\tValue of element: %d\n", i, array[i]); i++; // `i = i + 1` } return 0; }
Focus on the expression array[i].
Here, the variable i denotes the index of the array[] elements.
With every cycle, the variable i increases by 1. This means that during each cycle, the expression array[i] accesses the subsequent array element:
The key distinction between the do-while and while loops is that the former guarantees at least one execution, even if its condition is initially false.
main.c
123do { // do_something } while (condition);
This type of loop is handy for crafting basic user interfaces. For instance, when prompting for a password, because it lets you keep asking until the input is valid. A do-while style loop is perfect here since the prompt must appear at least once, while a while loop works when you only enter if a precondition is met.
Main.c
12345678910111213#include <stdio.h> int main() { int i = 0; do { printf("Condition `i == 1` is false, because i = 0, but loop is working...\n"); } while (i == 1); printf("Loop is over\n"); return 0; }
Tak for dine kommentarer!