Pascal: Order of Execution of Program Statements

  • Thread starter C0nfused
  • Start date
  • Tags
    Pascal
In summary: When a procedure is called, it is considered an execution of a statement. The statements within the procedure are then executed, and once they are completed, the program continues with the statements in the main body as usual. This applies to most programming languages, including Pascal. In summary, statements are executed in the order they are written in a program, with some exceptions such as loops and conditionals. Procedures are considered as statements and are executed before the program continues with the remaining statements in the main body.
  • #1
C0nfused
139
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
  • #2
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.
 
  • #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
 
  • #4
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.
 
  • #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
 
  • #6
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.
 
  • #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
 
  • #8
Thanks for your answers. They were really helpful
 
  • #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
 
  • #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.
 

1. What is the order of execution for program statements in Pascal?

The order of execution for program statements in Pascal is from top to bottom, following the structure of the program. This means that the program will start executing at the first statement and continue until it reaches the last statement.

2. Can statements be executed out of order in a Pascal program?

No, statements in a Pascal program cannot be executed out of order. The program will always follow the sequential structure and execute statements in the order they are written.

3. What is the purpose of the "begin" and "end" keywords in Pascal?

The "begin" and "end" keywords are used to wrap a group of statements together in Pascal. This is known as a block of code and allows for multiple statements to be executed as a single unit. It also helps to improve the readability and organization of the program.

4. What happens if a statement in a Pascal program is missing a semicolon at the end?

If a statement in a Pascal program is missing a semicolon at the end, the program will not run and an error will occur. Semicolons are used to indicate the end of a statement, so it is important to include them in the correct places.

5. How does Pascal handle conditional statements in its order of execution?

In Pascal, conditional statements such as "if" and "while" are executed only if their conditions are met. These statements do not affect the overall order of execution of the program, but rather provide a way for certain statements to be executed only under specific conditions.

Similar threads

  • Computing and Technology
Replies
2
Views
636
  • Computing and Technology
Replies
12
Views
2K
  • Computing and Technology
Replies
9
Views
1K
Replies
6
Views
1K
Replies
21
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
  • Computing and Technology
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
22
Views
921
  • Computing and Technology
Replies
2
Views
725
Back
Top