Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pascal question

  1. Jan 30, 2005 #1
    Hi everybody,
    I am new in programming. I just want to ask this: when we write a program the statements are executed in the order that we have written them in the body of the program? This is about Pascal but does it apply to other languages too? Of course some statements "change the order" causing some to be executed more than once or not at all ("while" loop etc) but generally the order we write them is the order that are executed? And only one statement is executed at a time and not two for example simultaneously?
    Thanks
     
  2. jcsd
  3. Jan 30, 2005 #2

    I would hope so, otherwise program writing would be incredibly difficult.

    That means yes.
     
  4. Jan 30, 2005 #3
    Thanks for your answer. Just one more thing: every statement(except for the statements in the loops and the statements in the "if" statement) is only executed once and then the pc goes to the next statement, executes it once and goes on until it reaches the end of the program?
    These may be too easy for most of you but i just want to get the basics of how a program operates in a pc
    Thanks again
     
  5. Jan 30, 2005 #4

    dduardo

    User Avatar
    Staff Emeritus

    Actually, statements can run simultaneously in some cases. For example, if your writing a multi-threaded application you can fork a process and run another piece of code concurrently. Another case is in an an event driven system. If two or more events are triggered simultaneously, the code for the events will run together. Finally, programming langauges like VHDL, which is used to design hardware, allows procedures to run simultaneously. If this wasn't the case, digital chips would run very slow.
     
  6. Jan 30, 2005 #5
    Thanks for your answer, but I must admit i didn't understand much. My question
    "every statement(except for the statements in the loops and the statements in the "if" statement) is only executed once and then the pc goes to the next statement, executes it once and goes on until it reaches the end of the program?"
    is for simple programs made in Pascal! And I would like to know what is going on in such programs.
    Thanks anyway
     
  7. Jan 30, 2005 #6

    The things that dduardo mentioned are more advanced concepts. For the things it sounds like you'd be doing, you don't need to worry about it really. Unless you specify otherwise, the program will execute in the order the program is written.
     
  8. Jan 30, 2005 #7
    In most cases a statement will only be executed once. However, the order can change when using conditionals (if, else, etc). Also, loops (for, while) will execute the same code more than once, usually with some sort of a modification: ie:
    for (int i = 0; i < 5; i++) where i is being incremented by for each iteration

    And functions can be called multiple times.


    Ryan
     
  9. Jan 30, 2005 #8
    Thanks for your answers. They were really helpful
     
  10. Jan 30, 2005 #9
    And one more thing that i forgot: when we "call" a procedure in the body of a program ,is it considered as an execution of a statement? In other words, procedure call is a statement, which is executed like all other statements, and when executed, all the statements in the body of the procedure are executed (similar to the way the main program statements are executed) and after that, the program continues normally with the statements in the main body?
    Thanks again
     
  11. Jan 30, 2005 #10
    In a word, yes.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Pascal question
  1. Please help pascal (Replies: 1)

  2. Pascal question2 (Replies: 0)

  3. Beginner's questions (Replies: 7)

Loading...