Pascal: Order of Execution of Program Statements

  • Thread starter Thread starter C0nfused
  • Start date Start date
  • Tags Tags
    Pascal
Click For Summary
In programming, particularly in Pascal and many other languages, statements are generally executed in the order they are written in the program. Each statement is executed once sequentially, unless modified by control structures like loops or conditionals, which can alter the flow of execution. For simple programs, unless specified otherwise, the execution follows a linear path from start to finish. While most statements execute once, loops can repeat code, and function or procedure calls are treated as statements that execute their contained code before returning to the main program flow. Advanced concepts such as multi-threading or event-driven programming can allow for simultaneous execution, but these are not typically encountered in basic programming scenarios. Understanding these foundational concepts is crucial for grasping how programs operate.
C0nfused
Messages
139
Reaction score
0
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
 
Computer science news on Phys.org
C0nfused said:
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


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

That means yes.
 
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
 
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 languages 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.
 
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
 
C0nfused said:
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


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.
 
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
 
Thanks for your answers. They were really helpful
 
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
 
  • #10
C0nfused said:
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

In a word, yes.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
3K
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K