Professional Documents
Culture Documents
Prog Lectures Galing Kay Johann
Prog Lectures Galing Kay Johann
C++ Basics
Note:
A language is a set of valid sentences. What makes a sentence
valid? You can break validity down into two things: syntax and
semantics. The term syntax refers to grammatical structure
whereas the term semantics refers to the meaning of the
vocabulary symbols arranged with that structure.
• Symbols
+ - * / mathematical symbols
<= != == >= relational operators
• Symbols
Comment symbols
// single-line comment symbol
For example,
// This is my first program
// C++ Language
• Reserved Words
Reserved words are also called keywords that cannot be redefined
within any program; that is, they cannot be used for anything
other than their intended use.
Some of the important reserved words are :
char float int void
break if else switch
case return for do
while main
• Identifier
Identifier refers to the names of variables, constants, functions
and other objects defined in a C++ program. It allows us to name
data and other objects in the program.
• Identifier
Rules for identifiers are:
•An identifier cannot contain punctuation marks, math
symbols or other special symbols.
•ANSI guarantees only the first 32 characters to be
significant.
•C++ identifier is case-sensitive
•The last rule is that the name we create cannot be
keywords.
• Identifier
Examples of Valid and Invalid identifiers
Valid Identifiers Invalid Identifiers
grade grade+3
area1 1area
area_of_triangle area of triangle
• Data Type
A data type defines a set of values that a variable can hold.
Based on the data type of a variable; the operating system
allocates memory and decides what can be stored in the
reserved memory.
Type Keyword Data type Examples
Boolean boolean
Character char boolean true, false
Integer int
Floating point float char ‘A’ , ‘b’ ,’+’
Double floating double
point int 10,200,550
Valueless void
float /double 10.54, 2.56
• Variable
• Variable
Variable Declaration
datatype identifier;
For example:
int x;
• Operator
Types of operators:
•Assignment Operator
•Arithmetic Operators
•Relational Operators
•Logical Operators
• Operator
•Assignment Operator
variable = expression;
Example:
y = 5;
• Operator
•Arithmetic Operators
Operator Meaning Example
* Multiplication x*y
/ Division x/y
% Modulus x%y
+ Addition x+y
- Subtraction x–y
++ Increment x++
-- Decrement x--
+ (unary) Positive sign +x
- (unary) Negative sign -x
• Operator
Order of Precedence
However, when operators have the same level, the operations are performed
from left to right.
• Operator
•Relational Operators
• Expression
Arithmetic Expression
M W Start page
a i
n n
a d
g o
e w
m
e
n
t
Logs window
Creating a File
1. Select File>New>File
Creating a File
2. Select C/C++ source
3. Click Next.
4. Select C++
{ }
The braces and mark the beginning
int main() and end of the main body of the
{ program.
statement 1; Everything between these braces is the
statement 2; function's body that defines what
. happens when main is called. The
.
statements inside the braces are called
as executable statements. Statements
.
are executed in the same order that
statement n;
they appear within a function’s body.
return 0;
}
int main()
{
statement 1;
statement 2;
.
. The line return 0; terminates
. main()function and causes it to
statement n; return the value 0 to the calling
return 0; process.
}
age = 18;
Escape Sequence
The backslash (\) is known as the escape character in the ASCII character set. The
escape character is used in the C++ language to tell the computer that a special
character follows.
Escape Sequence
Example:
To insert a line break in the output, a newline character (\n) is inserted in the exact position,
the line should be broken. Output
Hello!
cout<< "Hello!\n"; Have a nice day!
cout<< "Have a nice day!";
cin>> variable;
cin>> variable;
Example:
int age;
cout<<"Please enter your age: ";
cin>>age;
cout<<"You are "<<age<<“ years old”;
if statement
if -else statement
if (conditional expression) if (number>0)
{ {
cout<<“Positive”;
statement;
}
} else {
else { cout <<“Not a positive number”;
statement;
}
}
if (conditional expression1)
{
statement;
}
else if (conditional expression2)
{
statement;
}
else {
statement;
if (number>0)
{
cout<<“Positive”;
}
else if (number<0)
{
cout<<“Negative”;
}
else {
cout<<“Zero ”;
}
}
else {
statement;
}
} IT 103 Computer Programming 1
Selection Structure : if /if-else Statement
Example: Test if the number is positive , negative or zero. In case the number
is positive, test if it is even or odd.
if (number>0)
{
cout<<“Positive”;
if (number%2==0)
{
cout<<“Even ”;
}
else {
cout<<“Odd ”;
}
}
else if (number<0)
{
cout<<“Negative”;
}
else {
cout<<“Zero ”;
}
IT 103 Computer Programming 1
UNIT
4 STRUCTURES
Example :
struct student structure name
{
char name[20];
field list
char course[5];
(structure members)
int age;
int grade;
};
//Structure Definition
struct employee
{
double hours;
double rate; // or can be written as double hours,rate, salary;
double salary;
};
//Structure Variable Declaration
True
Statement 1
if (conditional expression) False
statement;
Statement 2
x=5; Output
if(x>=5)
hello good day
cout<<“hello ”;
cout<<“good day”;
x=4; Output
if(x>=5)
good day
cout<<“hello”;
cout<<“good day”;
x=5;
Output
if (x>=5)
12
x*=2;
x+=2;
cout<< x;
x=4;
Output
if (x>=5)
x*=2; 6
x+=2;
cout<< x;
{
Statement 1
Statement1;
False
.
Statement n
.
.
Statement n;
Statement
}
IT 103 Computer Programming 1
Selection Structure : if Statement
x=5; Output
if(x>=5)
hello good day
{
cout<<“hello ”;
cout<<“good day”;
}
x=4; Output
if(x>=5)
God Bless
{
cout<<“hello ”;
cout<<“good day”;
}
cout<< “God Bless”;
IT 103 Computer Programming 1
Selection Structure : if Statement
x=5; Output
if (x>=5)
12
{
x*=2;
x+=2;
}
cout<< x ;
IT 103 Computer Programming 1
Selection Structure : if Statement
x=4; Output
if (x>=5)
4
{
x*=2;
x+=2;
}
cout<< x ;
IT 103 Computer Programming 1
Selection Structure : if-else Statement
Statement 1
if (conditional expression)
False
statement 1;
else
statement 2; Statement 2
if (conditional expression)
{
statement(s);
}
else {
statement(s);
}
x=5; Output
if(x>=5)
hello
cout<<“hello ”;
else
cout<<“good day”;
x=4; Output
if(x>=5)
good day
cout<<“hello ”;
else
cout<<“good day”;
x=4; Output
if (x>=5)
6
x*=2;
else
x+=2;
cout<< x ;
x=5; Output
if (x>=5)
10
x*=2;
else
x+=2;
cout<< x ;
x=6; Output
if (x<=5)
{
x+=2; 13
}
else
{
x*=2;
x++;
}
cout<< x ;
if (conditional expression_1)
statement_1;
else if (conditional expression _2)
statement_2;
else if (conditional expression _n)
statement_n;
:
:
else
statement_else;
The nested if/ if-else statement is known as nested when there is another if/if-else
statement inside it. This is considered a sub-condition of a parent condition.
if ( conditional expression_1)
{
if ( conditional expression_1.1)
{
statement_1.1;
}
else
{
statement_1.2;
}
}
else
{
statement_2;
}
The switch statement is a multi-way decision and used when there are
multiple conditions. It is an alternative statement for an if-else ladder
with a limitation that switch can only test for equality.
Example :
True
body of the loop.
Increment/
Decrement
5. Repeat Step 2 until the loop condition evaluates to
false.
1 2 4
for (initialization; condition; increment)
{
3
statement(s);
}
Example 1 :
Output
for (x=1;x<=5;x=x+1)
cout<<x<<“ ”; 1 2 3 4 5
Example 2 :
Output
for (x=1;x<=5;x=x+1)
cout<<x<<“ ”; 1 2 3 4 5 6
cout<<x<<“ ”;
Example 3 :
Output
1
for (x=1;x<=5;x=x+1) 1
2
{
2
cout<<x<<endl; 3
3
cout<<x<<endl; 4
} 4
5
5
for (x=1;x<=5;x=x+1)
for (x=5;x>=1;x=x-1)
statement(s);
}
}
Example 6 :
Output
Condition-controlled loop
while (condition) do
{ {
statement;
} statement;
}while (condition);
Example 1 :
ans = 'Y';
while (ans =='Y')
{
cout<<"Hello\n";
cout <<"Try again?(Y/N) ";
cin>>ans;
}
Example 2 :
ans = 'Y';
do
{
cout<<"Hello\n";
cout <<"Try again?(Y/N) ";
cin>>ans;
Example 3 :
x=1;
while (x<=5)
{
cout<<"Hello ";
x++;
}
IT 103
Computer Programming 1
Lesson 1
Introduction to Modular Programming
IT 103
Computer Programming 1
Modular Programming
Breaking a complex problem into smaller parts, each of these parts of a
program is referred to as a module.
IT 103
Computer Programming 1
Modular Programming
IT 103
Computer Programming 1
Functions in C++
IT 103
Computer Programming 1
Functions in C++
IT 103
Computer Programming 1
Functions in C++
IT 103
Computer Programming 1
Functions in C++
A function can also call other functions; when the function call statement
is encountered even not in the main() function, the control is transferred
to the called function, and all its statements are executed.
With the execution of the return statement, the control returns to the
statement immediately following the function call statement.
IT 103
Computer Programming 1
Functions in C++
IT 103
Computer Programming 1
UNIT 4
Modular Programming
IT 103
Computer Programming 1
Lesson 2
Components of a Function
IT 103
Computer Programming 1
Function Definition
The function definition contains the code for a function. It is made up of two
parts: the function header and the function body, which is a compound
statement.
Function Header
A function header is made up of three parts: the return type, the
function name, and the formal parameter list. A semicolon is not used
at the end of the function definition header.
IT 103
Computer Programming 1
Function Definition
The function definition contains the code for a function. It is made up of two
parts: the function header and the function body, which is a compound
statement.
Function Body
The function body contains the local {
declarations and the function statements. local declarations
The body starts with local definitions that
specify the variables needed by the function. statements;
After the local definitions, the function return;
statements, terminating with a return }
statement, are coded recommend that every
function, even void functions, have a return
statement.
IT 103
Computer Programming 1
Function Definition
The function definition contains the code for a function. It is made up of two
parts: the function header and the function body, which is a compound
statement.
return_type function_name (formal parameter list)
{
local declarations
function header
statements;
return; function body
}
Note:
Variables that are declared inside a function are called local
variables while global variables are created by declaring them
outside of any function.
IT 103
Computer Programming 1
Function Declaration
Any identifier or user-defined word can only be used once they have
been declared. The same applies to functions that functions need to be
declared before being called.
The function declaration consists of the name, return type, and types
of parameters of the functions. With these prototype declarations,
they can be called before they are entirely defined, allowing, for
example, to place the function from where they are called (main)
before the actual definition of these functions.
The basic function declaration is as follows:
return_type function_name (parameter list);
IT 103
Computer Programming 1
Function Call
While creating a C function, you give a definition of what the function
has to do. To use a function, you will have to call that function to
perform the defined task.
IT 103
Computer Programming 1
C++ Program Structure with User-Defined Functions
IT 103
Computer Programming 1
C++ Program Structure with User-Defined Functions
C++ programs can be written where the function definition is above
Structure 2:
main(). In this case, a function declaration is not required. This will
make the structure of the code more organized and easier to read.
IT 103
Computer Programming 1
UNIT 4
Modular Programming
IT 103
Computer Programming 1
Lesson 3
Variables ,Return Type and Parameters
of a Function
IT 103
Computer Programming 1
Variables
A function can have variables that are local to its scope only, or it can
use variables that can be accessed throughout the program, referred
to as local variables and global variables, respectively.
Variables that are declared inside a function
are called local, while global variables are
created by declaring them outside of any
function.
IT 103
Computer Programming 1
Return Type
A function may return a value. The return type of the function is the
data type of the value the function returns.
IT 103
Computer Programming 1
Return Type
User-defined functions in C++ are classified into two categories based
on the return type of the function, namely:
Void Function
These functions do not return a value to the calling function. Thus,
they are not used (called) in an expression. A call to a void function is a
stand–alone statement.
IT 103
Computer Programming 1
Return Type
Void Function
IT 103
Computer Programming 1
Return Type
IT 103
Computer Programming 1
Function Parameters
Parameters refer to the type and identifier, and arguments are the
values passed to the function.
•Actual parameters. These are variables found in the function call whose
values will be passed to the formal parameters of the called function.
IT 103
Computer Programming 1
Function Parameters
formal parameters
actual parameters
IT 103
Computer Programming 1
UNIT 4
Modular Programming
IT 103
Computer Programming 1
Lesson 4
Basic Function Designs
IT 103
Computer Programming 1
The Four Basic Function Designs
IT 103
Computer Programming 1
void functions with no parameters
IT 103
Computer Programming 1
void functions with parameters
IT 103
Computer Programming 1
non-void functions with no parameters
IT 103
Computer Programming 1
non-void functions with parameters
IT 103
Computer Programming 1