Sie sind auf Seite 1von 12

CSC128

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.

i) Numeric data type:

#include <iostream.h>
#include <conio.h>

void main ()
{
int no1 = 10;
int no2 = 20;

sum = no1 + no2;


cout<<”The sum of two numbers = “<<sum;
getch();
}

Run:

The sum of two numbers = 30

ii) 1 char type

#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

iii) More than 1 char (string)

#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

Here is a simple example of output:

#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

i) Numeric data type:

#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:

How old are you? 21


In 10 years, you will be 31

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.

ii) 1 char type

#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:

Enter your initials:


First name initial: M
Last name initial: O
Hello, M. O.!

iii) More than 1 char (string)

#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:

Enter your name: Mahfudzah


Your name is: Mahfudzah

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.

More examples of C++ Arithmetic Expressions:

Algebra C++ Expressions


(2)(3)+(4)(5) (2*3) + (4*5)
6+18 divide 2 (6+18)/2
4.5 divide 12.2-3.1 4.5/(12.2-3.1)
4 mod 2 4%2
10% from 50 (10/100)*50
4.6(3.0+14.9) 4.6 * (3.0+14.9)
(15.3-3.8)(12.1+18.9) (15.3-3.8) * (12.1+18.9)

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:

The number stored in sum is: 25


The number now stored in sum is: 35

First step: sum = 25


Second step: sum = sum [25] + 10
So, sum = 35

 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:

 sum = sum +10  sum+=10


 price = price * rate  price*=rate
 price = price * (rate+1)  price*=rate+1

Accumulating

 Expression like sum+=10 is required in accumulating subtotals when data is


entered one number at a time.
 Eg: If we want to add numbers 96, 70, 85 and 60, we could use the following
statements:

Statement Value in sum


sum = 0 0
sum +=96 96
sum +=70 166

6
CSC128
Mahfudzah Othman
UiTM Perlis

sum +=85 251


sum +=60 311

 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

 An assignment statement that is very similar to the accumulating statement is


the counting statement.
 It has the form: variable = variable + fixed number
 Eg: i = i +1;
n = n + 1; increased by 1
count = count + 1;

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 (--)

Increment operator (++)


 We can replace the expression:
n = n +1  n++/++n or count = count +1  count++/++count

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

Decrement operator (--)


 We can replace the expression:
n = n -1  n--/--n or count = count -1  count--/--count
These operators transform a variable into a statement expression that
abbreviates a special form of assignment, which are the postfix and prefix
operators
 Postfix : when the ++/-- operator appears after a variable, it is called a postfix
operator. Eg: count++, count –

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.

Programming example 2: This show the difference between the pre-increment


and the post-increment

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

In the first assignment, m is pre-incremented, increasing its value to 67, which is


then assigned to n. In the second assignment, m is post-increment, so 67 is
assigned to n and then m is increased to 68.

In the third output statement, m is post-incremented, so the current value of m


(68) is dropped into the output stream and then m is increased to 69. In the last
output statement, m is pre-incremented, so m is increased to 70 first and then
that value is dropped into the output stream.

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

o For formatted floating-point numbers, require the use of 3 field width


manipulators.
o Sets the total width of the display (include decimal point).
o Sets the output type (exponential/conventional decimal display).
o Determine how many digits can be printed to the right of the
decimal point.

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

Das könnte Ihnen auch gefallen