In this lab, we'll explore how to write different types of loops in C programming. We'll start with a basic main program and gradually introduce new functions, each implementing a different type of loop: for, while, and do-while. Follow along as we demonstrate these loop constructs with examples.
Let's begin with a simple main program that demonstrates the basic structure of C code:
#include <stdio.h> int main() { // Our starting point printf("C loop demo program\n"); return 0; }
Compile and run the program by clicking the Excecute button in the Jdoodle window.
You should See the following output:
C loop demo program
This is our starting point. Now, let's add functions for different types of loops.
We'll start with the for loop, which is perfect for tasks with a known number of iterations. Here's how you can write a for loop in C:
#include <stdio.h> // Function to demonstrate a for loop void demonstrateForLoop() { printf("Using a for loop to print numbers from 1 to 5:\n"); for (int i = 1; i <= 5; i++) { printf("%d ", i); } printf("\n"); } int main() { printf("C loop demo program\n"); demonstrateForLoop(); return 0; }
In this example, the demonstrateForLoop function showcases the for loop. It prints numbers from 1 to 5.
Compile and run the program by clicking the Excecute button in the Jdoodle window.
You should See the following output:
C loop demo program Using a for loop to print numbers from 1 to 5: 1 2 3 4 5
Next, let's introduce the while loop, which is suitable when the number of iterations is not known in advance. Here's how to use a while loop:
#include <stdio.h> // Function to demonstrate a while loop void demonstrateWhileLoop() { printf("Using a while loop to print numbers from 1 to 5:\n"); int i = 1; while (i <= 5) { printf("%d ", i); i++; } printf("\n"); } // Function to demonstrate a for loop void demonstrateForLoop() { printf("Using a for loop to print numbers from 1 to 5:\n"); for (int i = 1; i <= 5; i++) { printf("%d ", i); } printf("\n"); } // Rest of the code remains the same int main() { printf("C loop demo program\n"); demonstrateForLoop(); demonstrateWhileLoop(); return 0; }
In the demonstrateWhileLoop function, we print numbers from 1 to 5 using a while loop. The loop continues as long as the condition i <= 5 is true.
Compile and run the program by clicking the Excecute button in the Jdoodle window.
You should see the following output:
C loop demo program Using a for loop to print numbers from 1 to 5: 1 2 3 4 5 Using a while loop to print numbers from 1 to 5: 1 2 3 4 5
Finally, let's incorporate the do-while loop, which guarantees that a block of code is executed at least once before checking the condition. Here's how to use a do-while loop:
#include <stdio.h> // Function to demonstrate a do-while loop void demonstrateDoWhileLoop() { printf("Using a do-while loop to print numbers from 1 to 5:\n"); int i = 1; do { printf("%d ", i); i++; } while (i <= 5); printf("\n"); } // Function to demonstrate a while loop void demonstrateWhileLoop() { printf("Using a while loop to print numbers from 1 to 5:\n"); int i = 1; while (i <= 5) { printf("%d ", i); i++; } printf("\n"); } // Function to demonstrate a for loop void demonstrateForLoop() { printf("Using a for loop to print numbers from 1 to 5:\n"); for (int i = 1; i <= 5; i++) { printf("%d ", i); } printf("\n"); } // Rest of the code remains the same int main() { printf("C loop demo program\n"); demonstrateForLoop(); demonstrateWhileLoop(); demonstrateDoWhileLoop(); return 0; }
In the demonstrateDoWhileLoop function, we print numbers from 1 to 5 using a do-while loop. This loop ensures that the code block runs at least once before checking the condition.
Compile and run the program by clicking the Excecute button in the Jdoodle window.
You should see the following output:
C loop demo program Using a for loop to print numbers from 1 to 5: 1 2 3 4 5 Using a while loop to print numbers from 1 to 5: 1 2 3 4 5 Using a do-while loop to print numbers from 1 to 5: 1 2 3 4 5
Congratulations! You've successfully learned how to write different types of loops in C programming. We started with a basic main program and gradually introduced for, while, and do-while loops, each implemented within a separate function. Loops are essential tools for controlling program flow and automating repetitive tasks, making your C programs more efficient and versatile.
File: labc_loops.md
© Copyright 2023 Destin Learning