0% found this document useful (0 votes)
20 views

CSC 218 Lecture Slides [Intro to C++]

Uploaded by

Habeeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CSC 218 Lecture Slides [Intro to C++]

Uploaded by

Habeeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CSC 218:

Computer Programming III (Scientific)


(3 Credits)

Amos O. Bajeh, PhD


Room 29, Ground Floor
CIS Building
Introduction to C/C++
Programming Language
C/C++ Programming Language
 History of C and C++ -assignment

 Fundamentals of C/C++

 Control structures

 Arrays and Pointers

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

Preprocessor directives #include <iostream>


int main(){
Main function definition cout<<“Hello world”;
}
2. Example:
Preprocessor directives #include <iostream>
User-defined function(s) void greet(){
Main function definition cout<<“Hello world”;
}
int main(){
greet();
}
3.
Preprocessor directives
User-defined function declaration(s)
Main function definition

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

Is
Debug the Code error-
free

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:

Escape sequence meaning


\n New line
\t tab
\a alert
\\ backslash
\’ Single quotes
\” Double quotes
C Programming Language
 Fundamentals of C
reserved words: are words which have predefined meaning and
can only be used for the purpose they are defined.
auto, break, case, char, const, continue, default, do, double,
enum, else, extern, float, for, goto, if, int, long, register, return,
short, signed, sizeof, static, struct, switch, typedef, union,
unsigned, void, volatile, while

Comments : are descriptive text embedded within programs to


add explanatory notes into codes. They are not executed.
Comments can be enclosed in the symbols /* and */. In C++,
they can also begins with the symbol //.
 Fundamentals of C
Operators:
Arithmetic operators, Assignment operators, Logical operators,
Relational operators, Unary operators and Ternary/conditional
operator, shift operators, bitwise operators, comma operator
 Arithmetic operators

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

 A Statement is a complete program construct that causes


and execution. It is simply an expression terminated with a
semicolon:
 Assignment statement
• Simple statements
 Declaration statement • Compound statements
 Control flow statement
 Data Types
 Primitive/Basic data types
 Character (char)
 Integer (int)
 Float (float)
 Double (double)
These types can be further described using the qualifiers short,
long and unsigned as follows:
short int, long int, unsigned int, long float

 Composite/Derived data types


 Array
 Pointer
 Structure (struct) and union
 Enumeration (enum)
 Data Types:
 Primitive/Basic data types cont’d
Data type Memory space (bits) range
short 16 [-32768, 32767]
unsigned short 16 [0, 65535]
int 16 [-32768, 32767]
unsigned int 16 [0, 65535]
long 32 -2147483648, 2147483647
unsigned long 32 [0, 4294967295]
float 32 [3.4E-38, 3.4E38]
double 64 [1.7E-308, 1.7E308]
long double 80 [3.4E-4932, 1.1E4932]
unsigned char 8 [0, 255]
char 8 [-128, 127]
 Fundamentals of C/C++
Identifiers: are names assigned to program entities
Rules for constructing an identifier:
 Must start with an alphabet or the underscore ( _ )

 Other characters can be one or a combination of alphabets, digits or


underscore

 Must not be a reserved word

 C/C++ is case sensitive i.e. name, Name, naMe and NaMe are four
different identifiers

Note: Always use descriptive names as identifiers e.g. total is a better


identifier than t

Examples: _age, 4ages, cumulative_salary, electric&water, 123, auto,


 Fundamentals of C/C++

variables and constants :

Variables: are program entities that can change in


value during the course of program execution

Constants: are program entities that cannot change in


value during the course of program execution
 Variable Declaration
the syntax for declaring variables are:
 dataType varName;
e.g. int age;

 dataType varName [=initial value];


e.g. float height = 1.92;

 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”;
 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: printf(“Sunday”);
break;
case 2: printf(“Monday”);
break;
case 3: printf(“Tuesday”);
break;
case 4: printf(“Wednesday”);
break;
case 5: printf(“Thursday”);
break;
case 6: printf(“Friday”);
break;
case 7: printf(“Saturday”);
break;
default: printf(“Wrong day number”);
}
 Iteration statement
 while
while(expr)
stmt
e.g.

Short i=1;
While(i<6){
printf(“This is iteration %d“, i);
i=i+1;
}
 Iteration statement
 do - while
do{
stmts
}while(expr);

e.g.

int i=1;
do{
printf(“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++)
printf(“This is iteration %“, i);
 Other control statement
 break
int x=1;
while(x<10){
if(x==8)break;
System.out.println(“X = “,x);
x++;
}
 continue
int x=1;
while(x<10){
if(x==8){x++;continue;}
System.out.println(“X = “,x);
x++;
}

You might also like