0% found this document useful (0 votes)
12 views

Programming

The document provides an overview of programming languages, focusing on Python, Visual Basic, and Java, along with their respective integrated development environments. It covers fundamental programming concepts such as data use, sequence, selection, iteration, and the importance of variables, constants, and data types. Additionally, it discusses input/output handling, string manipulation, procedures and functions, and file handling techniques essential for effective programming.

Uploaded by

khushimehta1231
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Programming

The document provides an overview of programming languages, focusing on Python, Visual Basic, and Java, along with their respective integrated development environments. It covers fundamental programming concepts such as data use, sequence, selection, iteration, and the importance of variables, constants, and data types. Additionally, it discusses input/output handling, string manipulation, procedures and functions, and file handling techniques essential for effective programming.

Uploaded by

khushimehta1231
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming

Programming Languages

There are many high-level programming languages to choose from. We will only be treating
Python, Visual Basic, or Java.

 Python is an open-source, versatile programming language that encourages quick program


development and emphasises code readability. The integrated development environment
(IDE) showcased in this chapter is referred to as IDLE.

 Visual Basic is a popular programming language that is extensively used for Windows
development. The integrated development environment (IDE) featured in this chapter is
known as Visual Studio, which is utilised for capturing screenshots.

 Java is a widely adopted programming language utilised by numerous developers. The


integrated development environment (IDE) employed for capturing screenshots in this
chapter is known as BlueJ.

Programming Concepts

Constructs of a Program

 Data use – variables, constants and arrays

 Sequence – order of steps in a task

 Selection – choosing a path through a program

 Iteration – repetition of a sequence of steps in a program

 Operators use arithmetic for calculations and logic and Boolean for decisions.

Variables and Constants

 A variable within a computer program refers to a named storage unit with a value that can
be modified throughout the program's execution. To enhance comprehension for others, it is
advisable to assign significant names to variables.

 A constant within a computer program represents a named storage unit that holds a value
which remains unchanged throughout the program's execution. Similar to variables, it is
recommended to assign meaningful names to constants to enhance comprehensibility for
others.

Data Types

 Different data types are assigned to computer systems for effective processing and storage.

 Data types allow data, such as numbers or characters, to be stored appropriately.

 Data types enable effective manipulation using mathematical operators for numbers and
character concatenation.

 Some data types provide automatic validation.

 The types of datatypes are told in Chapter 1 already!

Input and Output


 Programs require input and output statements to handle data.

 In IGCSE Computer Science, algorithms and programs are designed to take input from a
keyboard and output to a screen.

 Prompting the user with clear instructions for input is necessary for the user to understand
what is expected.

 Input data in programming languages must match the required data type of the variable
where it will be stored.

 By default, inputs are treated as strings, but commands can convert input to integer or real
number data types.

 Users should be provided with information about the output/results for a program to be
useful.

 Each output should be accompanied by a message explaining the result's meaning or


significance.

 If an output statement has multiple parts, they can be separated by a separator character.

Basic Concepts

When writing the steps required to solve a problem, the following concepts need to be used and
understood:

 Sequence

 Selection

 Iteration

 Counting and totalling

 String handling

 Use of operators.

Sequence

The ordering of the steps in an algorithm is very important. An incorrect order can lead to incorrect
results and/or extra steps that are not required by the task.

Selection

Selection is a very useful technique, allowing different routes through the steps of a program. The
code of this is explained in the notes of previous chapters.

Iteration

As explained in the previous chapter, we already

Totalling and Counting

As explained in the previous chapter, we already

String Handling
 Strings are used to store text and can contain various characters.

 An empty string has no characters, while the programming language specifies the maximum
number of characters allowed.

 Characters in a string can be identified by their position number, starting from either zero or
one, depending on the programming language.

 String handling is an important aspect of programming.

 In IGCSE Computer Science, you will need to write algorithms and programs for the following
string methods:

o Length: Determines the number of characters in a string, including spaces.

o Substring: Extracts a portion of a string.

o Upper: Converts all letters in a string to uppercase.

o Lower: Converts all letters in a string to lowercase.

 These string manipulation methods are commonly provided in programming languages


through library routines.

Finding the length of a string:

LENGTH("Text Here")

LENGTH(Variable)

Extracting a substring from a string:

SUBSTRING("Computer Science", 10, 7)

// returns the next 7 values starting from the 10th value of the string "Computer Science" i.e.
"Science"

SUBSTRING(Variable, Position, Length)

Converting a string to upper case

UCASE("Text here")

UCASE(Variable)

Converting a string to lowercase

LCASE("Text Here")

LCASE(Variable)

Arithmetic, Logical and Boolean Operators

As explained in the previous chapter, we already


Use of Nested Statements

 Selection and iteration statements can be nested, meaning one statement can be placed
inside another.

 Nested statements help reduce code duplication and simplify testing of programs.

 Different types of constructs can be nested within each other, such as selection statements
within condition-controlled loops or loops within other loops.

Procedures and Functions

 A procedure refers to a collection of programming statements organized under a single


name, invoked at any given point in a program to execute a specific task.

 A function is a compilation of programming statements consolidated under a singular name,


invoked at any moment within a program to accomplish a particular task. Unlike a procedure,
a function also has the capability to return a value back to the main program.

 Parameters refer to variables that store the values of arguments passed to a procedure or
function. While not all procedures and functions require parameters, some utilize them to
facilitate their operations.

Procedures without parameters:

PROCEDURE ProcedureName ()

[Commands]

ENDPROCEDURE

//Calling/running the procedure

CALL ProcedureName()

The procedure with parameters:

PROCEDURE ProcedureName (ParameterName : ParameterDatatype)

[Commands]

ENDPROCEDURE

//Calling/running the procedure

CALL ProcedureName (ParameterValue)

Function:

FUNCTION FunctionName (ParameterName : ParameterDatatype) RETURNS


DataTypeOfValueReturned

[Commands]

RETURN ValueToBeReturned

ENDFUNCTION

 When defining procedures and functions, the header is the first statement in the definition.
 The header includes:

o The name of the procedure or function.

o Parameters passed to the procedure or function, along with their data types.

o The data type of the return value for a function.

 Procedure calls are standalone statements.

 Function calls are made as part of an expression, typically on the right-hand side.

Local and Global Variable

 Any part of a program can use a global variable – its scope covers the whole program

 A local variable can only be used by the part of the program it is declared in – its scope is
restricted to that part of the program.

Note: Any variables/arrays made in this procedure and functions will be local and cannot be used
out of these. To be made available all over the program, they must be declared globally in the
following way.

DECLARE [VariableName] : DataType AS GLOBAL

Library Routines

 Programming language development systems often provide library routines that can be
readily incorporated into programs.

 Library routines are pre-tested and ready for use, making programming tasks easier.

 Integrated Development Environments (IDEs) typically include a standard library of functions


and procedures.

 Standard library routines perform various tasks, including string handling.

 MOD – returns the remainder of a division

 DIV – returns the quotient (i.e. the whole number part) of a division

 ROUND – returns a value rounded to a given number of decimal places

 RANDOM – returns a random number.

Examples:

 Value1 <--- MOD(10,3) returns the remainder of 10 divided by 3

 Value2 <---- DIV(10,3) returns the quotient of 10 divided by 3

 Value3 <--- ROUND(6.97354, 2) returns the value rounded to 2 decimal places

 Value4 <--- RANDOM() returns a random number between 0 and 1 inclusive

Creating a Maintainable Program

A maintainable program should:


 always use meaningful identifier names for variables, constants, arrays, procedures and
functions

 be divided into modules for each task using procedures and functions

 be fully commented using your programming language’s commenting feature

Commenting in pseudocode:

// Now the text written is commented and thus ignored

""

This method can also be used to comment

multiple lines but the singular line method

is more widely accepted and reccomended too

""

Arrays

 An array is a data structure containing several elements of the same data type; these
elements can be accessed using the same identifier name.

 The position of each element in an array is identified using the array’s index.

 There are two types of arrays

One-Dimensional Array

Explained in the previous chapter in detail

Two-Dimensional Array

 A two-dimensional array can be referred to as a table with rows and columns.

 When a two-dimensional
array is declared in pseudocode, the first and last index values for rows and the first and last
index values for columns alongside the data type are included.
Declaring a 2D Array:

DECLARE Name : ARRAY[RowLower:RowUpper,ColumnLower:ColumnUpper] OF DATATYPE

Filling a 2-D array using a loop:

FOR ColumnCounter ← 0 TO 2

FOR RowCounter ← 0 TO 9

OUTPUT "Enter next value "

INPUT ArrayName [RowCounter, ColumnCounter]

NEXT RowCounter

NEXT ColumnCounter

File Handling

 Computer programs store data that will be needed again in a file.

 Data stored in RAM is volatile and will be lost when the computer is powered off.

 Data saved to a file is stored permanently, allowing it to be accessed by the same program at
a later date or by other programs.

 Stored data in a file can be transferred and used on other computers.

 The storage of data in files is a commonly used feature in programming.

Key point: When writing in a file, the program is outputing the data to the file, and when reading a
file, the program in inputing the data from the file

\n There are 3 ways a file can be opened in a program i.e. to write, to read and to append

Writing in a file

OPENFILE "filename.txt" FOR WRITE

//When opening a file to write, all the data already existing in the file is OVERWRITTEN

WRITEFILE "filename.txt" , Value

// The next command of WRITEFILE would be writen on next line of the file

CLOSEFILE "filename.txt"

Reading a file:

OPENFILE "filename.txt" FOR READ


READFILE "filename.txt" , Variable

// The value in the line (which is identified by the number of times this is being run) is stored in the
variable

CLOSEFILE "filename.txt"

Reading a file till EOF:

OPENFILE "filename.txt" FOR READ

DECLARE DataVariable : STRING

WHILE NOT EOF("filename.txt) DO

READFILE "filename.txt", DataVariable

// here the line can be outputted or stored in an array. This process will repeat until every line

//before the file ends has been read

ENDWHILE

You might also like