Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Reading And Writing Variables

Writing out the value of variables

Now we have looked at the four elementary types in Pascal, namely

  • INTEGER
  • REAL
  • CHAR
  • STRING


I will finish this chapter with a short resume of how to write out the values held in variables, of any type in a Pascal program. Suppose that we have a variable called the_variable, the type does not matter here. Variables are written out by putting them inside the parenthesis (curved brackets) of either a WRITE or a WRITELN. The various ways of writing out the value of a variable are shown below.

WRITELN(the_variable);


If no surrounding text is required, a parenthesis at either side is all that is needed.

WRITELN('There will be ',the_variable);


If there is text only to the left of the variable, then a single quote and comma is needed to separate the text from the variable.

WRITELN(the_variable,' green bottles hanging on the wall');


If the variable has text to the right only, then a comma and a single quote separates it from the text.

WRITELN('There will be ',the_variable,' green bottles');


If there is to be text at either side of the variable then there needs to be a comma at either side of the variable and a single quote at each side to separate it from the text. Similar rules apply to writing out more than one variable, but variables must be separated from each other by commas.

WRITELN(the_variable,another_variable);


Of course it would be better if we put some text between them such as a space, as below.

WRITELN(the_variable,' ',another_variable);


RULE :- A variable needs either a parenthesis or a comma at either side. Text must have a single quote at either side of it. This last bit might seem a bit crazy, but without text, I hope it might be a little bit clearer.

WRITELN(' ',the_variable,' ',another_variable,' ');

Reading in the value of variables

The reading of variables into a program follows similar rules. Using a READLN you can read in a single variable at a time, or many variables at a time, though each variable must be separated by a comma.

READLN(the_variable);


One variable per READLN.

READLN(the_variable,another_variable);


Two variables per READLN.

READLN(first,second,third,fourth,fifth,sixth,seventh);


Seven variables per READLN. An important point to be aware of when typing in multiple variables to a single READLN is that the input variables must be separated by a space. For instance, if you were typing in input for this READLN.

WRITE('Enter three integers please : '); READLN(first_integer,second_integer,third_integer);


The display would look something like this.

Enter three integers please : 21 42 16


NOTE :- This is all very well, but for what it's worth I would recommend, at this point, that you stick with one variable per READLN. The reason might become clear later when I go into the intricacies of the READ and READLN in greater depth, but I will leave you to decide whether or not to take the advice.

 

Chapter 2

Control Structures




This chapter covers:

  • IF - THEN (selection)
  • IF - THEN - ELSE (selection)
  • REPEAT - UNTIL (looping)
  • FOR - TO - DO (looping)
  • CASE - OF (selection)
  • WHILE - DO (looping)


Also in this chapter :

  • Comparison operators.

1. Control Structures : The low-down

This chapter will deal with control structures. Effective use of control structures is the key to realising the full power of computer programming. Some of them, in the beginning, may seem too difficult to understand. I promise, you will get the hang of them. The programming language Pascal is an imperative programming language. BASIC, C, and Ada are but 3 examples. These imperative languages can only control the flow of a program in three ways.

  • Sequence
    Execute one single instruction, in a section of program, after another.
  • Select
    Choose, depending on a tested condition, between two, or more pathways through a section of a program.
  • Loop
    Executing a single instruction, or group of instructions, one or more times.


The proof that any imperative program of any complexity can be constructed from separate units of these three methods was provided by the mathematicians Böhm and Jacopini in the 1960s. Each of these three methods can be described graphically by the flowcharts below.


Figure 1 - Program Control Structures


The sequence flowchart is as straight forward as it seems. One instruction is followed by the next until the last is reached, at which point that section of program is completed. With selection, a point is reached in the program where an either or decision must be made. In Pascal we use the following.

  • Selection
    IF - THEN
    IF - THEN - ELSE
    CASE


With loops, if you wanted to execute the same statement five times, you would write it once and put it in a loop which would loop five times. In Pascal we use these three looping techniques.

  • Loops
    FOR
    REPEAT - UNTIL,
    WHILE


By far the simplest of these control structures to understand, is the IF - THEN and the IF - THEN - ELSE statements. So we will begin there.

2. Selection : IF - THEN & IF - THEN - ELSE

One of the main flow control points in programs involve making choices between options. At one of these points the program chooses which statement, or group of statements, to execute depending on the conditions in either an IF - THEN, an IF - THEN - ELSE, or a CASE statement. You can imagine someone making the statement : IF it is raining THEN put on hat and coat.

2.1. The idea is:

IF condition is true THEN carry out these instruction(s).
The IF - THEN - ELSE statement, works in much the same way as it would work in the everyday world. You use the IF - THEN - ELSE statement everyday and probably don't realise it. Let me explain. When a driver approaches traffic lights at a pedestrian crossing, a decision must be made. It goes something like this: IF the light is red THEN stop, ELSE IF it is green OR orange AND there is no one on the crossing THEN drive on. The AND and the OR are boolean logical constructs, this example should explain their use, but see below for the exact syntax.

2.2. {The idea is:

IF condition is true THEN carry out these instruction(s) ELSE carry out these instruction(s).
There can be as many IF - THEN - ELSE statements as you require, for your problem.
The condition, in the IF statement, has what is called a boolean result. That is, the result of the evaluation of the condition will be either True or False. An example of a simple condition, with a boolean result, is found in the first program in this chapter. In more complex programs, you might need to use more that one condition in an IF statement. You might need both of them to be True (light at green AND crosswalk clear). Or you might need only one of them to be True, (light at green OR orange). That is overcome with the use of the boolean constructs AND and OR.

Syntax

IF condition THEN instruction
Alternatively. IF condition THEN instruction ELSE instruction


Date: 2016-03-03; view: 669


<== previous page | next page ==>
More on the type STRING | Comparison Operators
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)