logo

How to convert a for loop into a while loop

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { int arr[] = { 1, 2, 3, 4, 5 }; int n = 5; int i; // For loop for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // While loop i = 0; while (i < n) { printf("%d ", arr[i]); i++; } printf("\n"); // While loop with -1 init i = -1; while (++i < n) { printf("%d ", arr[i]); } printf("\n"); return 0; }
Course thumb

The C programming language made simple