1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I better understand loops?

Discussion in 'Programming' started by jv170, Oct 10, 2013.

  1. #1
    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.
     
    jv170, Oct 10, 2013 IP
  2. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #2
    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++)
     
    HowDoYou, Oct 10, 2013 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    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.
     
    ThePHPMaster, Oct 10, 2013 IP
  4. jv170

    jv170 Member

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    I'm looking in c language. Sorry for not mentioning it.
     
    jv170, Oct 10, 2013 IP
  5. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #5
    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.
     
    HowDoYou, Oct 11, 2013 IP
    ryan_uk and ThePHPMaster like this.
  6. HerksAw

    HerksAw Greenhorn

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    21
    #6
    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):
     
    HerksAw, Oct 11, 2013 IP
  7. cristina.emerson

    cristina.emerson Member

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #7
    Hey there,

    if you want to know more about c for loop , do more and more practice in series.
     
    cristina.emerson, Oct 14, 2013 IP
  8. rehan.khalid.9235

    rehan.khalid.9235 Active Member

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    85
    #8
    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...
     
    rehan.khalid.9235, Oct 18, 2013 IP
    ryan_uk likes this.
  9. kourang

    kourang Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #9
    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.
     
    kourang, Oct 19, 2013 IP
  10. uprentiss

    uprentiss Greenhorn

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    16
    #10
    this should help you bro

    http://www.cprogramming.com/tutorial/c/lesson3.html
     
    uprentiss, Oct 19, 2013 IP
  11. jv170

    jv170 Member

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #11
    Hi thanks everyone you huys have helped me a lot.
     
    jv170, Oct 21, 2013 IP