0% found this document useful (0 votes)
9 views31 pages

GENFC401 Syllabus

The document outlines the competencies and performance criteria for applying basic C++ concepts, object-oriented programming (OOP) concepts, and CPU optimization techniques. It provides an introduction to C++, its features, applications, and tools, as well as detailed explanations of variables, data types, operators, and the development environment setup. Additionally, it emphasizes the importance of proper coding practices and conventions in C++ programming.

Uploaded by

benitnishimwe1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views31 pages

GENFC401 Syllabus

The document outlines the competencies and performance criteria for applying basic C++ concepts, object-oriented programming (OOP) concepts, and CPU optimization techniques. It provides an introduction to C++, its features, applications, and tools, as well as detailed explanations of variables, data types, operators, and the development environment setup. Additionally, it emphasizes the importance of proper coding practices and conventions in C++ programming.

Uploaded by

benitnishimwe1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Element of competence Performance criteria

1. Apply basics C++ concepts 1.1 Development Environment is prepared properly


based on C++ programming standards

1.2 Variables are properly applied based on


programming rules and guidelines

1.3 Operators are properly applied based on


programming rules and semantics defined

1.4 Control structure are properly applied based on the


specific rules and syntax defined

1.5 Functions are correctly applied based on task to be


done

1.6 Arrays are properly applied based on

KAVUMU TSS L4 CSA 2024-2025


programming rules and conventions

2. Apply OOP concepts 2.1 Class and object are properly applied based on the
principles of object-oriented programming

2.2 Inheritance and polymorphism are properly


applied based on the rules and principles of object-
oriented programming

2.3 Namespaces are properly applied based on the


rules and conventions

2.4 Errors and Exceptions are effectively handled


according to the rules and practices of error handling

3. Perform CPU optimization 3.1. Pointers are properly applied based on


programming rules and specific concepts of pointer
manipulations

3.2. File handling are properly applied based on the


rules and concepts of file Input/ Output

3.3. Multithreading and concurrency are effectively


applied based on the computer performance needed

3.4 Inline-assembly is appropriately applied based on


processor control of hardware architecture

Learning outcome 1- Apply basic C++ concepts


1.1 Preparation of development environment

✔ Introduction to C++ programming


Definition
C++ is an object-oriented programming (OOP) language that is viewed by many as the best
language for creating large-scale applications. C++ is a superset of the C language. C++ was

KAVUMU TSS L4 CSA 2024-2025


developed by Bjarne Stroustrup in 1979, as an extension to the C language. C++ gives
programmers a high level of control over system resources and memory.
Features of C++
C++ is a general-purpose programming language that was developed as an enhancement of
the C language to include an object-oriented paradigm. It is an imperative and compiled
language. C++ has a number of features, including:
● Object-Oriented Programming
● Machine Independent
● Simple
● High-Level Language
● Popular
● Case-sensitive
● Compiler Based
● Dynamic Memory Allocation
● Memory Management
● Multi-threading
Applications of C++
⮚ Operating Systems
C++ is a fast and strongly-typed programming language which makes it an ideal choice for
developing operating systems. Mac OS X has large amounts written in C++. Most of the
software from Microsoft like Windows, Microsoft Office, IDE Visual Studio, and Internet
Explorer are also written in C++.
⮚ Games
Since C++ is closer to hardware, game development companies use it as their primary choice
to develop gaming systems. It can easily manipulate resources and can override the
complexities of 3D games and multiplayer networking.
⮚ GUI Based Applications
C++ is also used to develop GUI-based and desktop applications. Most of the applications
from Adobe such as Photoshop, Illustrator, etc. are developed using C++.
⮚ Web Browsers
Web browsers need to be fast in execution as people do not like to wait for their web pages to
be loaded. This is why most browsers are developed in C++ for rendering purposes. Mozilla
Firefox is completely developed from C++. Google applications like Chrome and Google File
System are partly written in C++.
⮚ Embedded Systems

KAVUMU TSS L4 CSA 2024-2025


Various embedded systems that require the program to be closer to hardware such as
smartwatches, medical equipment systems are developed in C++. It can provide a lot of low-
level function calls, unlike other high-level programming languages.
⮚ Banking Applications
Since banking applications require concurrency, multi-threading, concurrency, and high
performance, C++ is the default choice of programming language. Infosys Finacle is a popular
banking application developed using C++.
⮚ Compilers
The compilers of many programming languages are developed in C and C++. This is because
they are relatively lower-level when compared to other higher-level languages and are closer
to the hardware.
⮚ Database Management Software
C++ is also used to write database management software. The world’s most popular open-
source database, MySQL, is written in C++.
⮚ Cloud/Distributed Systems
Cloud storage systems that are used extensively need to work closer to the hardware. This
makes C++ the default choice for implementing cloud storage systems. These systems also
require multithreading support to build concurrent applications that support load tolerance,
which C++ provides. Bloomberg is a distributed RDBMS application that is primarily written
in C, but its development environment and set of libraries are all written with C++.
⮚ Libraries
Libraries require very high-level mathematical computations, performance, and speed. Hence
C++ is the core programming language used by most libraries. Tensorflow, one of the most
popularly used Machine Learning libraries uses C++ as its backend programming language.
⮚ Switches
Because C++ is one of the fastest programming languages, it is widely used for programming
routers, telephone switches, and space probes.
⮚ Advanced Computation and Graphics
Just like web browsers, all graphics applications require fast rendering. High-end graphical
processing, computer vision, or digital image processing software all use C++ as the backend
programming language.
✔ Description of C++ tools

⮚ IDE

KAVUMU TSS L4 CSA 2024-2025


An integrated development environment (IDE) is a software application that helps
programmers develop software code efficiently. An IDE, or Integrated Development
Environment, enables programmers to consolidate the different aspects of writing a computer
program.
IDEs increase programmer productivity by combining common activities of writing software
into a single application: editing source code, building executables, and debugging.
Examples of C++ IDE: Visual Studio, Eclipse, NetBeans, Code::Blocks, CLion, Xcode,
CodeLite, etc.
⮚ Text editor
Editors are basically computer programs that are utilized to edit files on a computer. The
provide environment to a programmer to create, edit, update, format a document in any order
he/she wants to.
In system programming or programming, editors are software or tools that are used to edit the
program.
Examples: VS Code, Sublime Text, Atom, Brackets, Geany, Notepad++, etc.
⮚ Compiler and interpreter
Compiler: A compiler is a computer program that transforms code written in a high-level
programming language into the machine code. It is a program which translates the human-
readable code to a language a computer processor understands (binary 1 and 0 bits).

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.

KAVUMU TSS L4 CSA 2024-2025


Interpreter Compiler
Translates program one statement at a Scans the entire program and translates it as a
time. whole into machine code.
It takes less amount of time to analyze It takes large amount of time to analyze the
the source code but the overall execution source code but the overall execution time is
time is slower. comparatively faster.
Generates intermediate object code which
No intermediate object code is generated,
further requires linking, hence requires more
hence are memory efficient.
memory.
Continues translating the program until It generates the error message only after
the first error is met, in which case it scanning the whole program. Hence debugging
stops. Hence debugging is easy. is comparatively hard.
Programming language like Python, Programming language like C, C++ use
Ruby use interpreters. compilers.

⮚ 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

⮚ Test development environment


First C++ program
#include <iostream>

KAVUMU TSS L4 CSA 2024-2025


using namespace std;

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.

1.2 Apply variables

⮚ 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:

KAVUMU TSS L4 CSA 2024-2025


auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for,
goto, if, int, long, register, return, short, signed, unsigned, sizeof, static, struct, switch,
typedef, union, unsigned, void, volatile, while, asm, bool, catch, class, const_cast, delete,
dynamic_cast, explicit, false, friend, inline, mutable, namespace, new, operator, private,
protected, public, reinterpret, cast, static_cast, template, this, throw, true, try, typeid,
typename, using, virtual, wchar_t.
⮚ Description of data types
C++ supports the following data types:
€ Primary or Built-in or Fundamental data type
€ Derived data types
€ User-defined data types

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.

KAVUMU TSS L4 CSA 2024-2025


The below table summarizes the modified size and range of built-in datatypes when combined
with the type modifiers:

Data Type Size (in bytes) Range

short int 2 -32,768 to 32,767

unsigned short int 2 0 to 65,535

unsigned int 4 0 to 4,294,967,295

int 4 -2,147,483,648 to 2,147,483,647

long int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295

long long int 8 -(2^63) to (2^63)-1

unsigned long long int 8 0 to 18,446,744,073,709,551,615

signed char 1 -128 to 127

unsigned char 1 0 to 255

KAVUMU TSS L4 CSA 2024-2025


Data Type Size (in bytes) Range

Float 4 -3.4×10^38 to 3.4×10^38

double 8 -1.7×10^308 to1.7×10^308

long double 12 -1.1×10^4932 to1.1×10^4932

wchar_t 2 or 4 1 wide character

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(_).

KAVUMU TSS L4 CSA 2024-2025


€ We cannot used C++ keyword (e.g float, double, class) as a variable name.
⮚ Identifier
Identifiers are the unique names given to variables, classes, functions, or other entities by the
programmer.
There are two types of Identifiers in C++. These are:
 Standard Identifier
 User defined Identifier
1. Standard Identifier
Like keywords, there are also predefined identifiers in C++. The predefined identifiers of the
C++ language that are used for special purposes in the source program are called the standard
Identifier. Each identifier has special meanings in C++. The names used for standard library
functions are standard identifiers.
For Example:
In C++ program, the cin and cout functions are mostly used for input or output operations.
Therefore, the functions names ''cin'' or "cout" are examples of standard identifiers.
2. User Defined Identifiers
The user (or programmer) can define its own identifiers in the C++ program such as variables,
arrays, functions, structures, unions and labels. The Identifiers defined by the user in the
program are called user defined Identifiers
⮚ Scope of variable
A scope is a region of the program and broadly speaking there are three places, where
variables can be declared
● Inside a function or a block which is called local variables,
● In the definition of function parameters which is called formal parameters.
● Outside of all functions which is called global variables.
Local variable
Variables that are declared inside a function or block are local variables. They can be used
only by statements that are inside that function or block of code. Local variables are not
known to functions outside their own.
Global variable
Global variables are defined outside of all the functions, usually on top of the program. The
global variables will hold their value throughout the life-time of your program. A global
variable can be accessed by any function.

KAVUMU TSS L4 CSA 2024-2025


1.3 Apply operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
1. C++ Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,

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

% Modulo Operation (Remainder after division)


Example 1: Arithmetic Operators
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;

KAVUMU TSS L4 CSA 2024-2025


// printing the product of a and b
cout << "a * b = " << (a * b) << endl;
// printing the division of a by b
cout << "a / b = " << (a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;
return 0;
}
2. C++ Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and -- respectively.

++ increases the value of the operand by 1


-- decreases it by 1
For example,
int num = 5;
// increment operator
++num; // 6
Example 2: Increment and Decrement Operators
// Working of increment and decrement operators
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result_a, result_b;
// incrementing a by 1 and storing the result in result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;
// decrementing b by 1 and storing the result in result_b
result_b = --b;

KAVUMU TSS L4 CSA 2024-2025


cout << "result_b = " << result_b << endl;
return 0;
}
3. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. For example,
// assign 5 to a
a = 5;
Here, we have assigned a value of 5 to the variable a.

Operator Example Equivalent to

= 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;

Example 3: Assignment Operators


#include <iostream>
using namespace std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

KAVUMU TSS L4 CSA 2024-2025


cout << "\nAfter a += b;" << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}
4. C++ Relational Operators
A relational operator is used to check the relationship between two operands. For example,
// checks if a is greater than b
a > b;
Here, > is a relational operator. It checks if a is greater than b or not.
If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

Operator Meaning Example

== Is Equal To 3 == 5 gives us false

!= Not Equal To 3 != 5 gives us true

> Greater Than 3 > 5 gives us false

< Less Than 3 < 5 gives us true

>= Greater Than or Equal To 3 >= 5 give us false

<= Less Than or Equal To 3 <= 5 gives us true

Example 4: Relational Operators


#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;

KAVUMU TSS L4 CSA 2024-2025


b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is " << result << endl;
result = (a != b); // true
cout << "3 != 5 is " << result << endl;
result = a > b; // false
cout << "3 > 5 is " << result << endl;
result = a < b; // true
cout << "3 < 5 is " << result << endl;
result = a >= b; // false
cout << "3 >= 5 is " << result << endl;
result = a <= b; // true
cout << "3 <= 5 is " << result << endl;
return 0;
}
5. C++ Logical Operators
Logical operators are used to check whether an expression is true or false. If the expression is
true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning

expression1 && Logical AND.


&&
expression2 True only if all the operands are true.

Logical OR.
expression1 ||
|| True if at least one of the operands
expression2
is true.

Logical NOT.
! !expression
True only if the operand is false.

Example 5: Logical Operators


KAVUMU TSS L4 CSA 2024-2025
#include <iostream>
using namespace std;
int main() {
bool result;
result = (3 != 5) && (3 < 5); // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 < 5); // false
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;
result = (3 != 5) || (3 < 5); // true
cout << "(3 != 5) || (3 < 5) is " << result << endl;
result = (3 != 5) || (3 > 5); // true
cout << "(3 != 5) || (3 > 5) is " << result << endl;
result = (3 == 5) || (3 > 5); // false
cout << "(3 == 5) || (3 > 5) is " << result << endl;
result = !(5 == 2); // true
cout << "!(5 == 2) is " << result << endl;
result = !(5 == 5); // false
cout << "!(5 == 5) is " << result << endl;
return 0;
}
6. C++ Bitwise Operators
In C++, bitwise operators are used to perform operations on individual bits. They can only be
used alongside char and int data types.

Operator Description

& Binary AND

KAVUMU TSS L4 CSA 2024-2025


| Binary OR

^ Binary XOR

~ Binary One's Complement

<< Binary Shift Left

>> Binary Shift Righ


1.4 Apply control structure
✔ Selection
C++ provides three types of selection structures. The if selection structure either performs
(selects) an action if a condition (predicate) is true, or skips the action if the condition is false.
The if/else selection structure performs an action if a condition is true, or performs a different
action if the condition is false.

The if selection structure is a single-selection structure—it selects or ignores a single action.


The if/else selection structure is a double-selection structure—it selects between two different
actions. The switch selection structure is a multiple-selection structure—it selects the action to
perform from many different action states.

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

e.g.: int marks, failed;


if (marks>=50)
if (failed<=3)

KAVUMU TSS L4 CSA 2024-2025


cout<<”Promoted”;
else
cout<<”Second sitting”;
else
cout<<”Repeat”;

⮚ 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.

KAVUMU TSS L4 CSA 2024-2025


Syntax: switch (variable)
{
case value1:
Block 1;
break;
case value2:
Block 2;
break;
default:
Block;
break;
}

e.g.: int code=3;


switch (code)
{
case 1:
cout<<”You like RED”;
break;
case 2:
cout<<”You like BLUE”;
break;
case 3:
cout<<”You like GREEN”;
break;
default:
cout<<”You like BLACK”
break;
}

KAVUMU TSS L4 CSA 2024-2025


✔ Iteration
Suppose that you need to print a string (e.g., "Welcome to C++!") a hundred times. It would
be tedious (or boring) to have to write the following statement a hundred times:

cout<<”Welcome to C++\n”;

cout<<”Welcome to C++\n”; 100 times

…..

cout<<”Welcome to C++\n”;

So, how do you solve this problem?

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;
}

KAVUMU TSS L4 CSA 2024-2025


While loop flowchart

e.g.: int I=1;


while(I<=20)
{
Cout<<I;
I++;
}
⮚ Do/while loop

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.

The do/while loop has the following general format:


do
{
statement
} while ( condition ) ;
Do While loop flowchart

KAVUMU TSS L4 CSA 2024-2025


e.g.: int counter = 1; // initialize counter

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

KAVUMU TSS L4 CSA 2024-2025


A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner
or outer loop can be any type: while, do while, or for. For example, the inner loop can be a
while loop while an outer loop can be a for loop.
a. Syntaxes
⮚ Syntax for nested for loop
for ( initialization; condition; increment ) {
for ( initialization; condition; increment ) {
// statement of inside loop
}
// statement of outer loop
}
⮚ Syntax for nested while loop
while(condition) {
while(condition) {
// statement of inside loop
}
// statement of outer loop
}
⮚ Syntax for nested do-while loop
do{
do{
// statement of inside loop
}while(condition);
// statement of outer loop
}while(condition);
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any
type of loop nested inside any type and to any level.
Example 1
// C++ program to display 7 days of 3 weeks
#include <iostream>
KAVUMU TSS L4 CSA 2024-2025
using namespace std;
int main() {
int weeks = 3, days_in_week = 7;
for (int i = 1; i <= weeks; ++i) {
cout << "Week: " << i << endl;
for (int j = 1; j <= days_in_week; ++j) {
cout << " Day:" << j << endl;
}
}
return 0;
}
Example 2
// C++ program to display a triangular pattern
// Number is entered by the user
#include <iostream>
using namespace std;
int main() {
int i, j, n;
cout << "Enter Number : ";
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}

KAVUMU TSS L4 CSA 2024-2025


⮚ Jump statements
Definition
Jump statements in C++ are a type of Control Statements in C++ used to interrupt the normal
flow of the program. It makes the program jump to another section of the program
unconditionally when encountered. It can also be used to terminate any loop.
Jump statements are implemented to change the flow of the program when particular
conditions are satisfied. It is used within a program to end or continue a loop or to pause the
execution of a function. C++ has four jump statements: continue, break, return, and goto.
1. break statement
The break statement is used to end the loop immediately after the encounter. It is used in
C/C++ in the following scenario:
✔ It is used to terminate the loop, and program control resumes at the next statement
following the loop.
✔ It is used to terminate a case in the switch statement.
Flowchart,

Syntax

KAVUMU TSS L4 CSA 2024-2025


Example
// program to print the value of i
#include <iostream>

using namespace std;


int main() {
for (int i = 1; i <= 5; i++) {
// break condition
if (i == 3) {
break;
}
cout << i << endl;
}
return 0;
}
2. continue statement

KAVUMU TSS L4 CSA 2024-2025


Instead of terminating the loop, it performs the next iteration of the same loop but ignoring the
parts specified by the condition. Within the loop, it must be used in conjunction with a
decision-making statement. This statement can be used within a for, while, or do-while loop.
Example
Consider a situation in which all of the numbers between 1 and 15 except 7 are present. So,
after the value of j is 7, the goal is to use the continue statement. The program for the same is
as follows:
// C++ code that shows how to use the continue statement
#include <iostream>
using namespace std;
// main function
int main ()
{
for (int j = 1; j < 15; j++) {
if (j == 7)
continue;
cout << j << " ";
}
return 0;
}
3. return statement
It removes control from the function. It is more powerful than a break. It is used to end the
entire function after the function has completed or after a condition has been met. Except for
the void() function, every function contains a return statement that returns some value.
Example
// C++ code that shows how to use the return statement
#include <iostream>
using namespace std;
// main function
int main()

KAVUMU TSS L4 CSA 2024-2025


{
cout << "Start with ";
for (int j = 0; j < 15; j++) {
if (j == 7)
return 0;
cout << j << " ";
}
cout << "end";
return 0;
}
4. goto statement
This statement enables us to jump directly to the section of the program that is being
referenced. Each goto statement is connected with a label that directs them to the section of
the programme for which they are named. Label statements can be written anywhere in the
program; no before or after goto statements are required.
// C++ code to show the goto statement
#include <iostream>
using namespace std;
// main function
int main ()
{
int p = 7;
if (p % 2 == 0)
goto label01;
else
goto label02;
label01:
cout << "Even" << endl;
return 0;

KAVUMU TSS L4 CSA 2024-2025


label02:
cout << "Odd" << endl;
}

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

KAVUMU TSS L4 CSA 2024-2025

You might also like