CSC 218 Lecture Slides (Intro To C++)
CSC 218 Lecture Slides (Intro To C++)
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.
Example:
1.
#include <iostream>
int main(){
Preprocessor directives cout<<“Hello world”;
}
User-defined function(s) }
int main(){
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 error-
Debug the Code
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:
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
--Ternary
Decrement
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
• Simple statements
• Compound statements
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
Note: Always use descriptive names as identifiers e.g. total is a better identifier than t
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;
data_type list_of_varName;
e.g.
float vol, height, weight;
float age=32, height, weight=75;
Constants
Literals Example
Integer
Constant Declaration
657, O657(octal), Ox657(hexadecimal)
the syntax for declaring constant are:
using the const keyword
Float 0.567, 1.8E3, 5.6E-4
e.g. const float height=1.92;
using the #define preprocessor
Character
e.g. #define height 1.92 ‘A’, ‘&’
String
e.g.
data_type list_of_varName;
“amos”, “abdulganiu”
float vol, height, weight;
float age=32, height, weight=75;
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++;
}