CSC 218 Lecture Slides [Intro to C++]-1
CSC 218 Lecture Slides [Intro to C++]-1
Fundamentals of C/C++
Control structures
Functions
Function libraries
Fundamentals of C/C++
Structure of C/C++ program:
Generally, a C/C++ program contain one or more functions of
which one must be a main function from which execution
begins.
1. Example:
Example:
#include <iostream>
void greet();
int main(){
greet();
}
void greet(){
cout<<“Hello world”;
}
Steps for Writing a C/C++ Program
1.Enter code in text editor
2.Compile the code using the C/C++
compiler Enter code into
Text Editor
3.If the code is error-free, execute the
program
4.Execute the program Compile the
Code
No Is
Debug the Code error-
free
Yes
Execute Code
Fundamentals of C/C++
character set:
alphabets: a,b,c ….z, A,B,C…Z
digits: 0…9
special characters: +,-, /, ;, #, !, <, >, =, (, ), ^, ?, &, ,, *, ’, ”, {, }, %, :,|
Escape sequence: contains two characters but treated as a
single character and are used to denote special action:
Operator Description
+ Additive operator (also used for joining strings)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Assignment operators
Operator Description
= x=y
+= x+=y implies x=x+y
-= x-=y implies x=x-y
*= x*=y implies x=x*y
/= x/=y implies x=x/y
%= x%=y implies x=x%y
Relational operators
Operator Description
< Less than: returns 1 (true) if the left hand operand is numerically
less than the right hand operand otherwise it returns 0 (false)
<= Less than or equal to: returns 1 (true) if the left hand operand is
numerically less than or equal to the right hand operand otherwise
it returns 0 (false)
> Greater than: returns 1 (true) if the left hand operand is
numerically greater than the right hand operand otherwise it
returns 0 (false)
>= Greater than or equal to: returns 1 (true) if the left hand operand is
numerically greater than or equal to the right hand operand
otherwise it returns 0 (false)
== Equals to: returns 1 (true) if the left hand operand is numerically
equal to the right hand operand otherwise it returns 0 (false)
!= Not equal to: returns 1 (true) if the left hand operand is
numerically NOT equal to the right hand operand otherwise it
returns 0 (false)
Logical operators
Operator Description
&& And: Returns 1 (true) iff both operands are true
|| Or: Returns true if one or both operands are true
! Negation: Inverts the value of a Boolean
& Bitwise And operator
| Bitwise Or operator
Operators
Unary operators
Operator Description
+ Indicates positive numbers
- Indicates a negative number or expression
++ Increment
-- Decrement
Ternary operator ( ? : )
(relational/logical expr)? expr1 : expr2
e.g.
(age>18) ? salary=25000: salary=45000;
Operator precedence
level operator associativity level operator associativity
1 ( ), [ ], -> Left to right 14 ?: Right to left
2 ~, ++, --, ! Left to right 15 =, +=, -=, *=, /=, %= Right to left
3 *, &, sizeof() Left to right 16 , Left to right
4 *, /, % Left to right
5 +, - Left to right
6 <<, >> Left to right
7 <, <=, >, >= Left to right
8 ==, != Left to right
9 & Left to right
10 ` Left to right
11 | Left to right
12 && Left to right
13 || Left to right
Expression and Statement
An Expression is a construct that consist of variables,
constants, operators or function invocation which yield a
single value
Arithmetic expression
Assignment expression
Logical expression
Relational expression
C/C++ is case sensitive i.e. name, Name, naMe and NaMe are four
different identifiers
data_type list_of_varName;
e.g.
float vol, height, weight;
float age=32, height, weight=75;
Constants
Literals Example
Integer 657, O657(octal), Ox657(hexadecimal)
Float 0.567, 1.8E3, 5.6E-4
Character ‘A’, ‘&’
String “amos”, “abdulganiu”
Constant Declaration
the syntax for declaring constant are:
using the const keyword
e.g. const float height=1.92;
using the #define preprocessor
e.g. #define height 1.92
Control structures
Selection statements
If
If-else
else if
switch
Repetition statements
while
do-while
for
Selection statement
If statement
if (expr) stmt
e.g.
if(age >=18) status = “Adult”;
If-else statement
If(expr) stmt_1
else stmt_2
e.g.
if(age>=18) status = “Adult”;
else status=“Teenager”;
Nexted if
switch statement - char, int and enum,
switch(expr){
case val_1: stmt_1
break;
case val_2: stmt_2
break;
.
.
.
case val_n: stmt_n
break;
[default: default_stmt]
}
E.g.
int weekDay =5;
switch(weekDay){
case 1: cout<<“Sunday”;
break;
case 2: cout<<“Monday”;
break;
case 3: cout<<“Tuesday”;
break;
case 4: cout<<“Wednesday”;
break;
case 5: cout<< “Thursday”;
break;
case 6: cout<<“Friday”;
break;
case 7: cout<<“Saturday”;
break;
default: cout<<“Wrong day number”;
}
Iteration statement
while
while(expr)
stmt
e.g.
Short i=1;
While(i<6){
cout<<“This is iteration %d“<<i;
i=i+1;
}
Iteration statement
do - while
do{
stmts
}while(expr);
e.g.
int i=1;
do{
cout“This is iteration %d“<<i;
i=i+1;
}While(i<6);
Iteration statement
for
for ([initialization]; [termination]; [increment/decrement]){ statement(s)
}
e.g.
for(Short i=1;i<6;i++)
cout<<“This is iteration %“<<i;
Other control statement
break
int x=1;
while(x<10){
if(x==8)break;
cout<<“X = “<<x;
x++;
}
continue
int x=1;
while(x<10){
if(x==8){x++;continue;}
cout<<“X = “<<x;
x++;
}
BASIC C++ Input and Output
function body
}
Function Definition
where:
returnType : is the data type of the value the
function is expected to return;
functionName is an identifier for referencing the
function;
list of para: is the declaration of one or more
variables separated by commas to represent the
value(s) that will be passed to the function. These
variables are referred to as formal parameters; and
function body is the line(s) of statements that will
perform the task designated to the function.
Function Definition
Example:
the main( ) function that we’ve defined so far in our
codes.
int main(){
int age=10;
if(age<=18)
cout<<"\nA Teenager";
else
cout<<"\nAn Adult";
return 0;
}
Function Definition
Function call:
add_up(x,y,z);
Passing Arguments to Function:
Pass by Reference: in this call, the reference
(address) of the actual parameters are copied into
the formal parameters. Thus, any change made to
the formal parameter within the function will reflect
on the actual parameters because they are directly
accessed
By reference
Function header:
float add_up(float &x, float &y, float &z)
Function call:
add_up(x,y,z);
Passing Arguments to Function:
Pass by Reference:
By pointer
Function header:
float add_up(float *x, float *y, float *z)
Function call:
add_up(&x,&y,&z);
Function declaration / Prototype:
It is simply the function header terminated with a
semi colon (;)
Example
Function header:
float add_up(float *x, float *y, float *z);
Or
float add_up(float, float, float);