Pascal: Questions on Variables, Arrays & Booleans

  • Thread starter C0nfused
  • Start date
  • Tags
    Pascal
In summary: If the procedure simply computes the sum and returns an integer value, you could use "function sum(a,b:integer):integer;" as the prototype.In summary, variables are named memory locations used to store data and can be retrieved by using their names in expressions. Boolean type variables can be used in if and while statements by using comparison operators. Arrays contain a series of memory locations and each cell is the same as a simple variable of the specified type. Procedures and functions can use array cells as parameters as long as they are the same type as the formal parameters.
  • #1
C0nfused
139
0
Hi everybody,
Probably some easy questions in pascal:1) what exactly is a variable?Is it just a "place" to store data? And how do we retrieve this data from variables? When we write a variable's name in an expression the value of the variable is used in this expression?For example when we have an integer variable named "x" then when we write (3*x+5) the pc retrieves the value of x and uses it to calculate the value of the expression? So the name of the variable "represents" its value?

2) When we have a boolean type variable, named for example "check" are we allowed to use it in the following way: " if check then <statement1> else <statement2> " or like this :
" while check do <statement3> " ? In these cases the variable is like a boolean expression whose value(true/false) is the value of the boolean type variable? So the above examples are the same as these:
" if (check=true) then <statement1> else <statement2> " and
" while (check=true) do <statement3> " ?
This also applies when we write "(not check")?This is the same as (check=false) ? Generally writing " check and test" (where test is another boolean type variable) is like writing "((check=true) and (test=true))" ? They are all considered boolean expressions?
And one thing about arrays: when we use an array [1..10] of integer for example named "ar" then each cell of the array(ar[1],ar[2] etc) is exactly the same as a simple integer variable? So if we have a procedure that uses a variable parameter and a value parameter, for example
" procedure sum(a:integer;var b:integer); etc " then we can call it in the main body of the porgram using two of the cells of the array as real parameters of the procedure? For example we can make this call of the above procedure: " sum(ar[1],ar[2+3]) ? This applies to any kind of arrays that have the same type of values( integer,real etc) with the type of the parameters of the procedure?

Thanks
 
Last edited:
Computer science news on Phys.org
  • #2
Very late reply in case others have similar questions...
C0nfused said:
Hi everybody,
Probably some easy questions in pascal:1) what exactly is a variable?Is it just a "place" to store data?
Pretty much. I like to define a variable as a memory location that is named. The actual size of the memory depends on the type used when the variable is declared.
C0nfused said:
And how do we retrieve this data from variables? When we write a variable's name in an expression the value of the variable is used in this expression?
Yes.
C0nfused said:
For example when we have an integer variable named "x" then when we write (3*x+5) the pc retrieves the value of x and uses it to calculate the value of the expression?
The program code evaluates the expression 3*x + 5 by using the current value of x.
C0nfused said:
So the name of the variable "represents" its value?
Yes
C0nfused said:
2) When we have a boolean type variable, named for example "check" are we allowed to use it in the following way: " if check then <statement1> else <statement2> " or like this :
" while check do <statement3> " ? In these cases the variable is like a boolean expression whose value(true/false) is the value of the boolean type variable? So the above examples are the same as these:
" if (check=true) then <statement1> else <statement2> " and
" while (check=true) do <statement3> " ?
It's been a very long time since I wrote any Pascal code, but I believe that all of what you wrote is mostly correct. One difference between Pascal and languages derived from C is that Pascal uses = for comparison and := for assignment, which C etc. use == for comparison and = for assignment.
The only thing I see wrong is "while check do ...". This should be "while (check) do ..."
C0nfused said:
This also applies when we write "(not check")?This is the same as (check=false) ? Generally writing " check and test" (where test is another boolean type variable) is like writing "((check=true) and (test=true))" ? They are all considered boolean expressions?
Yes. If check is a Boolean variable, then check = false is a Boolean expression that could be true or false, depending on the value of check.
C0nfused said:
And one thing about arrays: when we use an array [1..10] of integer for example named "ar" then each cell of the array(ar[1],ar[2] etc) is exactly the same as a simple integer variable?
Yes, assuming that the type of the array is integer. In that case, each cell of the array is an integer.
C0nfused said:
So if we have a procedure that uses a variable parameter and a value parameter, for example
" procedure sum(a:integer;var b:integer); etc " then we can call it in the main body of the porgram using two of the cells of the array as real parameters of the procedure? For example we can make this call of the above procedure: " sum(ar[1],ar[2+3]) ? This applies to any kind of arrays that have the same type of values( integer,real etc) with the type of the parameters of the procedure?
Yes. As long as the actual arguments (ar[1] and ar[5]) are the right type, they would match the formal parameters, both of which are type integer. Keep in mind though, that the var prefix suggests that the second parameter is an "out" parameter, and can have its value changed.
 

1. What is a variable in Pascal?

A variable in Pascal is a named location in memory that stores a value. It can hold different types of data, such as numbers, characters, or Boolean values.

2. How do you declare a variable in Pascal?

To declare a variable in Pascal, you use the var keyword followed by the variable name and its type. For example: var age: integer;

3. What are arrays in Pascal?

Arrays in Pascal are data structures that allow you to store a collection of values of the same type. They are declared using the array keyword and can be either static or dynamic in size.

4. How do you access elements in an array in Pascal?

To access elements in an array in Pascal, you use the array name followed by the index of the element you want to access in square brackets. For example: myArray[2] would access the third element in the array.

5. What are Booleans in Pascal?

Booleans in Pascal are variables that can only hold two values: true or false. They are used to represent logical values and are often used in conditional statements and comparisons.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
20
Views
1K
  • Set Theory, Logic, Probability, Statistics
2
Replies
40
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
927
  • Programming and Computer Science
Replies
16
Views
1K
Back
Top