computer science
An Introduction to Programming -
COMPONENTS OF A PROGRAMMING LANGUAGE
Every programming language as we have discussed earlier has its syntax and semantics. this simply means that, the language also known as an interpreter or compiler, decides how the programmer designs his program (code). Notwithstanding, there are certain basic components which usage has to be defined by the programming language including:
Keywords
These are peculiar to the individual language you may wish to learn and they are special. These words/elements are predefined and so their definition can not be changed by the user (programmer). In addition, keywords also cannot be used as names for variables, methods, classes, or as any other identifier.
Example
Some keywords in python
and, del, from, not , while, as, elif, global, or, with, def
Some keywords in java
abstract, assert, boolean, break, byte, case,catch, char, class, const, continue, default
Some keywords in JavaScript
break, case, catch, continue, debugger, default,var, do, else, finally, for, function, if, in
Identifiers
Identifiers are user-defined word usually in form of a string of alphanumeric characters that begins with an alphabetic character or an underscore character that are used to represent various programming elements such as variables, functions, arrays, structures, unions and so on. In other words, you can say Identifiers are the names of variables, functions, structures etc in program, used for identification purpose. The words you choose should be meaningful to whoever sees them at first glance. A common use of identifiers is in naming variables, by beginning with letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9)
Example of valid identifiers
-
luvcoding222
-
MyNumber
-
MYNUMBER
-
myvariable
-
y
-
j
-
j1
-
name1
-
_myvariable
-
list_of_array
Example of invalid identifiers
-
222luvoding
-
my Number
-
1name
-
list-of-array
Data Types
A data type in programming, is a classification of the type of data that an identifier (variable or object) can hold during programming execution. Data types are an important factor in virtually all computer programming languages, including python, C++, JavaScript, and java.
Example:
-
Boolean (e.g., True or False)
-
Character (e.g., a)
-
Date (e.g., 03/01/2016)
-
Double (e.g., 1.79769313486232E308)
-
Floating-point number (e.g., 1.234)
-
Integer (e.g., 1234)
-
Long (e.g., 123456789)
-
Short (e.g., 0)
-
String (e.g., 'abcd')
-
Void (e.g., no data)
_____Variables
What ever language you choose, the syntax of all programming languages are very similar and follow generally same rules in different forms (syntax) and meanings (semantics). Variables are simply named memory locations. Variables are used to store information that can be referenced and manipulated when writing a computer code or program. We normally use variables for labeling data with a descriptive name, so that we can easily refer to them and use them for further computation. We sometimes want to keep data in our source code in a place where we can recall the data, to use again. variables are usually a memory location which our compiler/interpreter reserves for us. We need to name these memory locations (with identifiers) to recall them later.
Declaring variables in Python
name = 'Johnson'
name is an example of a variable. Because the type of data stored in name is a string. It is known as a string variable. There are numeric variables as well. Variables are categorized by their data types. In general, variables are the containers that hold data in the computer's memory for easy access and reference.
More python variable examples.
myfloat = 7.0
mystring = 'hello'
pi = 3.14159;
Declaring variables in Java.
int myNumber = 5;
double d = 4.5;
char c = 'g';
double pi = 3.14159;
Declaring variables in JavaScript.
let r = 1
var p =new Point(pos.x, pos.y)
let points = []; ---this creates an empty list called points
_____Constants
A Constant is a value stored just like a variable, but which the program cannot easily change. Constants do not change throughout the lifetime of the program. In programming, a constant is a symbols that can represent same values throughout the course of a program.A constant can be a number like 25 or a term like "const maxPlayerCount = 8" . Some languages provide a way to create constant values, while others do not.We use capital letters to enforce that some values are to be constant values.
Example:
Declaring constants values in JavaScript.
const PI = 3.141592653589793;
const options = {
zoom: 2.8,
lat: 10
};
const numbers = [1, 2, 3];
Declaring constants values in Python.
PI= 3.14
GRAVITY = 9.8
TEMP = 22
Declaring constants values in C.
const double PI = 3.14
const float XYZ = someVariable
const int LENGTH = 10
Declaring constants values in Java.
public static final int MAX_SECONDS = 25;
public static final Point ORIGIN = new Point(0,0);
public static final double PI = 3.14159265358979323846;
_____Literals
Literal is a raw data given in a variable or constant. There are various types of literals,
In python for instance, some examples are as follows ;
numeral literals,
-
b = 100
-
float_1 = 10.5
-
float_2 = 1.5e2
-
x = 3.14j
string literals,
-
strings = "This is Python"
-
char = "C"
-
multiline_str = """This is a multiline string with more than one line code."""
-
raw_str = r"raw \n string"
Boolean literals etc.
-
a = True
-
b = False
The names on the left hand side of all the examples are Identifiers while the ones on the right hand side after the equal sign are literals. In every source code (computer program), literals should not to be confused with variables or constants. they can be seen once you glance through the source code.
Punctuations/Symbols
In most programs, you will generally find different punctuation marks depending on the programming language. Common punctuation marks include
-
the comma (,),
-
semi-colon(;),
-
colon(:),
-
braces ({ }),
-
Brackets (()),
-
square braces ([ ]),
-
quotation marks (“ ”),
-
pipe (|),
-
slash (\),
-
period (.),
-
question mark (?),
-
caret(^) and percentage (%).
You will be using a lot of them when coding.
Operators
In computer programming, An operator is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result. We are presented with a large number of operators by the programming languages we use. Examples include
-
addition(+),
-
division(/)
-
multiplication(*),
-
subtraction(-) and greater than(>).
Operators can generally be classified as follows:
-
Assignment Operators This is sometimes misconstrued as equal-to. Equal to is used to compare two values. Assignment Operator puts a value in a variable, such as PI = 3.14
-
Arithmetic Operators for carrying out arithmetic tasks such as addition, and subtraction. Some languages provide some arithmetic operators which others may not have. Computers use different symbols in programming to represent some of these: Addition (+), subtraction (-), multiplication (*) and division (/). More complex operators such as square root (Öx). other operators like the modulus operator (%), returns the remainder value in division operations.
-
Relational Operators are used for comparing values. They include greater than (>), less than (<), equal-to (=), not-equal-to (!=). In computer science, a relational operator is a construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3). Their representation varies as well depending on what programming language you are learning. <> is not equal-to in some languages, while in others, it’s != or !==.
-
Logical Operators are used to compute logical operations. Logical operators allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. The && operator is used to determine whether both operands or conditions are true. The commonly used logical operators are and , or , not. Some languages represent these operators with symbols such as && for and , || for or , and ! for not.
Comments
Comments are used by a programmer to explain their code to other programmers. A comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by translators. Comments vary across languages.
The # symbol is used to introduce comments in Python.
Example.
# this program snippet computes the average of N numbers
In Java, C and C++ ,
The // symbol is used instead for a single line just like the # in Python.
But, for multi-line comments /* … */ is used.
Example
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/