Activity 3.4.
-
Try it out by clicking on the โRunโ button in the above window and verify that the same line is indeed printed 5-times.
-
Now modify the code so that it prints
Hello, World!10-times.
for-Loop
Compile and run the code below in your own C environment.
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
printf("Hello, World!\n");
return(0);
}
Compile and run the code below in your own C environment.
#include <stdio.h>
int main(void) {
int i;
for (i = 1; i<=5; i++) {
printf("Hello, World!\n");
}
return(0);
}
for-loop; weโll learn about the other types of loops later. We will also soon learn more about the use of the rather mysterious-looking โiโ and the line โint i;โ. For now, just think of i as something that can hold different values. In this particular example, i takes on values between 1 and 5, and for each value of i between 1 and 5 this loop prints out Hello, World!. That is, Hello, World! is printed exactly 5-times.
Hello, World! 10-times.Compile and run the code below in your own C environment.
#include <stdio.h>
int main(void) {
int i;
for (i = 1; i<=3; i++) {
printf("Hello, World! ");
printf("Today is Saturday.\n");
}
return(0);
}
Hello World 3-times, then Today is Saturday 3-times?Hello World. Today is Saturday. 3-times ?