Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping structures in C (for, while, do-while)
#1
Loops allow you to execute a block of code repeatedly.

// Using a for loop to print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}
printf("\n");

// Using a while loop to calculate the sum of numbers from 1 to 10
int sum = 0, i = 1;
while (i <= 10) {
    sum += i;
    i++;
}
printf("Sum: %d\n", sum);

// Using a do-while loop to print numbers from 1 to 5
int num = 1;
do {
    printf("%d ", num);
    num++;
} while (num <= 5);
printf("\n");
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested structures in C Qomplainerz 0 229 07-25-2023, 01:10 PM
Last Post: Qomplainerz
  Defining and using structures in C Qomplainerz 0 268 07-25-2023, 11:43 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 2 Guest(s)