Odin AFi

Breaking Down If, For, and While Statements

This post is a basic breakdown regarding how if, for, and while statements are assembled.

If Statement:


The "if" statement will process a block of code if a specific condition is met. There are two parts to the "if" statement: the condition and the block of code to be executed. The condition compares two or more parameters with an operator or set of operators. If the condition is true, the code executes, otherwise the compiler moves on.
	//Comparing two conditions.
	if (3 >= 3) { 
		//If true, execute this block of code.
	}
	//Comparing more than two conditions.
	if (3 >= 3 || 5 == 5 && 1 == 1) { 
		//Again, execute this block of code if true.
	}

For Statement:


The "for" statement will process a block of code until a preset condition is met. There are four parts to the "for" statement: the starting parameter, parameter comparison/evaluation, parameter increment. As long as the parameter comparison is true, the block of code will execute.
	//Set up initial parameters, compare, and increment. Separate parameter sections with a semi colon.
	for (Set Initial Parameter; Set Parameter Comparison(s); Increment Parameter) {
		//Execute this code until the parameter comparison is false.
	}
	//Set up example:
	for (var x = 1; x <= 5; x++) { 
		//Again, execute this block of code until a parameter comparison is not true.
	}

While Statement:


The "while" statement will process a block of code until one or more conditions are met. There are two parts to the "while" statement: the condition comparison and the block of code to be executed. Until the condition(s) are false, the block of code will continue to loop.
	//Establish comparison parameter(s)
	while (parameter1 != parameter2) {
		//Execute this code until the parameter comparison is false.
	}
	//Multiple parameters
	while (parameter1 == parameter2 || parameter1 > parameter2 || parameter2 == 0) { 
		//Again, loop this block of code until a parameter comparison is not true.
	}

More information: