General Programming
Basics of programming
Last updated
Basics of programming
Last updated
This section will discuss about the general things that applies to all programming languages. This includes
We will discuss each and how it applies to programming.
Program - Process of writing computer programs. Programs - written step by step instructions that guide users on how to do a specific task. You will be instructing the computer by giving specific instructions.
Syntax - Rules of a programming language. Every language has there own rules just like our natural languages. e.g. terminate every line with a semicolon at the end.
Algorithms - Set of rules/instructions in problem solving. Step by step instructions for solving a problem.
Flowchart - Diagramatic representation of an algorithm
This is a computer storage location. Imagine asking a user for their names and store it in the computer's memory and you can call the memory location anywhere in the program to use it.
Variable Declaration is just giving name to a variable while Variable initialization is giving value to a variable
Rules of writing variables
Specific languages follow specific rules. This includes:
A variable cannot start with a number
A variable can start with a dollar sign ($
), underscore(_
) , capital or small letter.
variables are case-sensitive
Variables cannot have keywords
These are same with variables only that they don't change once initialised. Variables can be re-initialised but constants can't change.
These are the reserved words used by a specific language to do something. Like above const
is a reserved keyword for constants.
Note each language has specific keywords.
There are a lot of data that we use to come up with programs. The most common include:
Integers - Include numbers e.g. int num1 = 100
Characters - ( char
) single lettered char 1st = 'r'
Strings - (string
) Combination of characters data types string MyName = "Nicanor"
Boolean - bool
values required for true or false occasions.
Float - used for numbers but with decimal values float num2 = 5.434
Double - used for numbers with large decimal values double num = 5.4324343434343
Array - Collection of the same data types int numValues = [3, 2, 4, 5, 2, 43, 23]
Object - Data type which stores data and information on how to process that data e.g. const Names = { Name : "Nicanor" }
Returns the type of argument. If you want to check the type of data type you using
This includes combination of more than one data type, preferably integers, float and double. These include
Arithmetic Operators - +, -, *, modulas(%), /
.
Assignment Operator - =
e.g. int x = 100;
Logical Operators - &&(AND) , OR(||), not(!)
Relational Operators- Used when making comparisons >, <, >=, <=, equal to(==), not equal to(!=)
e.g. (10 >= 20) && ('c' == 'd')
More advanced operators include conditional Operator that uses ternary operator (?) e.g.
Used by programmers to explain their code. Cannot be read by the program during execution. Every language has its own syntax of writing comments e.g.
Used to perform different actions based on different conditions. Based on making decisions. The diagram below shows if....else statement
The following are conditions we going to discuss about
If statement
If ... else statement
if ... else if ... statement
If Statement
Control statements that allows to make decisions and execute statements conditionally. Syntax is as follows:
If ... else Statements
Allows execution of statements in a more controlled way. Syntax and an example
If ... else if ... Statements
Advanced form of If ... else statements that allows making correct decisions based on several conditions. Syntax is as below
This is an alternative of If ... else statements by minimising the repeated statements. Flowchart is as shown below
Syntax
An example of Switch statements
This is a group of code that does a specific thing. Also a group of re-usable code. Syntax
Function Declaration is just like the above function syntax. Just writing group of codes to do specific task
Function Calling is using the function in other places. You just call the function name in several places that needs application. functionname(arguments)
Parameters are like variables that are passed into the function
Arguments are real values that will replaces the parameters and be used in the function.