GENFC401 Syllabus
GENFC401 Syllabus
2. Apply OOP concepts 2.1 Class and object are properly applied based on the
principles of object-oriented programming
⮚ IDE
Interpreter: In computing, interpreter is a program that can analyze and execute a program
line by line. In computer science, an interpreter is a computer program that directly executes,
i.e. performs instructions written in a programming or scripting language, without requiring
them previously to have been compiled into a machine language program.
⮚ Debugging
Debugging is a methodical process of finding and reducing the number of bugs (or defects) in
a computer program, thus making it behave as originally expected.
⮚ Version control
Version control, also known as revision control or source control, is the management of
changes to documents, computer programs, large websites, and other collections of
information. Each revision is associated with a timestamp and the person making the change.
⮚ Tools installation
⮚ IDE installation
⮚ Compiler installation
⮚ Setup environment variable path
int main() {
cout << "Hello World!";
return 0;
}
Explanation
Line 1: #include <iostream> is a header file library that lets us work with input and output
objects, such as cout (used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a
function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<)
to output/print text. In our example it will output "Hello World!".
Note: Every C++ statement ends with a semicolon ;.
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.
Line 6: return 0 ends the main function.
Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
⮚ Definition
We can define a variable as a portion of memory to store a determined value. Each variable
needs an identifier that distinguishes it from the others
⮚ Identification of C++ reserved keyword
Note that the following C++ keywords are not allowed to be identifiers:
Datatype Modifiers
As the name suggests, datatype modifiers are used with built-in data types to modify the
length of data that a particular data type can hold.
Note: Above values may vary from compiler to compiler. In the above table, the considered
compiler is GCC 32 bit.
⮚ Declaration of variable
A typical variable declaration is of the form:
// Declaring a single variable
type variable_name;
// Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;
Examples:
// Declaring float variable
float simpleInterest;
// Declaring integer variable
int time, speed;
// Declaring character variable
char var;
⮚ Naming rules
€ The name of the variable contains letters, digits, and underscores.
€ The name of the variable is case sensitive (ex Arr and arr both are different
variables).
€ The name of the variable does not contain any whitespace and special characters (ex
#,$,%,*, etc).
€ All the variable names must begin with a letter of the alphabet or an underscore(_).
a + b;
Here, the + operator is used to add two variables a and b. Similarly there are various other
arithmetic operators in C++.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
Logical OR.
expression1 ||
|| True if at least one of the operands
expression2
is true.
Logical NOT.
! !expression
True only if the operand is false.
Operator Description
^ Binary XOR
NOTE
There are also nested if (means if inside another if), and ladder if (if/else if…).
⮚ If
Syntax: if(condition)
{
Statements block
}
e.g. if(age>50)
cout<<”Old”;
cout<<”Bye”;
if the condition is true, the first line is executed
⮚ if/else
KAVUMU TSS L4 CSA 2024-2025
The if/else selection structure allows the programmer to specify an action to perform when the
condition is true and a different action to perform when the condition is false.
Syntax: if (condition)
{
True block
}
else
{
False block
}
e.g.: if (grade>=60)
cout<<”Passed”;
else
cout<<”Failed”;
Note
C++ provides the conditional operator (? :), which is closely related to the if/else structure.
The conditional operator is C++'s only ternary operator—it takes three operands. The
operands, together with the conditional operator, form a conditional expression.
The first operand is a condition, the second operand is the value for the entire conditional
expression if the condition is true and the third operand is the value for the entire conditional
expression if the condition is false. For example, the output statement
cout << ( grade >= 60 ? "Passed”: "Failed”); contains a conditional expression, grade >= 60?
"Passed”: "Failed", that evaluates to the string "Passed" if the condition grade >= 60 is true,
but evaluates to the string "Failed" if the condition is false.
⮚ Nested if
Syntax: if (condition1)
if (condition2)
if (condition3)
block 4
else
block 3
else
block 2
else
block 1
⮚ Ladder if
Nested if/else structures test for multiple cases by placing if/else selection structures inside
if/else selection structures.
Syntax: if (condition1)
{
Block 1
}
else if (condition2)
{
Block 2
}
else if (condition3)
{
Block 3
}
else
{
Block 4
}
e.g.: if (grade>=90)
cout<<”A”;
else if (grade>=80)
cout<<”B”;
else if (grade>=70)
cout<<”C”;
else if (grade>=60)
cout<<”D”;
else
cout<<”F”;
⮚ Switch
The switch selection structure performs one of many different actions, depending on the value
of an integer expression or any other type of variable. The switch structure consists of a series
of case labels and an optional default case.
cout<<”Welcome to C++\n”;
…..
cout<<”Welcome to C++\n”;
C++ provides a powerful construct called a loop that controls how many times an operation or
a sequence of operations is performed in succession. Using a loop statement, you simply tell
the computer to print a string a hundred times without having to code the print statement a
hundred times.
Loops are constructs that control repeated executions of a block of statements. The concept of
looping is fundamental to programming. C++ provides three types of loop statements: while
loops, do-while loops, and for loops.
While loop
Syntax:
While (condition)
{
Body of the loop;
Incrementation / decrementation;
}
The do/while repetition structure is similar to the while structure. In the while structure, the
loop-continuation condition test occurs at the beginning of the loop before the body of the
loop executes. The do/while structure tests the loop-continuation condition after the loop body
executes; therefore, the loop body executes at least once.
do {
cout << counter << " •" ; // display counter
} while ( ++counter <= 10 ) ;
⮚ For repetition
The for repetition structure handles all the details of counter-controlled repetition.
The general format of the for structure is
for ( initialization; loopContinuationCondition; increment/decrement )
statement
e.g.: int I;8888888888888y
for (I=0;I<=20;I++)
cout<<I;
⮚ Nested loops
Syntax
https://www.w3schools.com/cpp/cpp_intro.asp
https://www.techtarget.com/searchdatamanagement/definition/C
https://www.geeksforgeeks.org/features-of-cpp/
https://www.simplilearn.com/tutorials/cpp-tutorial/top-uses-of-c-plus-plus-programming
https://linuxhint.com/best_cpp_editors/
https://www.programiz.com/article/difference-compiler-interpreter