I've been trying to understand loops but I understand it 70%. The for and while loop is the hardest for me. I need someone to break it down very simple.
What programming language? its different for each. example, PHP: <?php for ($x=0; $x<=10; $x++) { echo "The number is: $x <br>"; } ?> Code (markup): Starts with the variable $x being 0 ($x=0), while its less than, or equal to 10 ($x<=10), increase by 1 ($x++)
Loops are pretty much the same for all programming and scripting languages. As states on the manual for the for: Examples: <?php /* example 1 */ for ($i = 1; $i <= 10; $i++) { echo $i; } /* example 2 */ for ($i = 1; ; $i++) { if ($i > 10) { break; } echo $i; } /* example 3 */ $i = 1; for (; ; ) { if ($i > 10) { break; } echo $i; $i++; } /* example 4 */ for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); PHP: While on the other hand is pretty simple, keep doing something until the expression is false. The difference between while and do while is that if the expression is false on the first run, it will run once on a do-while while zero times on a while loop.
No, not all languages are the same. C is the same as PHP. but for example VB.NET: For value As Integer = 0 To 5 ' Exit condition if the value is three. If (value = 3) Then Exit For End If Console.WriteLine(value) Next Code (markup): This is simply saying count 0 to 5. setting the int in the var value. in Python: for x in range(0, 3): print "We're on time %d" % (x) Code (markup): in Delphi: begin // Loop 5 times For i := 1 to (10 div 2) do ShowMessage('i = '+IntToStr(i)); end; Code (markup): The basic principal of how you USE them is the same; but how you execute them in code is NOT the same.
Loop is a way that let us repeat the same action. For example, int i = 0; while (i < 10) { // Repeat the action 10 times } Code (markup): For loop is basically same with while loop, except we can initialize a variable in it. for (int i = 0; i < 10; i++) { // Repeat the action 10 times } Code (markup):
Basic logic remains same in almost every programming language. its the Syntax that changes. There are three common types of Loops 1) For Loop 2) While loop 3) Do while each loop is designed for some specific kind of situation. But you can use which ever you like. 1) For Loop it is preferably used when you know how many times loop will run. in other words you already the know the number of times same task will be repeated. example:- display number upto 100. for(i=0;i<=100;i++){} 2)While loop this is used when you do not know how many times you need to repeat the task. example:- Read contents from file untill you reach 'end of file'. Or repeat task until sum of two consecutive numbers exceeds 50. While(condition){} // as long as condition remain true, it will run 3) do While its same as While loop, but difference is that it will check condition after running loop once. example:- always calculate sum of first two consecutive numbers, and then check the condition do{ }while(condition); hope it will help you...
Try to code yourself with different loops logic the best programs for learning loops is to create the Pyramid of numbers and Diamond in any language that will help you to understand the logic building through loops and the concepts of loops for example. make this First code this 1 12 1234 123 12 1 then 1 1 2 1 2 3 1 2 3 4 1 2 3 1 2 1 AFter this code this * * * * * * * * * * * * * * * * AFter this your everything would have cleared for loops. and try to make with all loops not just with for or while loop.