|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/jun02/count5For.c
/*
similar to count5.c but this uses a for loop to do te same thing we
did with a while loop
*/
#include <stdio.h>
int main()
{
int num, start, inc, end; //variable declarations
/*
below, we're prompting the user for the start, increment and end values
and then getting them from the user
*/
printf("Starting Number: ");
scanf("%d", &start);
printf("Counting up by?: ");
scanf("%d", &inc);
printf("Ending Number: ");
scanf("%d", &end);
/*
inside the parentheses in the for statement, we have
left(initalization): where we're starting
middle(comparison): as long as this is true, keep doing what's
inside the brackets
right(increment): after we do what's inside the brackets, do this thing
and then check with the middle statement to see if we
keep the for loop going or stop.
compare with the while loop in the comments below
*/
for(num = start; num <= end; num = num + inc){
printf("%d\n", num);
}
/*
num = start;
while(num <= end){
printf("%d\n", num);
num = num + inc;
//if we want to increment a value by something other than 1, a statement like the one
//above would be one way to do it.
}
*/
return 0;
}
|