Part 4 | Loops: FOR Loop | C Programming Malayalam Tutorial
3 min read
6 months ago
Published on Aug 30, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
In this tutorial, we will explore the concept of loops in C programming, specifically focusing on the FOR loop. Loops are essential for performing repetitive tasks efficiently in programming. This guide will break down the FOR loop, its components, and practical applications, including examples for printing even numbers, finding prime numbers, and creating patterns.
Step 1: Understanding the Looping Concept
- Definition: A loop is a programming structure that repeats a sequence of instructions until a specific condition is met.
- Importance: Loops are crucial for automating repetitive tasks, reducing code redundancy, and improving efficiency.
Step 2: Introduction to the FOR Loop
- Syntax of the FOR Loop:
for (initialization; condition; increment/decrement) { // Code to be executed }
- Components:
- Initialization: Sets the starting point of the loop.
- Condition: The loop continues as long as this condition is true.
- Increment/Decrement: Changes the loop variable after each iteration.
Step 3: Implementing a FOR Loop
- Example of a Basic FOR Loop:
#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { printf("%d\n", i); } return 0; }
- This loop prints numbers 0 through 4.
Step 4: Printing Even Numbers
- Code to Print Even Numbers:
#include <stdio.h> int main() { for (int i = 0; i <= 20; i += 2) { printf("%d\n", i); } return 0; }
- This loop iterates from 0 to 20, incrementing by 2 to print even numbers.
Step 5: Finding Prime Numbers
- Code to Find Prime Numbers:
#include <stdio.h> int main() { int num, isPrime; for (num = 2; num <= 100; num++) { isPrime = 1; // Assume the number is prime for (int j = 2; j <= num / 2; j++) { if (num % j == 0) { isPrime = 0; // Not prime break; } } if (isPrime) printf("%d\n", num); } return 0; }
- This nested loop checks each number from 2 to 100 for primality.
Step 6: Pattern Printing
- Code for Pattern Printing:
#include <stdio.h> int main() { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }
- This code prints a right-angled triangle of asterisks.
Step 7: Using Break and Continue
- Break Statement: Exits the loop immediately.
- Continue Statement: Skips the current iteration and moves to the next.
- Example:
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exit loop when i is 5 } printf("%d\n", i); } return 0; }
- This loop prints numbers from 0 to 4 and then exits.
Conclusion
In this tutorial, we covered the fundamentals of FOR loops in C programming. We explored its syntax, implemented examples for printing even numbers, finding prime numbers, and creating patterns, and discussed the break and continue statements. Understanding loops is vital for efficient coding practices.
Next Steps
- Practice writing different types of loops in C.
- Experiment with variations of the examples provided.
- Explore other looping constructs like WHILE and DO-WHILE loops.