logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jun23/loop.c

/*

a review of loops

using both a while and a for loop to do the exact same thing: 
printing numbers 1-10

*/

#include <stdio.h>

/*

all loops have three crucial parts

1. initial statement   - where you want the loop to start
2. condition statement - as long as this statement is true, keep doing the loop
3. increment statement - do this thing at the end of each loop, then we check the
						 condition statement to see whether or not it's still true


in a while loop, you have to do the initial statement before calling the while loop
then inside the parenthesis of the while loop the only thing you put there is the 
condition statement. then somewhere inside (preferably at the end of) the curly braces 
of the loop, you do the increment statement

A while can be structured like this

inital

while(condition){
	
	//code where stuff happens

	increment 
}


in a for loop, you handle all three statements inside the parenthesis, seperated by
semicolons and structured as such

for(initial; condition; increment){
	//code where stuff happens
}



*/

int main(){


	//doing a while loop first

	int n = 1; //inital statement

	printf("Printing 1-10 using while.\n");
	while(n <= 10){//in a while loop, the only thing that goes inside the
				   // parenthesis is the condition statement

		printf("%d\n", n);


		n++; //increment statement, do this at the end of the while loop
	}
	printf("Printing 1-10 using for.\n");
	
	//for(initial statement; condition statement; increment statement)
	for(n = 1; n <= 10; n++){
		printf("%d\n", n);
	}

	return 0;
}