Exam Question: Pascal: forloop + what is the problem with nesting?

Discussion in 'Programming' started by golasso, Jan 2, 2010.

  1. #1
    Hello everyone!

    I'm a student and soon my exams will start.. One of my courses is Programming.
    We have to understand all kinds of concepts among 6 languages (FORTRAN, Algol, Pascal, LISP, Smalltalk and Prolog)

    He gave us some example questions of what he could ask during the oral exam. One of the questions was:

    "What is nesting? Give an example of it in a for loop in Pascal + what is the problem there?"

    Ok, I understand what nesting is, but I don't know what the problem might be by nesting in a forloop? Does someone have an idea?

    Thanks a lot in advance!
     
    golasso, Jan 2, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nested for loop in pascal is rarely used due to confusion of Begin and End. (Begin only comes if there exists any code and End comes if and only if Begin is there).
    Consider following definition.

    for {loop counter} := {original value} to {final value} do {Begin-if required}
    
       {[B]code if any..begin should be included[/B] (i.e more than one action)}
    
       for {loop counter} := {original value} to {final value} do {Begin-if required}
    
            {[B]code..if more than one action, include begin in the second for loop[/B]}
    
       {End; - [B]if begin is included in the second for loop[/B])}
    
       {[B]code if any..begin should be included in the first for loop[/B]}  
    
    {End; - [B]if begin is included in the first for loop[/B])}
    Code (markup):
     
    NeoCambell, Jan 2, 2010 IP
  3. proud2bpinoy

    proud2bpinoy Peon

    Messages:
    225
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nesting can be explained simpler in html

    Properly nested
    <i><b></b></i>
    HTML:
    Not nested properly
    <i><b></i></b>
    HTML:
     
    proud2bpinoy, Jan 2, 2010 IP
  4. golasso

    golasso Active Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    AWESOME :D

    Thanks a lot for your help!!
     
    golasso, Jan 3, 2010 IP