Chapter 2 Basic Program Elements Part 2
Chapter 2 Basic Program Elements Part 2
PROGRAMMING
FUNDAMENTALS
Upon completion of
this course, students
should be able to:
CLO1 - Implement CLO2 - Show simple
programming programs by developing
element and code to solve problems in a
articulate how they computer using C++
are used to achieve programming language.
a working program. ( P2, PLO 3 )
( C3, PLO 2 )
CHAPTER 2
BASIC PROGRAM
ELEMENTS
LESSON LEARNING OUTCOMES
stream Description
cin Standard input stream
cout Standard output stream
STANDARD OUTPUT (cout)
On most program environments, the standard
output by default is the screen, and the C++
stream object defined to access it is cout
Format / Syntax:
cout<<item1<<item2<<item3;
Example 2:
cout<<”Polytechnic Malaysia”;
Generate an output of Polytechnic Malaysia
STANDARD OUTPUT (cout)
iv) Getting Out Variable Information
Example 1:
char VariableName[4]=”Ali”; //Declare Variable
cout<<VariableName; //Getting out variable information
Example 2:
int Nombor=99; //Declare Variable
cout<<Nombor; //Getting out variable information
STANDARD OUTPUT (cin)
i) Definition
cin is the Standard Input Stream
OBJECT.
>> is the Stream Insertion
Operator.
STANDARD INPUT (cin)
1. Format:
cin>>item1;
3. Rules :
i) only one character is read at a time.
STANDARD INPUT (cin)
Example 1:
#include <iostream> User type : abu
using namespace std; Output :a
void main ( )
User type : 79
{ Output :7
char Name;
cin>>Name;
‘ \ 0’
cout<<Name;
} char : Name
STANDARD INPUT (cin)
Example 2 :
#include <iostream> User type : abu
Using namespace std; Output : abu
void main ( )
{ User type : 79
char Name [20]; cin>>Name;
Output : 79
cout<<Name;
}
PROBLEM WITH cin WHEN READING string
White space (Blanks, tabs, new lines and form
feed) are ignored by cin using >> operator and
left it in the keyboard buffer
Example :
#include <iostream>
using namespace std;
void main ( )
{
char Name [5]; u t
tp
cin>>Name;
ou
cout<<Name;
}
“=” : This is the simplest assignment operator. This operator is used to assign the
value on the right to the variable on the left.
For example : a = 10; b = 20; ch = 'y';
“+=” : This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the
current value of the variable on left to the value on right and then assigns the result to
the variable on the left.
Example : (a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11
“-=” : This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts
the value on right from the current value of the variable on left and then assigns the
result to the variable on the left.
Example : (a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2
1) ASSIGNMENT OPERATORS
Example :
int answer; //define answer as integer
answer =10/3; // real answer = 3.3333 (floating point)
cout<<answer; // but you will get answer =3
3) PRE-INCREMENT AND PRE-DECREMENT
OPERATORS
PreIncrement ( + + X )
PreDecrement ( - - X )
PostIncrement ( X + + )
PostDecrement( X - - )
1. + + X is equivalent to X+ 1.
2. - - X is equivalent to X-1.
Example:
Y = + + x +5, mean X will be increment by
one before added to 5.
Y = X -- +5 mean Y is equal to X + 5, and
after that the value of X will be decrement
by 1.
Example of program
//Example for preincrement and predecrement Memory Output :
#include <iostream>
using namespace std;
void main ( )
{
int x=5; ++X=X+1
int y=10; X = 5+1 X=6
y=++x-5; //Preincrement Y = 6-5 Y=1
cout<<"\nY = "<<y;
cout<<"\nX = "<<x;
Y = X-5
y=x-- -5; //Postdecrement Y = 6 -5 Y=1
cout<<"\nY = "<<y; X--= X-1
cout<<"\nX = "<<x; X= 6-1 X=5
y=x++-5; //Postincrement Y = X-5
cout<<"\nY ="<<y; Y = 5-5 Y = 0
cout<<"\nX ="<<x; X++ = X+1
} X = 5+1 X=6
COMPOUND ASSIGNMENT EXPRESSIONS
n=n+8;
It simply adds 8 to n
Example 2
#include >iostream>
using namespace std;
// Tests combined operators:
void main ( )
{
int n = 44;
n +=9; //n= n+9
cout<<n<< endl;
n –=5 ; //n=n-5
cout <<n<< endl;
n *= 2; //n=n*2
cout <<n<< endl;
return 0;
}
Output
53
48
96
The statement n += 9 adds 9 to n, the
statement n - =5 substract 5 from n,
n*= 2 multiplies n by 2.
4) RELATIONAL OPERATORS/BOOLEAN OPERATORS
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand, if yes then
condition becomes true.
< Checks if the value of left operand is less than (A < B) is true.
the value of right operand, if yes then
condition becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand, if
yes then condition becomes true.
<= Checks if the value of left operand is less than (A <= B) is true.
or equal to the value of right operand, if yes
then condition becomes true.
Program Example 1 :
#include <iostream>
using namespace std;
void main ( )
{
int Number; cout <<”Please
press 5 and enter : ”; cin>>Number;
if ( Number = = 5 )
cout<<”You just press 5! “ else
cout<<”You did not press 5”;
}
Output 1
User type: 3
Out :You did not press 5
User type: 5
Out : You just press 5.
Program Example 2 :
#include <iostream>
using namespace std;
void main ( )
{ cout << (3+4)<<”\n”;
cout<<(3>4)<<”\n”;
cout<<(3<4)<<”\n”;
cout<<( ‘j’ >’k’ )<<”\n”;
}
Output 2
Output :
7
0
1
0
5) LOGICAL OPERATORS
Math C++
AND &&
OR ||
NOT !
The test result / Output :
either ‘yes’ or ‘no’ - ‘1’ or ‘0’ only
A B A&&B
0 0 0
0 1 0
1 0 0
1 1 1
Example 1 :
The “=“ is used to assign the value on the right to the variable
on the left.
The “==“ operator checks whether the two given operands
are equal or not. If so, it returns true. Otherwise it returns
false
The differences can be shown in tabular form as follows:
int main()
{
int h=1, i=2,j=3,k=4; //initialized variables
int total=0;
total=(h < i + j && k);
cout<<total;
Output :
OPERATOR PRECEDENCE
Example :
OPERATOR PRECEDENCE
Example :