Assignment Statement: Csc128 Mahfudzah Othman Uitm Perlis
Assignment Statement: Csc128 Mahfudzah Othman Uitm Perlis
Mahfudzah Othman
UiTM Perlis
CHAPTER 2
SEQUENTIAL CONTROL STRUCTURE
SEQUENTIAL STATEMENT
Is one of the statement structures in programming.
Instruction in algorithm design can be translated into these statements.
Assignment Statement
To assign values to variables without keying in the data through input devices.
#include <iostream.h>
#include <conio.h>
void main ()
{
int no1 = 10;
int no2 = 20;
Run:
#include <iostream.h>
#include <conio.h>
void main()
{
char status = ‘M’;
cout<<”Your status is = ”<<status;
getch();
}
Run:
Your status is = M
1
CSC128
Mahfudzah Othman
UiTM Perlis
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char title [35];
strcpy(title, “C++ Programming”);
cout<<title;
getch();
}
Run:
C++ Programming
Output Statement
#include<iostream.h>
void main()
{
cout<<”Assalamualaikum”<<”w.b.t.\n”
}
Run:
Assalamualaikum w.b.t.
The symbol << is called the insertion operator or output operator. It inserts
objects into the output stream named on its left.
Here the message has been split into two pieces. As the line is executed from left
to right, each piece is dropped into the output stream: first: “Assalamualaikum”
then “w.b.t.”. Since there are no newline characters or other symbols added to
the stream between these two pieces, they all come out concatenated into a
single line, just as before.
2
CSC128
Mahfudzah Othman
UiTM Perlis
Input Statement
#include <iostream.h>
#include <conio.h>
void main()
{
int age;
cout<<”How old are you?”;
cin>>age;
cout<<”In 10 years, you will be “ age + 10<<”\n”;
getch();
}
Run:
The type shown in boldface in the sample run is the input that is typed by the
user.
Symbol >> is the extraction operator, also call the input operator.
#include <iostream.h>
#include <conio.h>
void main()
{
char first, last;
cout<<”Enter your initials:\n”;
cout<<”\tFirst name initial :”;
cin>>first;
cout<<”\tLast name initial :”;
cin>>last;
cout<<”Hello,”<<first<<”.”<<last<<”.!\n”;
getch();
}
3
CSC128
Mahfudzah Othman
UiTM Perlis
Run:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char name [35];
cout<<”Enter your name:”<<endl;
cin.getline (name,35);
cout<<”Your name is: ”<<name;
getch();
}
Run:
4
CSC128
Mahfudzah Othman
UiTM Perlis
Arithmetic Expression
Statement Result
Cost = price + tax; Cost is assigned a value of price plus
tax.
Owed = total-discount; Owed is assigned the value of total
minus discount.
Area = length * wide; Area is assigned the value of length
multiply wide.
One_eigth = 1/8; One_eigth is assigned the value of 1
divided by 8.
R = 5 % 2; R is assigned the remainder of 5
divided by 2 by using the modulus
operator.
X = -y; X is assigned the value of –y.
Assignment Variations
Variable to the left of the equals sign can also be used to the right of the
equals sign.
Eg: sum = sum +10;
In C++, the expression sum = sum + 10 is not an equation, it is an
expression that is evaluated in two major steps:
Eg: sum = sum + 10
First step: sum + 10
Second step: store the computed value in sum
5
CSC128
Mahfudzah Othman
UiTM Perlis
Programming example:
void main()
{
int sum;
sum=25;
cout<<”The number stored in sum is:”<<sum<<endl;
sum = sum +10;
cout<<”The number now stored in sum is:”<<sum<<endl;
}
Run:
Assignment expressions such as sum = sum + 10, which use the same
variable on both sides of the assignment operator, can be written by using the
following shortcut assignment operators, which are also known as compound
assignment expressions.
+= -= *= /= %=
Example:
Accumulating
6
CSC128
Mahfudzah Othman
UiTM Perlis
Explanations:
o The first statement initializes sum to 0.
o As each number is added, the value stored in sum is increased
accordingly.
o After completion of the last statement, sum contains the total of all
the added numbers.
Programming example:
void main()
{
int sum;
sum=0;
cout<<”sum =”<<sum<<endl;
sum +=96;//sum = sum+96
cout<<”sum now is=”<<sum<<endl;
sum +=70;//sum= sum+70
cout<<”sum now is=”<<sum<<endl;
sum +=85;
cout<<”sum now is=”<<sum<<endl;
sum +=60;
cout<<”The final sum is=”<<sum<<endl;
}
Run:
sum=0
sum now is= 96
sum now is=166
sum now is= 251
The final sum is =311
Counting
7
CSC128
Mahfudzah Othman
UiTM Perlis
j = j + 2; increased by 2
m = m + 2;
k = k + 3; increased by 3
Explanations:
o Each example shows that the same variable is used on both sides
of the equals sign.
o After the statement is executed, the value of the respective variable
is increased by a fixed amount.
C++ provides 2 unary operators:
o Using the increment operator (++)
Using the decrement operator (--)
Programming example:
void main()
{
int count = 0;
cout<<”count =”<<count<<endl;
count++;
cout<<”count now is=”<<count<<endl;
count++;
cout<<”count now is=”<<count<<endl;
count++;
cout<<”The final count is=”<<count<<endl;
}
Run:
count=0
count now is= 1
count now is=2
The final count is= 3
8
CSC128
Mahfudzah Othman
UiTM Perlis
Example on postfix:
k = n++
1) current value of n is assigned to k. so, k=n.
2) Then, value of n is incremented by 1. so, n=n+1.
Prefix: when the ++/-- operator appears before a variable, it is called a prefix
operator. Eg: ++count, --count
Example on prefix:
k = ++n
1) value of n is incremented by 1. so, n=n+1.
2) Then, new value of n is assigned to the variable k. so, k=n.
Programming example 1:
void main()
{
int m=44, n=66;
cout<<”m= “<<m<<”,n=”<<n<<endl;
++m;
--n;
cout<<”m= “<<m<<”,n=”<<n<<endl;
m++;
n--;
cout<<”m= “<<m<<”,n=”<<n<<endl;
}
Run:
m=44, n=66
m=45, n=65
m=46, n=64
Both the pre-increment operator ++m and the post-increment operator m++ have
same effect here: they add 1 to the value of m. Similarly, both the pre-increment
operator –n and the post-decrement operator n—have the same effect here.
They subtract 1 from the value of n.
9
CSC128
Mahfudzah Othman
UiTM Perlis
void main()
{
int m=66, n;
n=++m;
cout<<”m= “<<m<<”,n=”<<n<<endl;
n=m++;
--n;
cout<<”m= “<<m<<”,n=”<<n<<endl;
cout<<”m= “<<m++<<endl;
cout<<”m= “<<m;
cout<<”m= “<<++m<<endl; }
Run:
m=67, n=67
m=68, n=67
m=68
m=69
m=70
Formatted Output
o Most programs are judged, in fact on the perceived ease of data entry and the
style and presentation of their output.
o Eg: to display 1.8970000
it should be displayed as 1.89 or 1.90 depending on whether
rounding or truncation is used.
o The format of number displayed by cout can be controlled by field width
manipulators included in each output stream.
o Eg: Commonly used stream manipulators
Manipulator Action
setw(n) set the field width to n
setprecision(n) set the floating point precision to n places
setiosflags(flags) set the format flags
o Eg: setw(n) it is used in printing columns of numbers so that the numbers
in each column align correctly.
o Programming example:
10
CSC128
Mahfudzah Othman
UiTM Perlis
void main()
{
cout<<6<<endl;
cout<<18<<endl;
cout<<124<<endl;
cout<<___<<endl;
cout<<(6+18+124)<<endl;
}
Run:
6
18
124
____
148
Solution:
#include<iomanip.h>
void main()
{
cout<<setw(3)<<6<<endl;//field width manipulator must be
included for each occurrence of a
number inserted into “cout”
cout<<setw(3)<<18<<endl;
cout<<setw(3)<<124<<endl;
cout<<___<<endl;
cout<<(6+18+124)<<endl;
}
Run:
6
18
124
____
148
11
CSC128
Mahfudzah Othman
UiTM Perlis
o Eg 1:
cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(3)<<25.67;
Run:
____25.670
o Eg 2:
cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<25.6778;//
the number is rounded to the indicated number of decimal places
Run:
_____25.68
12