Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Subscribe to DeepL Pro to translate larger documents.

Visit www.DeepL.com/pro for more information.

1 Classification of
❶ program
Programming Languages

To be processed by computer

The overall work of creating the
It is called programming. program. programming
Also. The means of expressing a
program is called a con

The word "computer" means a It is called a ➌ programming lan-
guage
language understood by a computer. programming language.
Programming languages can be classified as shown in It can also be just a language.
10

Figure 1. There are.

machine
programming language
Interpreter language (e.g. BASIC)
language assembly
Compiler language (C, Java, etc.)
language
high-level
language
Figure 1 Programming Language

The computer directly executes the


1 machi
Languages that can be It is called

➍ machine language
ne
machine
langu language, and it is
age used in the con
It is a language specific to each central processing unit of a as an electrical signal to

computer. In machine language, the computer reads ",1" or "0" proceed with processing.

82 Chapter 4
rafting
Programming
To make machine
A machine language program is a
2 assembly language easier to

1
language series of numbers read, numerical
sequences of "1" and
and is difficult for humans to
"0" are often

Programming Basics
understand. The represented
hexadecimal
using

What you will learn in this section numbers.


15

When processing with a computer, it is necessary to execute instructions according to a
In order to make the program easier to Instruction write- Ⓖ See p. 93 for details.
predetermined procedure. This collection of instructions is called a program. In this
create, the in
codes andlanguages,
section, you will learn about programming flow diagrams, and the basic
The symbols structure
representofthe address of the storage location to be
programs.
viewed or read, etc. ❻ assembly language 20
(in Japanese) ❻
The language that was considered was called assembly
translati
language. Such a language is called assembly
on language.
Translate programs written in assembly language to
machine language programs.
(after a noun, adjective
software that outputs a ram. stem, onomatopoeic- ❼ assembler
mimetic word, etc.)

It is called an
assemblage.
Since the instruction code of assembly language may differ
depending on hardware such as central processing units,
many types of assembly languages exist.

Located at. 25

1 Programming Basics 83

High-level languages are easy for humans to understand ❶ high-level language


3 high-level
The language is a language that
The language is
language designed so that it can
uses imperative words designed to be used in the same way as be commanded using
terms similar to English
the language itself. There are various types of high-level and expressions
languages, each with its own characteristics. Table 1 shows such as "c = a + b".

examples of typical high-level languages.


Table 1 Examples of high-level languages

Langua Outline outline Program Examples


ge

FORT RUN
WRITE(*,10)
10 FORMAT('Hello')
Fortran A language developed for scientific and
STOP
technical computing. END

cobol PROCEDURE DIVISION:


COBOL It is widely used as a language for office DISPLAY "Hello".
STOP RUN.
work.
Basic
PRINT "HELLO"
BASIC Language developed for beginners.
END

#include <stdio.h>
sea int main(void){
C Widely used language in general use. printf("Hello¥n");
return 0; return
}

#include <iostream>
C++
A language developed from C. Data and using namespace std;
C++ operations can be described together.
int main(){
cout<<"Hello"<<endl;
}

A language in the vein of C and C++, which public❷ class Hello{


interpreter language
public static void main(String args[]){
can be used a
n
iy
environment (software) capable of
Java
5 High-level
Java languages can be compiled with interpretedSystem.out.println("Hello");
compiler language
executing Java, and the translation results
languages depending on how they are executed. }
➌ can be executed independently of the }
device
It is classified model.
as an Ila language.
a Interpreter language

A program written in an interpreted language is executed


by the computer while interpreting the instructions in the
program one by one. The program is executed by the
computer interpreting the instructions in the program one by
one.
10 The advantage is that the program can be executed
immediately, but the program's actual
It has the disadvantage of slow line speed. Programs written in
interpreted language

Software that executes while b Compiler
It is called an interpreter.
interpreting the language

84 Chapter 4
Programming
➍ interpreter

A program written in a compiler language is a batch program, in which the entire


program is
15 The advantage is that the program execution speed after
translation is fast, but the disadvantage is that the program
cannot be executed until all the translation work is
completed. The advantage is that the program execution
speed after translation is fast, but the disadvantage is that the
program cannot be executed until all the translation work is
completed.
Translate a program written in a compiler language into a machine language program.

Software for It is called a Ⓖ compiler

compiler.

1 Programming Basics 85
2 Primitive and Objective Programs
Assembly and high-level languages have their

1 Resource
Each of them has its own writing ❶ coding
program conventions, and this
is called a grammar. Writing a program according to the file
grammar is called coding.
❶❶ ❷ Day according to certain rules
The coding program is compiled as a file and stored in the same A grouping of two or more 5
directory. The coded program is compiled as a file persons.

It can be done. This is called a It is called a source ➌ source program

primitive program or program.


Execute the primitive program
while interpreting it, or translate

and other software. it into a machine language ➍ language processor


2 object
program.
program ➍
It is called a language processor.
Assemblers and con
The machine language program created by the pyramid is used as the target program or 10


the execution of the
is called an object program.
target program. Even if
Figure 2 shows how the language processor works.
the target program is
Execute one instruction
source program

at a time,
grammatically correct
program.
It's a primitive

interpreting it as
you go.
and can be executed to
program
source

the end, if there is an

assembler compile interpreter


error in the processing
r-language processor
procedure, the computer
may produce incorrect
results.
object program (in
Japanese)
Figure 2 How the Language Processor Works ❻
These errors are called
bugs. If you find a bug,
If there is a grammatical error in you need a primitive
3 Program errors
the middle of the primitive program.

program, the language processor


will make an error
The computer informs the user of the location of the If there
is an inconsistent operation, such as dividing by zero, during
the execution of the target program, the computer will stop

86 Chapter 4
Programming
Ⓖ object program

15

❻ bug
Including
grammatical errors
and logical errors in
execution.
All errors. 20
(after a noun,
adjective stem, ❼ debug
It must be corrected. This work
onomatopoeic-
mimetic word, etc.)
should be
It is called
debugging.

1 Programming Basics 87
3 Flow Chart and Program Structure
To be processed using a computer

1 flowch In order to achieve this, the following


art

Analyze a problem, create a program to solve it, and execute the


program.
5 In this case, it is necessary to process them in the correct order.
The hand of this processing

The order is called algorithm or ❶ algorithm

arithmetic.Flow
and
chart ❷ flowchart

A visual representation of the algorithm.


The use of flow diagrams increases the efficiency of program
development because the algorithm and coding correspond.
The use of flow diagrams increases the efficiency of program
development because algorithms and coding correspond.
10 As a rule, the direction of flow in a flow chart is promised
to be from top to bottom and from left to right. When the flow
does not conform to these directions, arrows are used to
indicate the flow.
The graphical symbols used in flow charts are called flow
chart symbols, and are designed to v i s u a l l y represent the
contents of each process. Table 2 shows the main flowchart
symbols.
Table 2 Main flow chart symbols
name

name

graphic meaning meaning graphic meaning meaning


symbol symbol

All kinds of processing, such


ing
process

ents
docum manual input

Printer output and other data


as performing arithmetic
operations
reserve

Preparation for processing, Keyboard input and other


including setting initial data
values
nt
judgme

display

Judgment by conditions Display indications and other


data

88 Chapter 4
Programming
port (e.g. LAN
Loop start (beginning of loop)

loop end
Start, end of flow chart

port)
Loop termination (end of
loop)
process
defined

n
associatio ray (e.g. X-
Processes defined elsewhere Flow chart connections

Input and output of data


data

Process Flow

ray)
without specifying the
medium

1 Programming Basics 89
first doing of first first
doing of doing
... (ever, in
... (ever, of ...
one's life, in
in one's (ever,
the new year, NO
xlife,
≥ in100 in
etc.) 1
the new
YES ←S+N
S one's
year, life, in
xRead
← (a +the
b) value
/2 Output
etc.) ←N+1
Nthe
S
of a of x
new output
Output of x Input x NO
year,
N > 10
Read the value
etc.) YES end
of bthe end the
end
S←
1 0
Flowchart for inputting Flow chart for
two numbers a and b outputting x when the Find the ← 1 S of positive integers 1 to 10.
Nsum
and outputting their input number x is Flow chart for outputting S.
average x. greater than or equal
to 100. (c)
(a)
(b)
Figure 3 Example of flow chart

An example flow chart is shown in Figure 3. As shown in a, b, and x in Figure 3( a ) , the


processing and

Both have a changing It is called a ❶ variable

value. variable.
When obtaining an answer from the number or order of
processes, as in Figure 3( c ), the values of the variables must be
determined in advance before processing. This kind of ❷ initialization 5

The operation is called initialization, and the value assigned to a Initial value
variable in initialization is called the initial value.

The term value is called the period value.
Figure 3( c ) shows a flowchart for finding the sum from 1
to 10, counting the number of iterations in N and finding first doing of ...
10
(ever, in one's
the sum in S. In this algorithm, S and N are initialized with
life, in the new
the initial value 0 and 1, respectively.
year, etc.)
When N exceeds 10, the loop is terminated. Figure 3( c )
S←0
can also be drawn with a loop end symbol as shown in
N←
S← S +1N
Figure 4. In this example, an exit condition to escape from
loop 15
repetition is written at the end of the loop. N←N+1

N > 10
loop
at end
conditio S
90 Chapter 4
n Output
Programming
the
Figure 4 Example of flow
chart with loop
ends

1 Programming Basics 91
How the value of a Table 3 Trace Table
2 trace
variable changes in a
certain process
❶�
The process of tra
scalled
g
cin Figure 3( c )Iteration process
freque sadist N N > 10
Table 3 shows the results of tracing the Such a table can be ncy
0 0 1 -
traced
1 1 2 NO
5 The algorithm in question is called an algorithm
2 3 3 NO
table, and it is used to check whether the algorithm in 3 6 4 NO
question is a correct procedure to be processed. 4 10 5 NO

Do. 5 15 6 NO
countermeasure
6 21 7 NO
esothold c( ), when
In Figure 3( c ) , the condition N > 10 do
n
7 28 8 NO
➌)
Branch to NO and repeat the process. Fulfills (YoES)
the when
condition (true 8 36 9 NO
u
The repeatability is escaped by branching out to 9 45 10 NO
nt
10

10 55❶ trace11 YES


er
❷ false
fe
➌ true
it

Questi In the flow chart in Figure 5, find the output results


ofonM,
1 P, and K w h e n t h e data D below is input in order from

left to right.

<Data D > 6, 9, 2, 7, 4, 3, 9, 3, 1, 0

92 Chapter 4
Programming
first
doing of
... (ever,
in one's
life, in
the new
year,
etc.) YES
D=0

M ← 0 NO
K P←←K0+ 1
K←0

Enter D YES
D<0 Output M, P, K

NO M←M+D
P←P+D

Enter D the
end
Figure 5 Flow chart

1 Programming Basics 93
structured To write a large program
3 programming efficiently, the program structure
-ing
must be able to

The information should be as easy Structured programming is ❶ structured program
to understand as possible. a simple
ming
The rules were designed to create a clean and understandable
program. 5

In structured programming, even a complex process is


considered to be a combination of several processes. Each
process always has one entry point and one exit point. There
are three types of basic program structures: sequential,
selective, and iterative. Since the basic structure can be
10

contained within the sequential structure, the program can be


considered hierarchically as shown in Figure 6.

sequenti
al
structure repetitive
structure

sequenti
al
structure

choice
structure

Figure 6 Structured programming example

a Sequential structure

A structure in which the flow of processing is linear is called


a sequential structure. Figure 7 shows a flow diagram of a
sequential structure. Each of them shows that process B is
performed after process A is completed.

94 Chapter 4
Programming
Processing A
Processing A
Processing B
Processing B

(a) (b)
Figure 7
Sequential
structure

1 Programming Basics 95
b Selective structure

2 One process is selected by a certain condition from among more


than two processes.
A structure in which the structure is selective is called a selective
structure. Figure 8 shows a flow diagram of a selective structure.

Theflowcharts(a)and(b)areshownbelow.(The Both represent the same process. ❶ Figure 8(a) shows a
comparison of the values
flowcharts in (a) and(b) are Figure 8(b) shows the
selection by
5 As shown in ( c ), when process D is unnecessary, it is omitted. indicates selection by
the truth or falsity of
Selective structure.
the comparison
expression.

≦ NO NO
X: Y X>Y X>Y
> YES YES
Processing Processing Processing Processing Processing
C D C D C

(a) (b) (c)


Figure 8
Selection
c Repetitive
Structure
structure
A structure that repeats the same process under certain
conditions is called a repeating structure. Figure 9 shows a flow
diagram of a repeating structure. A repeating structure includes
(a)
predecision post-definite (e.g. matrix)

10 There are pre-determination forms such as ( a ) and


post-determination forms such as ( b ).
Note that when iterative processing is written using the loop
end symbol, the exit condition for exiting the loop is written at
the beginning o r end of the loop.

Loop
name loop name
NO Processing
conditio end
n of conditio E
n
continua Processing Processing
YES
nce E NO E
Process contin the
ing E loop name ued afo End
conditio
article res n loop
name
aid

96 Chapter 4 YES

Programming
Conditional decisions are made before Conditional decisions are made after processing.
processing. Even if the continuation condition is
If the continuation condition is false false from the beginning, process E is
executed only once, the first time.
from the beginning, the process
E is never executed. (b) Post-determination type
Figure 9 Repeat
(a) Pre-determined form
structure

1 Programming Basics 97
Example
1 Figures 10(a) to 10(c) show the flow chart for finding the
sum S of odd numbers from 3 to 9. Fill in the blanks (1)
through (8).

first first first


doing doing of doing of
of ... ... (ever, ... (ever,
(ever, in in one's in one's
one's life, in life, in
Yes
life, in the new the new
the year, year,
new etc.) etc.)
Output
(6)
year, S
etc.) (4) Output S←0
S
S←0 N
N←3 N←N+2 the end Three, nine, two.
the end

(8)
(5)
(1)
No N

(2) N: 9

(7) S
(3)
output

end

(a) (b) (c)


Figure 10.

explanation
) is added to S by increasing N from 3 to 9 in increments of 2.

a blanks
After adding N to S, add 2 to N. If N exceeds 9, escape the loop. (1) through
) N is initially 1.

b
(3 ) .
) uses the loop end. In this book, N is represented by 2 from 3

c
to 9.

Repeating the process while increasing is written as "n 3, 9, 2".
answer
(1) N > 9 (N = 11 is also acceptable) (2) S ← S + N (3) N ← N + 2
(4) S←0, N←1 (5) S←S+N (6) ≧ (7) < (8) S←S+N

first
Questi Figure 11 shows a flowchart for
doing of
on 2
reading three sets of data A, B, and C and ... (ever,

outputting the maximum value. Fill in the in one's


M←A
life, in
the new
98 Chapter 4 (1)
NO
year,
Programming YES
etc.)
M←B
5 order.

1
15
10 NO
(2)
Write the initial value, closing value, and increment in this YES
(3)

M
Output

Figure 11. the end

1 Programming Basics 99

You might also like