For Loops
C for loop constructs
Basic for loop
Simple C for loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Number: %d\n", i);
}
return 0;
}
For loop with step
For loop with custom step
#include <stdio.h>
int main() {
for (int i = 0; i <= 10; i += 2) {
printf("Even number: %d\n", i);
}
return 0;
}
C for with basic iteration
C for loop with basic iteration
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("Number: %d\n", i);
}
return 0;
}
C for with step size
C for loop with custom step size
#include <stdio.h>
int main() {
for (int i = 0; i <= 20; i += 2) {
printf("Even number: %d\n", i);
}
return 0;
}
C for with array processing
C for loop processing array elements
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
C for with nested loops
C nested for loops creating pattern
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
C for with multiple variables
C for loop with multiple variables
#include <stdio.h>
int main() {
for (int i = 1, j = 10; i <= 5; i++, j--) {
printf("i = %d, j = %d\n", i, j);
}
return 0;
}
C for with string processing
C for loop processing string characters
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Programming";
for (int i = 0; i < strlen(str); i++) {
printf("Character %d: %c\n", i, str[i]);
}
return 0;
}
C for with multiple conditions
C for loop with multiple variables and conditions
#include <stdio.h>
int main() {
for (int i = 0, j = 10; i < 5 && j > 5; i++, j--) {
printf("i = %d, j = %d\n", i, j);
}
return 0;
}
C for with break and continue
C for loop with break and continue statements
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue; // Skip iteration
}
if (i == 8) {
break; // Exit loop
}
printf("Number: %d\n", i);
}
return 0;
}