Saturday, 6 August 2016

PHP Introduction - Lecture #8

PHP LOOPS Contd.

FOR LOOP

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is

FOR LOOP

This is very basic and very easier type of Loop. For loop allows a statement to be executed for a specific number of times. This loop contains three parts
i-                   Initialization
ii-                 Condition
iii-               Increment/decrement also called updation/alteration.
Syntax
for(init; condition; increment/decrement)
  {
  code to be executed;
  }

Firstly the initialization step is executed, in this step you declare a variable and assign any value to it and put a semicolon.
Next the condition is checked whatever you apply a condition is either true or not. If it’s true the body of the loop is executed, if its false the flow of control is moved to out of the loop and if there is any statement outside the loop it will be printed otherwise code execution will be stopped.
After the body of for loop is executed the flow of control is moved to the increment/decrement statement. This statement will increment by 1 or decrement by 1 to the initialized value, and then loop executes repeatedly, condition is matched after the value is altered then loop body is executed and then again increment/decrement. And this process keeps going on unless the condition becomes false. After the condition is false, the for loop is terminated.
Let’s look an example.



Above figure shows code and the result, first a variable is declared and assigned value 1 after that condition is checked either 1 is less or equal to 10 or not and condition is true then it moves to loop body and prints whatever is in echo statement and in last it goes back to the increment operator that adds +1 to the declared variable and it becomes 2, similarly same steps are repeated unless the condition becomes false and loop stops execution.
Hope you got the concept, further you try yourself more and more by applying different conditions, changing the syntax, and keep doing experiments on programming then you will be the expert in programming.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `Functions `.
Good Luck J

No comments:

Post a Comment