AC1 Computer Programming - Reviewer
AC1 Computer Programming - Reviewer
PROGRAMMING
Keywords
BASIC OF PROGRAMMING
COMPUTER PROGRAM
are:
Programming Environment
In programming, programmer writes a series of
has to perform.
ASSEMBLY LANGUAGE
Most modern programming languages are designed to a high-level language into a low-level language (such as
be relatively easy. These are known as high-level machine language) before executing the statements.
languages.
INTERPRETER – Act as a compiler, but it translates one (1)
Each high-level language has its syntax, or rules of the
program statement at a time, this executes the statement as
languages (For example, the verb print or write is used
soon as it is translated.
to produce an output)
SYNTAX ERROR – In the process of translation, an invalid
ALGORITHM DESIGN
error might be encountered as well. Logical Error (Semantic
Error) occurs when the syntax of the program is correct but the
After analyzing the problem, the next step is to design an
ALGORITHM
and integrate the solution to sub-problems.
problem
free from any syntax error, the compiler generates the machine
code.
3. Coding: implement the algorithm in a programming
language
Example of Algorithm
Here are some rules that are frequently followed when writing lastname
pseudocode
Print fullname
Assignment (=)
It contains symbols/shapes describing how an
object-oriented paradigm.
C++ Environment
as 123 or -123
19.99 or -19.99
definition is a part where the variable is assigned a memory These variables are created when entered into the block
location and a value. Most of the time, variable declaration and or the function is called and destroyed after exiting from
definition are done together. the block or when the call returns from the function.
TYPES OF VARIABLES The scope of these variables exists only within the block
INSTANCE VARIABLE
Instance Variables
variables are created when an object of the class is will replace the object name with the class name
Unlike local variables, we may use access specifiers for If we access the static variable without the class name,
instance variables. If we do not specify any access the Compiler will automatically append the class name.
Initialization of Instance Variable is not Mandatory. information the variable will store
STATIC VARIABLE
NUMBER TYPES
any method constructor or block.
ends.
only take the values true or false.
default value is 0
certain characters.
STRING TYPES
The string type is used to store a sequence of characters BINARY OPERATORS – These operators operate or work
(text). This is not a built-in type, but it behaves like one with two operands.
functionality.
ARITHMETIC OPERATORS
RELATIONAL OPERATORS – These operators are used for
mathematical operations.
on the operands.
All the statements starting with the # (hash) symbol are known
perform certain tasks to improve the program's helps to compile a specific portion of the program or to
with the # (hash) symbol). Other directives – Apart from the above directives,
IN C++ PROGRAMMING
STREAMS IN C++
Macros – In C/C++, Macros are pieces of code in a Input Stream (istream): This stream is used for reading
program that is given some name. data from a source, such as the keyboard or a file. The
So far, we have been using the iostream standard library, which and writing.
1. fstream Class:
input operations.
Input functions in C++ are used to receive data from the user A basic C++ program follows a structure that typically
stream. It is used to print data to the console or another output Comments: These are used to add explanatory notes to
STATEMENT IN C++
#include <cstdio>
doubleValue,
charValue);
In C++, a format string is a string that contains placeholders Character: %c\n". Here:
Structure.
// Using escape sequences in a string
There are three types of Control Structures in C++, And all the
std::cout << "This string contains a tab character\tand
program processes we write can only be implemented with
continues after the tab.\n";
these three control structures
return 0;
SEQUENCE STRUCTURE
}
This is the most simple and basic form of a control structure. It
In this example: is simply the plain logic we write; it only has simple linear
\t is an escape sequence representing a tab character. control structure only. It executes linearly line by line in a
manipulation and formatting in C++ applications. Format Let's understand the program flow and how it happened.
characters in your strings, such as newline or tab characters. Then, the sum variable was declared and was assigned
to a program. There are three types of basic control We can see that our program came one way, in a straight-line
SELECTION STRUCTURE We are first asking the user to enter their age and then
condition checks that this part of code should only execute if a Then we are checking the condition using an if-else
particular condition meets or another part of code should run. control statement, and if it satisfies, then executing the
or not based on two conditions: A block means a group of statements enclosed in curly
coming section.
LOOP STRUCTURE
While Loop Use the else statement to specify a block of code to be executed
For Loop
CONDITIONAL STRUCTURES
The If Structure
condition is false.
Do While Loop
The do/while loop is a variant of the while loop. This loop will
true, then it will repeat the loop as long as the condition is true.
do, for, and range-based for. Each of these iterates until its
The While Loop When you know exactly how many times you want to loop
through a block of code, use the for loop instead of a while loop:
The while loop loops through a block of code as long as a
Statement 1 is executed (one time) before the execution of the Continue Statement
code block.
The continue statement breaks one iteration (in the loop), if a
Statement 2 defines the condition for executing the code block. specified condition occurs, and continues with the next
been executed.
Go to Statement
JUMP STATEMENTS
Use of goto statement is highly discouraged because it makes
Break Statement difficult to trace the control flow of a program, making the
be executed.
The value of the expression is compared with the values A function is a block of code which only runs when it is called.
of each case
Functions are used to perform certain actions, and they are
If there is a match, the associated block of code is important for reusing code: Define the code once, and use it
The break and default keywords are optional, and will C++ provides some pre-defined functions, such as main(),
be described later in this chapter which is used to execute code. But you can also create your own
indicate that the function does not return any value. When a
Overloaded functions in C++ are multiple functions with the You can initialize an array at the time of declaration or later
same name but different parameter lists. The compiler during the program. Here are examples:
the same thing but with different types or numbers of inputs. MULTIDIMENSIONAL ARRAYS:
// 2D array
Recursive functions can be a powerful and elegant way to solve
int matrix[3][3] = {
certain problems, especially those that exhibit a recursive
{1, 2, 3},
structure.
{4, 5, 6},
DECLARING FUNCTIONS:
{7, 8, 9}
Function declarations in C++ provide information to the
};
compiler about the function's name, return type, and
define a function later in the code or if you want to use a Arrays can be passed to functions as parameters. When an
function before its actual definition. array is passed to a function, the function receives a pointer to