-
Notifications
You must be signed in to change notification settings - Fork 0
C_While_Loop.c
ani9977 edited this page Sep 11, 2024
·
1 revision
Here’s the updated C code with added documentation and an explanation:
#include <stdio.h>
/**
* @brief The main function demonstrates the use of a while loop in C.
*
* This function uses a while loop to print the value of the integer variable 'i' from 0 to 4.
* The loop continues until the condition `i < 5` is no longer true.
*
* @return int Returns 0 to indicate successful execution.
*/
int main() {
int i = 0; /**< Integer variable initialized to 0. */
// While loop to print the value of 'i' as long as 'i' is less than 5
while (i < 5) {
printf("i: %d\n", i); /**< Print the current value of 'i'. */
i++; /**< Increment 'i' by 1 after each iteration. */
}
return 0; /**< Return 0 to indicate successful execution. */
}-
Variable Initialization:
-
int i = 0;declares an integer variableiand initializes it with the value0.
-
-
While Loop:
-
while (i < 5)sets up a loop that will continue executing as long as the conditioni < 5is true. - Inside the loop:
-
printf("i: %d\n", i);prints the current value ofi. -
i++;increments the value ofiby1after each iteration.
-
-
-
Termination:
- The loop stops when
ireaches5, asi < 5becomes false.
- The loop stops when
-
Return Statement:
-
return 0;indicates that the program has executed successfully. The value0is returned to the operating system.
-