Sei sulla pagina 1di 24

CSC 425 INTROUCTION

TO COMPUTER
PROGRAMMING
CHAPTER 1.1 LANGUAGE BASICS

Learning Objectives
What

is identifier, variables,
constants?
Input and Output
Basic mathematical,
relational and logical
operators
Basic control structures such
as sequence, selection and
noth/csc425 - ch2

What is identifier, variable and


constant?
Basic

structure of C++ programming, includes


header file (library), always start with a
comment, the main function, open curly
bracket, the body, the return statement and
the last one is close curly bracket.
Eg:
#include <iostream.h>
void main()
{
cout <<\n Hello;
return;
}
noth/csc425 - ch2

Header File

Each program must contain the


header file or library. Eg :
iostream.h, iomanip.h, math.h,
conio.h, string.h. If the header file
is not included, the program will
contain many errors.
Sample header files.
EXAMPLE OF HEADER FILES.docx
noth/csc425 - ch2

Identifier
A

name which willbe used to describe functions,


constants, variables, and other items.
The rules for writing identifiers in C++ are:
Identifiers must begin with a letter or underscore.
Only letters(A-Z,a-z), digits(0-9), or underscore( _ ) may
follow the initial letter.

Ex:

sum_of_squares, box_22A, GetData,


count---valid
The blank space cannot be used.

Ex:

Get Data cannot be an identifier as blanks


are not allowed in identifiers
Identifiers cannot be reserved words. Reserved words
or keywords are identifiers reserved for system use.

Ex Int
noth/csc425 - ch2

Variable

Also referred to as identifier. A location


in memory that holds changeable data.
It has a set of operations that can be
used to change or manipulate them.
4 important types of variable : int, char
float, double
Notation in giving the variable names :

Always start with alphabetic characters, eg

int number;
char name[30];
float rate;
double charges;

noth/csc425 - ch2

Variables
An

identifier name can be made up of


only the following:
Letters - "a" through "z", and "A" through
"Z".
Numbers - 0 through 9.
The underscore character "_".

Identifiers

must begin with a letter or


an underscore.
Do not use reserved words such as
"int" since it is indicate the type of the
variable.
noth/csc425 - ch2

Rules of variable declaration:


Start

with alphabetic or underscore


Dont declare the same variable more than once.
Variable cannot start with number. Eg: 1marks,
4_age.
Cannot use the reserved word to declare variable.
Eg : asm, auto, break, case, catch, char, class,
const, continue, default, delete, do, double, else,
enum, extern, float, for, friend, goto, if, inlineint,
long, new, operator, private, protected, public,
register, return, short, signed, sizeof, static, struct,
switch, template, this, throw, try, typedef, union,
unsigned, virtual, void, volatile, wchar_t, while,
noth/csc425 - ch2

char

Variables Type

A 1 byte integer usually used to store a character.

short

A 2 byte integer.

int

A 4 byte integer.

long

4 or 8 byte integer (compiler dependent).

float
double

boolean

Single precision floating point number. (7 digits or more compiler dependent)


Double

precision

floating

point

number.

(15 digits or more - compiler dependent)


Boolean literal. Values are true (numerically=1) or false
(numerically=0).
Number of characters. In order to use this data type, you need

string

to add the statement: #include <string> to the start of your


program.

noth/csc425 - ch2

Constant
A

location in memory that


holds unchangeable data.
Normally constant is applied to
a variable which hold a value
and the value is never change.
Eg : const float pie = 3.142;

noth/csc425 - ch2

10

Commenting The Program


Comment

is very important in any program. The


purpose of commenting the program is to make us
easier to refer and understand the function of the
program. Normally the program will consists like
authors name, date program created, purpose, etc.
Example:
//Author : Aminah Bt Abu Bakar
//Date : 20 Jan 2010
//Purpose : to calculate the total of two numbers

#include <iostream.h>
void main()
{
int a, b;
a=5;
b=6;
cout << a+b;
noth/csc425 - ch2

11

Input and output Statements


The

C++ identifier for input is cin and


output is cout.

Example

of input

Ex 1 : cin >> a;
Ex 2 : cin.get(name, 30);
Example

of output

Ex 1: cout << a;
Ex 2: cout << \n Enter your name:
;
noth/csc425 - ch2

12

Example
#include <iostream.h>
void main()
{
char name[100];
int year, age;
const int currentyear = 2007;
cout<<\n Enter your name : ;
cin.get(name,30);
cin.ignore(80,\n);
cout<<\n Enter year of birth : ;
cin>>year;
age = currentyear year;
cout << \nName : <<name << and the age is <<age;
return;
}
noth/csc425 - ch2

13

Important!!!
the

usage of \n, \t and \a


every single line end with ;
every program must end with
return(); or return 0;
(depends on types of method)

noth/csc425 - ch2

14

Character Escape Sequences


Predefined

symbols can be used


to display better output.

\n

Newline.

\t

Horizontal tab.

\r

Carriage return.

\a

Alert sound (bell).

\\
\"

Outputs a backslash
character.
Outputs a double quote
character.
noth/csc425 - ch2

15

Example

: Input Statement
Data to be keyed in through keyboard
eg: cin >> value1
cin.get(name, 30);
cin.getline(name, 30);

Example

: Assignment

To assign a value to variable without keying


in the data. The value has been assigned in
the program.
Eg : mark = 3, grade = A, strcpy(name,
Sameha)

noth/csc425 - ch2

16

Basic Mathematical, Relational


and Logical Operators
The

Basic Operators in mathematical


operations are +, -, /, * and %
+ sum = a + b;
- sub = a b;
* mult = a * b;
/ div = a / b;
% mod = a % b;

The

complex mathematical operations,


normally will involved the usage of brackets.

Eg : x = (y-z) + b/(y+a);
noth/csc425 - ch2

17

Parenthesis

is used in C++
expressions in much the same
manner as in algebraic expressions.
Eg: a * (b+c)

There

are several rules that you need


to know:
Operators in expressions contained within
pairs of parenthesis are evaluated first.
*, / and % are applied next.
+ and operations are applied last.

Refer

to examples:
Example mathematics Problem.docx
noth/csc425 - ch2

18

Relational Operators

Are:

<, >, <=, >=, !


=, ==,
7 < 8 1 (true)
7>=71
(true)
8 != 8 0 false)

Logical Operators
Are:

&& (AND), || (OR) and !


(NOT)

(7 > 8) || ( 8 < 7)

0(false)
(8>7) && (7<8) 1
(true)
(8>=7) 1 (true)
!(8==8) 0 (false)
noth/csc425 - ch2

19

The Truth Table

A B A&&B
000 0
010 1
100 1
111 1

A||B !A
1
1
0
0
noth/csc425 - ch2

20

Mathematics Function
MUST

add header
Examples:

#include <cmath>

abs(x) //display positive from x


pow(x,y) //xy
sqrt(x)
exp(x) //ex
log(x) //ln x (natural log)
log10(x) //log10 x (base 10)
sin(x) //x is angle in radian
cos(x) //convert angle from degree to
radian
tan (x)
noth/csc425 - ch2

21

Increment and Decrement


Operators
Example:-

int i = 3, j = 3;
i++ // i becomes 4
j-- // j becomes 2
Example:-

i =1;
int j =
int j =
int j =
int j =

++i ; //j is 2, i is 2
i++; //j is 1, i is 2
--i;
//j is 0, i is 0
i--; //j is 1 , i is 0
noth/csc425 - ch2

22

Other shorthand Assignment


Operators
It

is used when the current value


of a variable is used, modified
and then reassigned back to the
same variable.
Example:

i
i
i
i
i

=
=
=
=
=

i + 3;
i 3;
i * 3;
i / 3;
i% 3;

-> i+=3;
-> i-=3;
> i *=3;
->i /=3;
->i %=3;
noth/csc425 - ch2

23

Formatting Program
Output
#include

<iomanip>
cout<<setw(8);
//print the next number in a 8
character wide column
Cout<<setw(8)<<fixed<<setfill(*)
<< setprecision(2)<<25.674
//prints the 25.674 in a field of width
8 and with 2 digits of precision
***25.67
noth/csc425 - ch2

24

Potrebbero piacerti anche