Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Header File
Identifier
A
Ex:
Ex:
Ex Int
noth/csc425 - ch2
Variable
int number;
char name[30];
float rate;
double charges;
noth/csc425 - ch2
Variables
An
Identifiers
char
Variables Type
short
A 2 byte integer.
int
A 4 byte integer.
long
float
double
boolean
precision
floating
point
number.
string
noth/csc425 - ch2
Constant
A
noth/csc425 - ch2
10
#include <iostream.h>
void main()
{
int a, b;
a=5;
b=6;
cout << a+b;
noth/csc425 - ch2
11
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
noth/csc425 - ch2
14
\n
Newline.
\t
Horizontal tab.
\r
Carriage return.
\a
\\
\"
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
noth/csc425 - ch2
16
The
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
Refer
to examples:
Example mathematics Problem.docx
noth/csc425 - ch2
18
Relational Operators
Are:
Logical Operators
Are:
(7 > 8) || ( 8 < 7)
0(false)
(8>7) && (7<8) 1
(true)
(8>=7) 1 (true)
!(8==8) 0 (false)
noth/csc425 - ch2
19
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>
21
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
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