E3062 Programming
E3062 Programming
POLYTECHNICS
MINISTRY OF EDUCATION
MODULE E3062
PROGRAMMING
download@
http://modul2poli.blogspot.com
BIODATA OF MODULE WRITERS
E3062 PROGRAMMING 1
Editor
Name :
Address :
Telephone No. :
e-mail :
Qualifications :
Position :
download@
http://modul2poli.blogspot.com
What Do You Think Of This Module?
Course : ____________________________________
4 Strongly Agree
3 Agree
2 Disagree
1 Strongly Disagree
No. How much do you agree with the following statements? SCALE
A. FORMAT 1 2 3 4
1 The pages are organized in an interesting manner.
2 The font size makes it easy for me to read the module.
The size and types of pictures and charts used are suitable for
3
the input.
4 The pictures and charts are easy to read and understand.
5 The tables used are well-organised and easy to understand.
6 The arrangement of the Input makes it easy for me to follow.
7 All the instructions are displayed clearly.
B. CONTENTS 1 2 3 4
8 I understand all the objectives clearly.
9 I understand the ideas conveyed.
10 The ideas are presented in an interesting manner.
11 All the instructions are easy to understand.
12 I can carry out the instructions in this module.
13 I can answer the questions in the activities easily.
14 I can answer the questions in the self-assessment.
15 The feedback section can help me identify my mistakes.
16 The language used is easy to understand.
17 The way the module is written makes it interesting to read.
18 I can follow this module easily.
19 Each unit helps me understand the topic better.
I have become more interested in the subject after using this
20
module.
download@
http://modul2poli.blogspot.com
CURRICULUM GRID
The curriculum grid of this module is based on the curriculum used by Malaysian
polytechnics.
7 Introduction To 8 9 4 Hours
Visual Basic (2H) (2H)
download@
http://modul2poli.blogspot.com
UNIT 1 Basic Concept And Problem Solving in Programming
1.0 Programming Basic Concept
1.1 Definition of Programming
1.2 The components of programmed structure
1.2.1 Declaration
1.2.2 Input
1.2.3. Storage
1.2.4. Operation
1.2.5. Control
1.2.6. Output
1.3 Identifying steps in programming
1.3.1. Definition and analyze
1.3.2. Planning user variables
1.3.3. Drawing Flowchart.
1.3.4. Program Writing
1.3.5. Testing and debugging the programmed
1.3.6. Documentation the programmed
download@
http://modul2poli.blogspot.com
UNIT 3 Introduction To The Concept And Basic Instruction of
Programming C++
3.0 Introduction The Technique To Develope C++ Programming .
3.0.1. Alogarithm
3.0.2 Flowcharts
3.0.3 Pseudocode
3.0.4 Structure chart.
3.1 Using Algorithm And Flowchart In Programming:
3.1.1 Algorithm
3.1.2. Flowchart
download@
http://modul2poli.blogspot.com
5.1.1 Sequence structure
5.1.2 Selection structure – Simple If, If-Then-Else, Case
5.1.3 Repetition structure – For, While, While…do
5.1 Compiles and Runs Program
download@
http://modul2poli.blogspot.com
8.4 Save and Run the project.
9.0 Introduction
9.1 Procedure Events
9.2 Create object an event procedure
9.3 Using a simple Visual Basic statement
9.3.1 Declaration
9.3.2 Sequence
9.3.3 Selection
9.3.4 Repetition
download@
http://modul2poli.blogspot.com
MODULE GUIDELINES
To achieve maximum benefits in using this module, students must follow the
instructions carefully and complete all the activities.
B3062 / UNIT 1 / 5
3. The general and specific objectives are given at the beginning of each unit.
4. The activities in each unit are arranged in a sequential order and the
following symbols are given:
OBJECTIVES
The general and specific objectives for each learning topic are stated
in this section.
INPUT
This section introduces the subject matter that you are going to learn.
ACTIVITIES
The activities in this section test your understanding of the subject
matter. You have to complete this section by following the
instructions carefully.
FEEDBACK
Answers to the questions in the activity section are given here
download@
http://modul2poli.blogspot.com
SELF-ASSESSMENT
Self-assessment evaluates your understanding of each unit.
FEEDBACK OF SELF-ASSESSMENT
This section contains answers to the activities in the self-assessment.
.
download@
http://modul2poli.blogspot.com
GENERAL AIMS
This module is prepared for students in the third semester who are following the
Certificate/Diploma programmes in Malaysian Polytechnics. It aims to expose
students to the Programming concept in each unit and to lead them towards self-
directed learning or with guidance from their lecturers.
The prerequisite for this module is at least the have to know how to used the
computer.
GENERAL OBJECTIVES
download@
http://modul2poli.blogspot.com
TEACHING AIDS AND RESOURCES NEEDED
REFERENCES
download@
http://modul2poli.blogspot.com
E3062/1/1
Basic Concept And Problem Solving in Programming
UNIT 1
OBJECTIVES
download@
http://modul2poli.blogspot.com
E3062/1/2
Basic Concept And Problem Solving in Programming
INPUT
Before we start writing our own programs let us examine some important
characteristics of well-written computer programs. Basically these apply to
any programming language that provide a useful set of guidelines.
a. Integrity. This refer to the accuracy of the calculations since it will be
meaningless if calculations are not carried out correctly.
b. Clarity. This refer to the overall readability of the program, with
particular emphasis on its underlying logic. If a program is clearly
written, it should be possible for programmer to follow the program logic
with ease.
c. Simplicity. The clarity and accuracy of a program are usually enhanced
by keeping things as simple as possible, consistent with the overall
program objectives.
d. Efficiency. This is concerned with execution speed and efficient memory
utilization.
e. Modularity. Many programs can be broken down into a series of
identifiable subtask that enhances accuracy and clarity of a program and
facilitates future program alterations.
f. Generality. Usually we will want a program to be as general as possible,
within reasonable limits. A considerable amount of generality can be
obtained with very little additional programming effort.
download@
http://modul2poli.blogspot.com
E3062/1/3
Basic Concept And Problem Solving in Programming
1.2.1 Declaration
A declarations associates a group of variables with a specific data
type. All variables must be declared before they can appear in
excutable statements.
Example :
int a, b, c;
float root1, root2;
char flag, text[80]
1.2.2 Input
A set of information called data will be entered into the computer
from keyboard, floppy disk, hard disk etc. and stored in a portion of
the computer memory. This input which is an input to the computer
will then be processed to produced the desired result.
1.2.3 Storage
Every piece of information are stored within the computer’s memory
which is encoded as some unique combination of zeros and ones.
Small computers have memories that are organized into 8-bit
multiples called bytes. Large computer have memories that are
download@
http://modul2poli.blogspot.com
E3062/1/4
Basic Concept And Problem Solving in Programming
1.2.4 Operation
There are two different ways computer can be utilized by many
different users. These are batch mode and the interactive mode. In
batch mode of operation the program and the data are typed into the
computer and stored within computer’s memory and processed in its
proper sequence. Large quantities of information can be transmitted
into and out of the computer without the user present while the job
being processed. Batch processing can be undesirable for simple jobs.
In interactive mode the user and the computer are able to interact with
each other during computational session.
1.2.5 Control
Program control refers to the order of execution of instructions in a
program. The instruction can be executed sequentially – one by one,
from top downwards or non sequential execution of program
instruction. Most real life problems require some kind of decision
making to take a certain course of action. This means that instruction
or a whole block of instructions can be executed, repeated or skipped.
1.2.6 Output
The processed data which produced certain result is known as the
output. The output data will be presented in a sheet of paper through
the printer or display on a monitor.
download@
http://modul2poli.blogspot.com
E3062/1/5
Basic Concept And Problem Solving in Programming
Activity 1A
1.3 State the difference between batch mode and interactive mode operation
download@
http://modul2poli.blogspot.com
E3062/1/6
Basic Concept And Problem Solving in Programming
Feedback To Activity 1A
1.2 A set of instructions that tells the computer what to do is called a program.
1.3 In batch mode of operation, the program and the data are typed into the computer
and stored within computer’s memory and processed in its proper sequence.
Large quantities of information can be transmitted into and out of the computer
without the user present while the job being processed. Batch processing can be
undesirable for simple jobs. In interactive mode the user and the computer are
able to interact with each other during computational session.
download@
http://modul2poli.blogspot.com
E3062/1/7
Basic Concept And Problem Solving in Programming
INPUT
Process of designing program can be divided into two phases mainly the
problem solving phase and implementation phase. The problem solving
phase consist of steps 1 through 3 and implementation phase involved steps 4
and 5. While in step 6, documentation is done throughout the process of
designing program.
download@
http://modul2poli.blogspot.com
E3062/1/8
Basic Concept And Problem Solving in Programming
resolving it. Analyzing the problems will determine the input, output
and information required to solve these problems, as follows:
a. input – data to be processed
b. output – the desired result
c. the constraint and additional features in resolving the
problems
d.
1.3.2 Planning of variables
Variables are simply references to memory locations. A well plan use
of variables will produce an efficient execution of program in terms
of speed and memory utilization.
download@
http://modul2poli.blogspot.com
E3062/1/10
Basic Concept And Problem Solving in Programming
Activity 1B
download@
http://modul2poli.blogspot.com
E3062/1/11
Basic Concept And Problem Solving in Programming
Feedback To Activity 1B
download@
http://modul2poli.blogspot.com
E3062/1/12
Basic Concept And Problem Solving in Programming
KEY FACTS
download@
http://modul2poli.blogspot.com
E3062/1/13
Basic Concept And Problem Solving in Programming
1.
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 1
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 1-1
Question 1-2
Question 1-3
download@
http://modul2poli.blogspot.com
E3062/1/14
Basic Concept And Problem Solving in Programming
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 1-1
i. Declaration
A declarations associates a group of variables with a specific data type. All
variables must be declared before they can appear in executable statements.
ii. Input
A set of information called data entered into the computer from
keyboard, floppy disk, hard disk etc. and stored in a portion of the computer
memory is known as input.
iii. Output
The processed data which produced certain result is known as the output. The
output data is usually presented in a sheet of paper through the printer or
display on a monitor.
Question 1-2
Once the program has been written it must be compiled and executed. This is
accomplished by an editor and compiler. An editor lets us type a program, makes
changes and save it to a file. The compiler then translates the program into a form
that the computer can read. Once the program has been compiled and executed the
presence of errors will be readily apparent. Syntactic and execution errors usually
download@
http://modul2poli.blogspot.com
E3062/1/15
Basic Concept And Problem Solving in Programming
result in the generation of error when compiling or executing a program. Error of this
type is usually quite easy to find and correct. Much more difficult to detect are
logical errors since the output resulting from logically incorrect program may appear
to be error free. Thus a good bit of probing may be required which is known as
logical debugging.
Question 1-3
CONGRATULATIONS!!!!…..
May success be with you
always….
download@
http://modul2poli.blogspot.com
E3062/2/1
UNIT 2
OBJECTIVES
download@
http://modul2poli.blogspot.com
E3062/2/2
INPUT
To make C++ work for you, you must be able to write C++ program. To give
C++ instructions to a computer, you will need and editor and a C++
compiler. An editor lets you type a C++ program, make changes and save it
in a file. The compiler then translate your C++ program into a form that your
computer can read. Then finally, you will be able to run your program.
To get started, we introduce in this chapter a simple C++ program and then
go on to briefly explain the various parts that make up a C++ program.
download@
http://modul2poli.blogspot.com
E3062/2/3
Program 2.0
2.1.1 Comments
Comments are used to insert remarks into the source code help to
explain about what the program does. In C++, comments can be place
anywhere in the programs. It can be used to include the details of the project
and the programmer who wrote or modified the code.
There are two types of comment used. They are:
download@
http://modul2poli.blogspot.com
E3062/2/4
a. Multi-line Comment
// This is a preprocessor
Example :
#include <iostream.h>
2.1.3 Functions
b. Function block { }
The function body, also called block can be of any size. The function always
ends with the return command.
{ - begin block
} - end block
Example
main ( )
{
download@
http://modul2poli.blogspot.com
E3062/2/6
Activity 2A
2.1 What is the only function all C++ programs must contain?
A. start()
B. system()
C. main()
D. program()
2.2 What punctuation is used to signal the beginning and end of code blocks?
A. { }
B. -> and <-
C. BEGIN and END
D. ( )
2.4 Which of the following are false about Comments used in C++?
A. Used to document programs
B. Improve the readability and understanding of programmer
C. Causes action to be performed when the program is executed
D. None of the above.
download@
http://modul2poli.blogspot.com
E3062/2/7
Feedback To Activity 2A
2.1 C. main()
2.2 A. { }
2.3 C. /*Comments*/
download@
http://modul2poli.blogspot.com
E3062/2/8
INPUT
In the iostream C++ library, standard input and output operations for a
program are supported by two data streams:
a. cin (console input) for input – normally assigned to the keyboard
b. cout (console input) for output – normally directed to the screen.
By handling these two streams you will be able to interact with the user in
your programs since you will be able to show messages in the screen and to
receive his/her input from the keyboard.
The function cout is used with the overloaded operator << (a pair of “less
than” signs). It follows the following format as in the following examples:
Example 2.2.1(a) This format is used to print out string values to the screen
download@
http://modul2poli.blogspot.com
E3062/2/9
Code Meaning
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\” Double quote
\” Single quote character
\\ Backslash
\v Vertical tab
\a Alert
download@
http://modul2poli.blogspot.com
E3062/2/10
Example,
This example shows the use of the new line (\n) character constant to print characters
on the next line.
download@
http://modul2poli.blogspot.com
E3062/2/11
Activity 2B
2.5 Determine which of the following are valid output;. If valid what is the
output
a. cout << “A”;
cout << “B”;
cout << “C”;
download@
http://modul2poli.blogspot.com
E3062/2/12
Feedback To Activity 2B
2.5
a. Valid ABC
b. valid A
B
C
c. Not valid
d. valid ABC
e. valid ABC
2.6
a. valid
b. not valid
c. not valid
d. valid
e. not valid
download@
http://modul2poli.blogspot.com
E3062/2/13
KEY FACTS
1. To make the C++ program executable you must be able to write a set of
instructions that tells the computer what to do.
download@
http://modul2poli.blogspot.com
E3062/2/14
3.
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 2-1
download@
http://modul2poli.blogspot.com
E3062/2/15
Question 2-2
#include <iostream.h>
main{}
(
int m, n;
float q;
m=5;
n=2;
q=m/n;
cout>> “\n”>>q;
m=m+n+q;
out<< “\n”<<m;
return 0;
)
download@
http://modul2poli.blogspot.com
E3062/2/16
Feedback To Self-Assessment 2
Have you tried the question????? If “YES”, check your answer now:
Question 2-1
Question 2-2
download@
http://modul2poli.blogspot.com
B3062/3/1
UNIT 3
OBJECTIVES
download@
http://modul2poli.blogspot.com
B3062/3/2
INPUT
The first step in writing instructions to carry out a task is to determine what the
output should be – that is, what the task should produce. The second step is to
identify the data, or input, necessary to obtain the output. The last step is to
determine how to process the input to obtain the desired output. The general process
of writing program is to analyze the problem, design and plan the solution, coding
download@
http://modul2poli.blogspot.com
B3062/3/3
which is to translate the algorithm into a programming language and finally testing
and debugging.
In this unit we shall discuss the logical sequence of precise steps that solve the
problem known as algorithm and three popular programming tools or techniques
used to develop the logic plan: flowcharts, pseudo code and structure chart or input-
process-output chart.
3.0.1 Algorithm
3.0.2 Flowcharts
download@
http://modul2poli.blogspot.com
B3062/3/4
download@
http://modul2poli.blogspot.com
B3062/3/5
The main advantage of using a flowchart to plan a task is that it provides a pictorial
representation of the task, which makes the logic easier to follow. We can clearly see
each and every step and how it is connected to the next step. For example lets look at the
discount sales program.
Start
End
download@
http://modul2poli.blogspot.com
B3062/3/6
The major disadvantage with flowcharts is that when a program is very large, the
flowcharts may continue for many pages, making them difficult to follow and modify.
3.0.3 Pseudocode
Pseudocode is an abbreviated version of actual computer code. The
geometric symbols used in flowcharts are replaced by English-like statements that
outline the process. Pseudocode looks more like computer code than does flowchart.
It allows programmer to focus on the steps required to solve a problem rather than on
how to use computer language.
download@
http://modul2poli.blogspot.com
B3062/3/7
Calculate price
discount value = price * rate of discount discount price = price – discount value
download@
http://modul2poli.blogspot.com
B3062/3/8
Activity 3A
Question 3.1
i. input/output
ii. processing
iii. decision
Question 3.2
Question 3.3
What is an algorithm?
download@
http://modul2poli.blogspot.com
B3062/3/9
Feedback To Activity 3A
Question 3.1
i. input/output
ii. processing
iii. decision
Question 3.2
1. Pseudocode
2. flowchart
3. structure chart
Question 3A.3
sequencing which emphasize the translating of each step of program outline into one
download@
http://modul2poli.blogspot.com
B3062/3/10
INPUT
Discussion: The average grade equals the sum of all grades divided by
number of students. We need a loop to read and then add (accumulate) the
grades for each student in the class. Inside the loop, we also need to total
(count) the number of students in the class.
Processing: Find the sum of the grades; count the number of students;
calculate average grades = sum of grades / number of students.
download@
http://modul2poli.blogspot.com
B3062/3/11
Start
counter and
Initialize sum start at 0
counter and
sum to 0
Are
There more No
Data?
Yes
read next
Get next
grade
grade
add 1 to
Increment counter
counter
accumulate
Add grade sum of grades
to sum
End
download@
http://modul2poli.blogspot.com
B3062/3/12
Activity 3B
Question 3.1
You are to plan a program to sum two integer entered by user and display the result
of the summation by using the flowchart technique, from the following pseudocode;
download@
http://modul2poli.blogspot.com
B3062/3/13
Feedback To Activity 3B
Question 3.1
Start
Calculate sum of
two number
End
download@
http://modul2poli.blogspot.com
B3062/3/14
KEY FACTS
1. The algorithm should be tested at the flowchart stage before being coded
into a program.
2. Pseudocode, flowcharts and structure charts are universal problem
solving tools that can be used to construct program in any computer
language
3. Flowcharts are used to provide visualization of the flow of certain
programming tasks
download@
http://modul2poli.blogspot.com
B3062/3/15
1.
SELF-ASSESSMENT
You are approaching success. Try the question in this self-assessment section and
check your answers with those given in the Feedback on Self-Assessment given on
the next page. If you face any problems, discuss it with your lecturer. Good luck.
Question 3-1
Every employee who earns more than RM 50,000.00 per annum is required to pay
2.5 % of their nett yearly income. Write a complete program algorithm which will
calculate and display all the employees income tax return. Also, show the flowchart
for the program.
download@
http://modul2poli.blogspot.com
B3062/3/16
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 3-1
Discussion: The program must read the yearly income entered by user and select
income more than RM 50,000.00 to be multiplied by 2.5 %. Income less than
RM50,000.00 will be multiplied by 0. The result will be displayed..
Processing: Select the value of income; more than 50000.00 calculate the income
tax return = yearly income * 0.025. Less than 50000.00 calculate the income tax
return = yearly income * 0.00.
download@
http://modul2poli.blogspot.com
B3062/3/17
Start
End
CONGRATULATIONS!!!!…..
May success be with you
always….
download@
http://modul2poli.blogspot.com
E3062/4/1
UNIT 4
OBJECTIVES
General Objective : To Introduce and Use the data types, operator and
expression in C++ Programming.
download@
http://modul2poli.blogspot.com
E3062/4/2
INPUT 4
The C++ character set includes the keyboard characters, namely the
uppercase letter A - Z, the lower letters a – z , the digits 0 – 9 , and other special
characters (such as!, #, [, ] , ,&,<,?, and * ). The blank space is also a character in
the set.
Identifiers are simply references to memory location which can hold values.
They are formed by combining letters (both upper and lowercase), digits and the
underscore( _ ). The first character of an identifier, however, must be a letter. The
blank character is not permitted in an identifier. Although identifiers can be formed
by freely combining the letters, digits and underscores, common sense tells us that
we should give them suggestive names, that is, names that reflect the data items that
they are going to store. Identifiers can be of any length although in practice they
seldom exceed 25 characters.
download@
http://modul2poli.blogspot.com
E3062/4/3
Table 4.1
Valid Identifier Invalid Identifier Comment
x “x” Illegal character
Sumx2 2sumx Illegal first character
Hourly_rate Hourly-rate Illegal character -
name name@ Illegal character @
GROSS_PAY GROSS PAY Illegal blank
i. int
ii. char
iii. double
iv. float
For additional of basic data types programming C++ can support another built-in
data types such as short, long, signed and unsigned. The various data types, the
number of bits required to store them, and their range are shown in Table 4.1
download@
http://modul2poli.blogspot.com
E3062/4/4
Table 4.2
Data Type C++ Keyword Bits Range
Integer int 16 -32768 to 32767
Long integer long 32 -42994967296 to 4294967295
Short integer short 8 -128 to 127
Unsigned integer unsigned 16 0 to 65535
character char 8 0 to 255
Floating point float 32 Approximately 6 digits of
precision
Double floating double 64 Approximately 12 digits of
point precision
This declaration means that the compiler was asked to hold integer
data name bilangan, nom1 and nom 2. Integer data types is classified as sign
(+) and unsigned (-).For all the computer system, data integer is refer to 0
and 1 bits. There are three types of integer short int, int, long int. The
comparison of this integer range in value.
download@
http://modul2poli.blogspot.com
E3062/4/5
This declaration means that variable lagi and huruf will given a
memory storage for character data type. Character variable store printed data
or un printed data in character set of computer includes;
• small and capital letters
• decimal digits (0-9)
• special character
1. float
2. double
3. long double
download@
http://modul2poli.blogspot.com
E3062/4/6
The comparison of the three types are range value and digits.
Constant are values that do not change during program execution. They can be of
type integer, character or floating point. To declare a constant, use the keyword const
as in the example
download@
http://modul2poli.blogspot.com
E3062/4/7
i. Character constants:
‘$’ ‘*’ ‘ ‘ ‘z’ ‘G’
All variables in a program must be declared prior to their use. The declaration takes
the form
Type variable_list;
Int x, y, z;
Short small_number;
Long big_number;
Unsigned positive_number;
Char ch;
Float amount, rate;
download@
http://modul2poli.blogspot.com
E3062/4/8
Variables may be initialized by assigning constants to them either at the time of their
declaration or at a later time.
int m, n=10;
float rate, total = 0.0;
char response= ’n’;
char color{6}=’green’;
download@
http://modul2poli.blogspot.com
E3062/4/9
Activity 4A
download@
http://modul2poli.blogspot.com
E3062/4/10
Feedback To Activity 4A
4.1.
a. Data types: integer Keywords : int
b. Data types : character Keywords: char
c. Data types : double Keywords : double
d. Data types : floating point Keywords ; float
4.2.
a. constant : const Int hourly_rate
b. variable: Int a, b, c
download@
http://modul2poli.blogspot.com
E3062/4/11
INPUT 4
C++ is very rich in built-in operators. Operators trigger some computations when
applied to operands in an expression. There are several general classes of operators.
But for now, we will present just the arithmetic, relational, logical and assignment
operators.
Table 4.3
Operator Action
- Subtraction
+ Addition
* Multiplication
/ division
% Modulus division
- Decrement
++ increment
download@
http://modul2poli.blogspot.com
E3062/4/12
Examples of operation involving integer variables. With a=7 and b=2, the
expression on the values on the right.
Table 4.4
Expression Value
a–b 5
a+b 9
a*b 14
a/b 3
a%b 1
a-- 6
a-- 3
or - . Example:
a = -20 // assigns ‘a’ a negative 20
b = +20 // assigns ‘b’ a positive 20 ( + sign normally not
needed)
Unary operators operate on single value. Thus the size of operator is a unary
operator. So are the decrement and increment operator.
There are six relational operators and three logical operators in C++ are
shown in Table 4.5 and Table 4.6
download@
http://modul2poli.blogspot.com
E3062/4/13
Table 4.5
Operator Meaning
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal
!= Not equal
There are three logical operators in C++ are shown in Table 4.6
Table 4.6
Operator Meaning
&& AND
|| OR
! NOT
The result of the logical operations on a and b are summarized as Table 4.7:
Table 4.7
a||b a&&b !a
a b either a or b Both a and b Produces the
must be true Must be true opposite relation
0 0 0 0 1
0 1 1 0 1
1 0 1 0 0
1 1 1 1 0
download@
http://modul2poli.blogspot.com
E3062/4/14
*Hierarchy of Operators
The hierarchy of operator precedence form highest to lowest is summarized
below:
Table 4.8
Operator Category Operator
Unary - -- ++
Arithmetic multiply, devide, remainder + / %
Arithmetic add and subtract + -
Relational operators < <= > >=
Equality operators == !=
Logical AND &&
Logical OR ||
Gross_pay – deductions
(basic_pay + hours * rate) – (socso + premium + loan)
(b * b – 4 * a * c) > 0
(gender = = ‘m’) && (age > 20)
(gender = = ‘m’ || gender = = ‘f’ ) && age >= 21
download@
http://modul2poli.blogspot.com
E3062/4/15
Activity 4B
download@
http://modul2poli.blogspot.com
E3062/4/16
Feedback To Activity 4B
4.3.
a. arithmetic operators : - , + , ++ , *
b. logical operators : && , !
c. Relational operators : <= , != , = =
4.4.
( a + b * 2 ) – (x + y + z) – arithmetic
(sex = = ‘m’ || sex = = ‘f’ ) && age >= 21
download@
http://modul2poli.blogspot.com
E3062/4/17
KEY FACTS
download@
http://modul2poli.blogspot.com
E3062/4/18
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 14
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 4-1
Question 4-2
Using the statement that you write in statement for question no.1, write the complete
program to calculate and printed multiplication of the three integers.
download@
http://modul2poli.blogspot.com
E3062/4/19
Feedback To Self-Assessment
Have you tried the questions????? If “YES”, check your answers now.
Question 4-1
a. // Program to determine three integer multiplication
or
/* Program to determine three integers multiplication*/
b. int x, y, z, hasil;
c. cout << “Enter three integers number:”;
d. cin >> x >> y >> z;
or
cin >> x;
cin >> y;
cin >> z;
e. hasil = x * y * z;
f. cout << “Multipication is :” << hasil;
Question 4-2
/* Program to determine three integer multiplication*/
# include <iostream.h>
int x, y, z, hasil;
void main( )
CONGRATULATIONS
{ !!!!…..May success be
cout << “Enter 3 integer number:”; with you always….
cin >> x >> y >> z;
hasil = x * y * z;
cout << “Multipication is :” << hasil;
}
download@
http://modul2poli.blogspot.com
E3062/5/1
UNIT 5
OBJECTIVES
General Objective : To Understand the logical structure types and basic instruction
refers to the order of execution of instructions in program.
download@
http://modul2poli.blogspot.com
E3062/5/2
INPUT
C++ has a set of rich and powerful control structures (statements) that makes it a
popular language. Control structure generally fall into four categories but in this unit
we have to know only three of them, which are:
i. sequence structure
ii. selection structure
iii. repetition or iteration structure
download@
http://modul2poli.blogspot.com
E3062/5/3
The sequence control structure is the simplest of all the structures. The
program instructions are executed one by one, starting from the first
instruction and ending in the last instruction as in the program segment.
Example :
x = 5 (S1)
y = 10 (S2)
Total = x * y (S3)
cout<< “ \n ” << “Total =” <<Total<< “\n”; (S4)
Entry
Exit
S1 S2 S3 S4
a. if Statement
If (expression) statement
download@
http://modul2poli.blogspot.com
E3062/5/4
x >10 No
Yes
Print x
Example 5.1:
If ( gred == ‘A’ )
cout<< “\n PASS ” ;
Example 5.2:
#include<iostream.h>
main()
{
int iVar1;
int iVar2;
iVar1 =20;
iVar2 =10;
download@
http://modul2poli.blogspot.com
E3062/5/5
20 is greater then 5
b. if – else statement
x >10? No
Yes Increment
x by 1
Print x
download@
http://modul2poli.blogspot.com
E3062/5/6
Example 5.3:
if ( gred == ‘ E ’ )
cout<< “\n FAIL ” ;
else
cout<< “\n PASS ” ;
Example 5.4:
#include<iostream.h>
main()
{
int iNum;
download@
http://modul2poli.blogspot.com
E3062/5/7
if (expression_1)
if (expression_2)
if (expression_3)
statement_1;
else
statement_2;
else
statement_3;
else
statement_4;
c. Switch statement
Switch (expression)
{
Case constant_1;
Statement sequence;
break;
case constant_2;
statement sequence;
break;
.
.
default :
statement sequence;
}
Expression
x=?
download@
http://modul2poli.blogspot.com
E3062/5/9
Example 5.6
#include<iostream.h>
main()
{
char cGrade;
switch (cGrade)
{
case ‘A’
cout << “Minimun marks is 80” <<endl;
break;
case ‘B’:
cout << “Minimun marks is 60” <<endl;
break;
case ‘C’
cout << “Minimun marks is 40” << endl;
break;
case ‘D’:
cout << “Minimun marks is 25” << endl;
break;
default :
cout<< “Marks between 0-24” <<endl;
}
In the example above, A,B,C and D are the possible values that can be
assigned to the variable cGrade.
In each case of the constants, A to D, a statement or sequence of statements
would be executed.
download@
http://modul2poli.blogspot.com
E3062/5/10
You would have noticed that every line has statement under it called “break”.
The break is the only thing that stops a case statement from continuing all the
way down through each case label under it. In fact, if you do not put break in,
the program will keep going down in the case statements, into other case
labels, until it reaches the end of a break.
However, the default part is the codes that would be executed if there were
no matching case of constant’s values.
download@
http://modul2poli.blogspot.com
E3062/5/11
Activity 5A
5.2 Define the basic form of the if statement and if-else statement?
5.3 What are the ‘break’ and ‘default’ statement are needed in the switch
statement ?
download@
http://modul2poli.blogspot.com
E3062/5/12
Feedback To Activity 5A
5.1
i. if statement
ii. if else statement
iii switch statement
5.2
i. if (expression) statement;
5.3
• The ‘break’ statement is the only thing that stops a case statement
from continuing.
• The default statement is the part of the codes that would be
executed if there were no matching case of constant’s values.
download@
http://modul2poli.blogspot.com
E3062/5/13
INPUT
The while loop repeats the body of the loop as long as the loop condition
holds. The basic form of the while statement is as below:
In this loop, the expression is first evaluated. If it is true (not zero), the
statement (which can be a block is executed; if it is (zero), the statement is
bypassed
download@
http://modul2poli.blogspot.com
E3062/5/14
is Yes
x >10?
No
Print x
x=x+1
Example 5.7
Do
statement
while (expression) ;
download@
http://modul2poli.blogspot.com
E3062/5/15
This do-while loop is quite similar to the while loop except that the
expression is evaluated after the statement is executed. This means the
statement in the do-while will be executed at least once. In the while
statement, the statement will not be executed if the expression is false.
x=x+1
Print x
No is
x >10?
Yes
Example 5.8
In the example 5.8, the program displays the menu and then requests
a selection. If the selection is 1, 2, or 3, the menu is displayed again;
otherwise, the loop is terminated. Note that the loop is repeatedly executed so
long as the selection is 1,2 or 3.
The for loop repeats the body of the loop as long as the loop condition holds.
The basic form of the for loop as below:
• The initialization part typically refers to the initial value (if any) given
to a loop variable or counter. But it can also include the initialization
of any other variable. This initialization part is carried out just once at
the beginning of the loop.
• The expression part determines whether the loop execution should be
continued. If the expression is zero (false), the for loop is terminated,
if it is not zero (true), the for statement is executed.
• The incrementation part typically increments (or decrements) the loop
counter variable. This is done after the for statement is executed. This
part, like the initialization part, can also include the incrementation of
other variables.
download@
http://modul2poli.blogspot.com
E3062/5/17
Example 5.9
#include<iostream.h>
main()
{
int iNum;
The example 5.9, given is a very short program and it is easy for us to
understand the for loop.
First, an integer variable is declared. Then, in the initialization part, the
variable, iNum is set to 1. For the condition checking, iNum is checked to see
whether it is equal to or less than 10. In each cycle of the loop, iNum is
incremented by 1. Once iNum reaches 10, the loop exits. We can see that the
program calculates the square of the first ten natural numbers.
Program output:
1 4 9 16 25 36 49 64 81 100
To run a C++ program, we must first create it, then compile it, then link it with other
modules ( or other compiled programs), and finally run it. You can create a C++
program using the C++ editor. The editor is like a word processor that allows you to
type, edit and save your program. The program you create is called the source
module.
download@
http://modul2poli.blogspot.com
E3062/5/18
After you have created the source program, you compiled it using the C++ compiler.
The compiler translates your C++ instructions into a machine-readable form. The
compiled program is called the object module.
Besides compiling the source module into an object module, the compiler also
generates information necessary for linker. The linker links the object module
generated by the compiler and any other object modules, your program may request
into a final executable module.
Editor Edit
Source module
Compiler Compile
Object module
Run
Result/Output
download@
http://modul2poli.blogspot.com
E3062/5/19
Feedback To Activity 5B
5.2 State the basic form of the for loop and write the part of program that uses a
for statement?
download@
http://modul2poli.blogspot.com
E3062/5/20
Feedback To Activity 5B
5.1
i. while loop
ii. do-while loop
iii for loop
5.2
for loop form
5.3
• The compiler translates your C++ instructions into a machine-
readable form.
5.4
• The linker links the object module generated by the compiler and
any other object modules.
download@
http://modul2poli.blogspot.com
E3062/5/21
KEY FACTS
3. To run a C++ program, we must first create it, then compile it, then link it
with other modules and finally run it.
download@
http://modul2poli.blogspot.com
E3062/5/22
4.
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 5-1
Write the program using selection and repetition structure (if, if else and for) to solve
following problem.
Enter 30 integer values in [1,200]. Find how many of these values fall in the range
1-50, 51-100, 101-150 and 151-200 ?
Question 5-2
Given integer variables x ,y and a character variable ch. Input values for x,y and ch 9
the value for ch must be ‘a’, ‘m’, ‘s’, ‘d’ or ‘r’.
Compute and output
x + y if ch = ‘a’
x *y if ch = ‘m’
x - y if ch = ‘s’
x / y if ch = ‘d’
x % y if ch = ‘r’
download@
http://modul2poli.blogspot.com
E3062/5/23
Feedback To Self-Assessment
** The feedback for this Self-assessment test is based on the student’s creativity as
long as the output meets the criteria required by the question.
download@
http://modul2poli.blogspot.com
E3062/6/1
UNIT 6
OBJECTIVES
General Objective : To Understand the function of preprocessor directive and header file.
download@
http://modul2poli.blogspot.com
E3062/6/2
INPUT
Preprocessor directive are orders that we include within the code of our programs that
are not instructions for the program itself but for the preprocessor. The preprocessor is
executed automatically by the compiler when we compile a program in C++ and is the one in
charge to make first verification and digestions of the program code.
Header files are basically pre-written function that help programmer to write new
code faster. Since header files are all tested and free from bugs, hence the new programs
written can also be said as less error prone programs.
download@
http://modul2poli.blogspot.com
E3062/6/3
Definition:
a. Preprocessor
Preprocessor is the directive to enables the program to use certain function
contained in the external file such as iostream.h.
b. File Header
Header files contain specific functions that we can use to accomplish the
programming tasks. Example of header files are ctype.h and math.h.
Before we can use any of the functions contained in a header file, we must include the
header file in the program. We can do this by using the #include and # define preprocessor
directive placed before the function main ( ).
6.1.1 #include
When preprocessor find an #include directive it replaces it by the whole
content of specified file. There are two ways to specify a file to be included as in
example below:
# include “ file.h ”
or
# include <file.h>
The difference between both expressions in the example above, is in the directories in
which the compiler is going to look for the file. In the first case in that the file is
specified between quotes the file is looked for from the same directory in which the
file that includes the directive is, and only in case that is not there the compiler
download@
http://modul2poli.blogspot.com
E3062/6/4
looks for in the default directories where it is configured to look for the standard
header files.
In case that the file name is included enclosed between < > the file is directly
looked for in the default directories where the compiler is configured to look for
standard header files.
6.1.2 # define
Its function is to define a macro called name that whatever it is found in some
point of the code is replaced by value . For example:
download@
http://modul2poli.blogspot.com
E3062/6/5
1: #include <iostream>
5:
6: void main ( )
7: {
8: int x=5, y:
9: y = getmax (x,8);
10: cout<<y;
11: }
And the output of program will be the bigger value, in this case:
8
download@
http://modul2poli.blogspot.com
E3062/6/6
Activity 6A
6.3 The #define functions to define a macro called name where at some point of
the code is replaced by___________________
a. address
b. Value
c. String
d. Type
6.4 There are two ways to specify a file to be included, which are
___________ or ________________.
download@
http://modul2poli.blogspot.com
E3062/6/7
Feedback To Activity 6A
6.2 C. #define
6.3 B. Value
download@
http://modul2poli.blogspot.com
E3062/6/8
INPUT
Header files contain numerous frequently-used functions that programmers can use
without having to write codes for them. In addition, programmers can also write their own
declarations and functions and store them in header files which they can include in any C++
program. We will discuss the functions contained in a standard header files.
6.2.1 stdio.h
This header file is used for declaring standard I/O streams. Here are some
functions from this header file.
The function printf ( ) sends formatted output to the screen. It has the form
For example, to display the phrase “Learning C++ is Fun” to the screen, just type
The function scanf() reads formatted input from the keyboard. It has the form
int scanf(const char *format[ , …..])
download@
http://modul2poli.blogspot.com
E3062/6/9
Special conversion characters can be used to describe the data you want to print in
printf function or input in scanf function as shown in table 6.1.
For printf the statement
printf(“I am %d years old and I make $%f a month”, 18, 2200.50);
6.2.2 ctype.h
Table 6.2 gives a partial list of function contained in the character type
header file. These functions come in handy when you want to
determine whether a character data is alphanumeric, alphabetic, digital,
uppercase, lowercase and so on. They are often used inside an if or
while statement.
download@
http://modul2poli.blogspot.com
E3062/6/10
download@
http://modul2poli.blogspot.com
E3062/6/11
6.2.3 math.h
This header file contains several mathematical functions. Table 6.3 shows
functions contained in this file. The argument (d) for each of these functions is
of type double.
download@
http://modul2poli.blogspot.com
E3062/6/12
Program 6.2.3
download@
http://modul2poli.blogspot.com
E3062/6/13
6.2.4 conio.h
This header file has several screen handling functions suchas the function
gotoxy( ) which position the cursor in the text window. It has the form
Program 6.2.4
6.2.5 iostream.h
The name iostream.h stand for input / output stream header . The basic input /
output ( I / O ) function contained in this header file are cin for console input
( keyboard) and Cout for console output ( the screen). The function cin is used
with redirection operator >> while the function cout is used with redirection
operator <<.
download@
http://modul2poli.blogspot.com
E3062/6/14
Activity 6B
download@
http://modul2poli.blogspot.com
E3062/6/15
Feedback To Activity 6B
1
a. sqrt( ) – math.h
b. sin( ) - math.h
c. ceil( ) - math.h
d. isgraph( ) – ctype.h
e. gets( ) – stdio.h
f. scanf( ) – stdio.h
g. islower( ) – ctype.h
h. log10( ) – math.h
i. atof( ) - math.h
j. isspace( ) – ctype.h
download@
http://modul2poli.blogspot.com
E3062/6/16
KEY FACTS
1. Preprocessor is the directive to enables the program to use certain function contained
in the external file that we include within the code of our programs that are not
instructions for the program but for the preprocessor
2. Header files contain specific functions that we can use to accomplish the
programming tasks which are basically pre-written function that help programmer to
write new code faster.
download@
http://modul2poli.blogspot.com
E3062/6/17
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2 given
on the next page. If you face any problems, discuss it with your lecturer. Good luck.
Question 6-1
Write the outputs for the following for the following functions
#include <math.h>
#include <iostream.h>
main ()
{
double d,d1,d2;
d=100.0; d1=100.5; d2=2.0;
cout<<”\nSquare root of “<<d<<” = “<<sqrt(d);
cout<<”\nLog (to base 10) “<<d<<” = “<<log10(d);
cout<<”\n“<<d<<” raised to the power of “<<d2;
cout<<” = “<<pow(d,d2);
return 0;
}
Question 6-2
Write program segments to output the following:
a. The square, square root, natural logarithm of the number 20.
b. The remainder of 11 divided by 6
download@
http://modul2poli.blogspot.com
E3062/6/18
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 6.1
Question 6.2
#include <math.h>
#include <iostream.h>
main ()
{ CONGRATULATIONS!!!
cout<<”\nThe square of 20 is “<<pow(20,2); !…..May success be with
you always….
cout<<”\nThe square root of 20 is “<<sqrt(20,0);
cout<<”\nThe natural logarithm of 20 is “<<log(20,0);
return 0;
}
download@
http://modul2poli.blogspot.com
E3062/7/1
Writing C++ Program
UNIT 7
OBJECTIVES
download@
http://modul2poli.blogspot.com
E3062/7/2
Writing C++ Program
INPUT
7.0 Introduction
download@
http://modul2poli.blogspot.com
E3062/7/3
Writing C++ Program
7.1.2 Grade
/* This program accepts the mark of 5 subjects, calculates and outputs the
average and its appropriate grade */
#include<iostream.h>
void main( )
{
float a,b,c,d,e,f ;
cout<<f<<endl;
if((f>=0) && (f<50))
{
cout<< “Grade is F”;
}
else if ((f>=50) && (f<62))
download@
http://modul2poli.blogspot.com
E3062/7/4
Writing C++ Program
{
cout<< “Grade is D-”;
}
else if ((f>=62) && (f<66))
{
cout<< “Grade is D”;
}
else if ((f>=66) && (f<69))
{
cout<< “Grade is D+”;
}
else if ((f>=69) && (f<72))
{
}
else if ((f>=86) && (f<89))
{
cout<< “Grade is B+”;
}
else if ((f>=89) && (f<92))
{
download@
http://modul2poli.blogspot.com
E3062/7/6
Writing C++ Program
Activity 7A
Positive Negetive
/* This program evaluates the number keyed in is either a positive or negetive
value */
#include <iostream.h>
void main ( )
{
int n;
cout<< “Enter Value:”;
cin>>n
if((n>0) && (n!=0))
cout<<n<< “ n is a positive value”;
else
cout<<n<< “ n is a negetive value”;
}
download@
http://modul2poli.blogspot.com
E3062/7/7
Writing C++ Program
Feedback To Activity 7A
7.1.
download@
http://modul2poli.blogspot.com
E3062/7/8
Writing C++ Program
INPUT
/* This program prompt 3 students details and list them accordingly( name,
id, and major) */
#include<iostream.h>
#include<string.h>
int i;
class student
{
private : string name;
int idno;
string major;
public : void insert( )
{
cout<< “\nEnter details of student:”<<endl;
cin>>name>>idno>>major;
}
void display ( )
{
cout<< “\n”<<name<< “ ” <<idno<< “ ”<<major<<endl;
}
}
void main ()
download@
http://modul2poli.blogspot.com
E3062/7/9
Writing C++ Program
{
student s[ 4 ];
for (i=1;i<4;i++)
s[ i ].insert();
cout<< “\n\nList of students”<<endl;
for( i=1;i<4; i++)
s[ i ] .display();
}
List of students
download@
http://modul2poli.blogspot.com
E3062/7/10
Writing C++ Program
cout<<a[i] << “ ”;
cout<< “ \n”;
return 0;
}
OROGINAL ARRAY
41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95
42 27 36 91 4 2 53 92
SORTED ARRAY
0 2 4 5 24 27 27 34 36 41 42 45 53 58 61 62 64
67 69 78 81 91 91 92 95
download@
http://modul2poli.blogspot.com
E3062/7/12
Writing C++ Program
Activity 7B
7.2. Write a program that checks whether the character you type is a vowel or not
with the following output
download@
http://modul2poli.blogspot.com
E3062/7/13
Writing C++ Program
Feedback To Activity 7B
7.2.
/* This program checks whether the character typed is vowel or not */
#include<iostream.h>
using namespace std;
void main ()
{
char c;
cout<< “ Enter a character:”;
cin>>c;
switch (c)
{
case ‘a’:cout<< “It is a vowel character”;
break;
case ‘e’:cout<< “It is a vowel character”;
break;
case ‘i’:cout<< “It is a vowel character”;
break;
case ‘o’:cout<< “It is a vowel character”;
break;
case ‘u’:cout<< “It is a vowel character”;
break;
default:cout<< “It is a non – vowel character”;
}
}
download@
http://modul2poli.blogspot.com
E3062/7/14
Writing C++ Program
1.
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 2
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 7-1
Write a program that finds the square of the number entered with the following
format output:
Question 7-2
Write a program that accepts three values and selects the greatest number with the
following format output:
download@
http://modul2poli.blogspot.com
E3062/7/15
Writing C++ Program
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 7.1
/* This program finds the square of the number entered*/
#include<iostream.h>
using namespace std;
void main( )
{
int a;
cout<< “Enter a value:”;
cin>>a;
cout<<endl<< “The square of ” <<a<< “ is”<<a*a;
}
Question 7.2
/* This program accepts three values and selects the greatest
number */
#include<iostream.h>
using namespace std;
void main ( )
{
int a,b,c;
download@
http://modul2poli.blogspot.com
E3062/7/16
Writing C++ Program
CONGRATULATIONS!!!!…..
May success be with you
always….
download@
http://modul2poli.blogspot.com
B3062/8/1
UNIT 8
OBJECTIVES
download@
http://modul2poli.blogspot.com
B3062/8/2
INPUT
To develop program in Visual Basic is quiet easy because the tools in programming
such as editor and translator is integrated in one Visual Basic Program known as
Integrated Development Environment (IDE). Basically Visual Basic is an Object
Oriented Program
download@
http://modul2poli.blogspot.com
B3062/8/3
The Visual Basic environment is where you create and plan your interface. This is
where you use the Visual Basic element for program development at design time. This
includes the Menu bar, Toolbar, Toolbox, Form Window, Property Window, Project
Window as shown in Figure 8.0.
download@
http://modul2poli.blogspot.com
B3062/8/4
Properties windows
Form
Tool box window
Figure 8.1 : Visual Basic Enviroment. (Ref. Microsoft Visual Basic 6.0 Program)
a. Title Bar
The Title Bar display the project name (Project1). The default name is
Project1. Subsequent projects are named Projects, Project3, etc.
download@
http://modul2poli.blogspot.com
B3062/8/5
b. Menu Bar
The Menu Bar has the usual menu items such as File, Edit, View, Project,
Run and Tools. You can click on a menu item to view the list of command
available for that menu item.
c. Tool Bar
The Tool Bar contains several group of icons, each of which represents a
command. They denote the most frequently used operation or commands.
They acts as short cuts to the commands. You can also select the from the
menu bar.
Figure 8.2 : Tool Bar (Ref. Microsoft Visual Basic 6.0 Program)
d. Toolbox Window
download@
http://modul2poli.blogspot.com
B3062/8/6
Figure 8.3 :Toolbox Control Object (Ref. Microsoft Visual Basic 6.0 Program)
e. Form Window
The window at the center of the main screen is the Form Window. This is
where you design your application’s interface. When you begin a project,
Visual Basic gives you a new form with default name Form1. You can
change it to a more meaningful name when you save the form.
download@
http://modul2poli.blogspot.com
B3062/8/7
Figure 8.4 : Form Window (Ref. Microsoft Visual Basic 6.0 Program)
f. Project windows
The Project Explorer window stores information about the current project.
By default, a project is given the name Project1. You can assign a more
meaningful name when you save the project.
The window also displays the various forms and windows modules (if there
are any) for the current project.
Figure 8.5: Project window (Ref. Microsoft Visual Basic 6.0 Program)
g. Properties Window
The properties window is where you set the properties for the different
window objects in your project. Each object has different set of properties.
download@
http://modul2poli.blogspot.com
B3062/8/8
You can set properties according to your needs. You will see more on
Setting properties.
Figure 8.6 : Properties Window (Ref. Microsoft Visual Basic 6.0 Program)
h. Form Layout
This Form layout window allows you to position the form layaout window.
This determines the position of the desktop when the project begins
execution.
i. Menu Editor
download@
http://modul2poli.blogspot.com
B3062/8/9
Projects in Visual Basic are labeled Project1, Project2 and so on. You can however
rename them to more appropriate names. Similarly, the forms in a project are
labeled Form1, Form2, etc. You can also rename them as necessary.
A Visual Basic application takes the form of a project. A project may consist of one
or more forms, each of which may have several controls (command buttons, text
boxes, labels, etc) as shown below.
Project
download@
http://modul2poli.blogspot.com
B3062/8/10
Activity 8A
8.2 The Visual Basic Environment consists of several elements. List five elements
how to develop the project?
i.______________________ iv.________________________
ii.______________________ v._________________________
iii.______________________
i. ___________________________
ii. ___________________________
iii. __________________________
iv. ___________________________
v. ___________________________
download@
http://modul2poli.blogspot.com
B3062/8/11
Feedback To Activity 8A
download@
http://modul2poli.blogspot.com
B3062/8/12
INPUT
by clicking on the plus and the minus signs. You can use the Project Window to
activate any of files that you want.
The Properties window is where you set the properties for the objects in your
project. Each object (form, text box, command button, etc) has its own properties.
The property names are shown on the left while the selection are shown on the
right. You use this window to set the object’s properties such as its caption,
appearance, and colour. Some of form properties are shown below.
download@
http://modul2poli.blogspot.com
B3062/8/13
i. Text Box
The Text box control is only used when the user needs to type in
input. To create a text box, firstly you have to double click on
Text Box control (in the Tool box) and drag the text box to
position and resize it (as shown in Figure 8.8). Some of the text
box properties that are normally changed are Name, Text,
Alignment and Multiline.
Name - Name used in code to identify the textbox
object.
Text -Returns or sets the text contained in the control
Alignment -Returns or sets the alignment(0-left, 1-right,
2-center) of the object.
Multiline - Returns or sets a value that determines whether
a control can accept multiple lines of text.
download@
http://modul2poli.blogspot.com
B3062/8/14
ii. Labels
A label control displays text as caption. The difference between
a label and a text box control is that a label displays text as
Caption , where a text box control displays text as Text. Double
click on the label control in the toolbox, click the label and drag
to the position and resize it (as shown in Figure 8.9).Some label
properties that are normally changed are Name, Caption,
Alignment, and Multi line.
Name - It is the name used in code to identify the label
object.
Caption - Returns or sets the text displayed in the label
control.
download@
http://modul2poli.blogspot.com
B3062/8/15
download@
http://modul2poli.blogspot.com
B3062/8/16
download@
http://modul2poli.blogspot.com
B3062/8/17
v. Checkbox
Checkbox allows the user to select or deselect an option. The
process of setting or creating a check box is the same as creating
an option button. Click on check box control form and use the
crosshair pointer, place and draw the check box.
The setting option button property in the properties window:
Name – It is the name that used to reference the check box
control in coding.
Caption - It displays the text you want to appear next to the
check box control.
Value - It determines whether the check box is checked or
unchecked. The Value property of a check box is set
to 0 if unchecked, 1 if checked and 2 if disabled.
download@
http://modul2poli.blogspot.com
B3062/8/18
After you have finished with your current session, you can Save your current project
by clicking on the Save icon on the standard tool bar, or you can click File menu and
select Save Project.
download@
http://modul2poli.blogspot.com
B3062/8/19
Ì
To run the program in Visual Basic is by clicking on the Start button ‘ ’on the
standard tool bar or click on Run in the Menu bar and then select Start or
press the F5 function key. When you run your program you will get the result
of your project.
download@
http://modul2poli.blogspot.com
B3062/8/20
Activity 8B
8.4 Why do you use the user interface in the Visual Basic ?
8.7 In Visual Basic project ___________________ control acts as a control for the
user to click on it.
8.8 Name some properties to change the label and commandbutton control objects?
download@
http://modul2poli.blogspot.com
B3062/8/21
Feedback To Activity 8B
8.5 Project 1
8.7 Commandbutton
download@
http://modul2poli.blogspot.com
B3062/8/22
KEY FACTS
1. Visual Basic programs display a windows style screen (called a form) with boxes
into which users type information and buttons that they click to initiate actions.
The boxes and buttons are referred to as controls. Forms and controls are called
objects.
2. Visual Basic programs consist of three parts – interface, values of properties and
code.
3. The user interface is use to interact with the system such as for entering data,
controlling the program and viewing results
download@
http://modul2poli.blogspot.com
B3062/8/23
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section and
check your answers with those given in the Feedback on Self-Assessment 2 given on
the next page. If you face any problems, discuss it with your lecturer. Good luck.
In self-assessment Questions 8.1 to 8.3, create the interface shown in the figure.
(These assessment give you practice creating objects and assigning properties. The
interface do not necessarily correspond to actual programs.)
Question 8-2
download@
http://modul2poli.blogspot.com
B3062/8/24
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 8-1
Create a new project. Change the form’s caption to “Dynamic Duo”. Place two
command buttons on the form. Enter as the caption of the first “&Batman” and of the
second “&Robin”. Increase the font size for both command buttons to 14.
Question 8-2
Create a new project. Change the form’s caption to “Fill in the Blank”. Place a label, a
text box, and another label on the form at appropriate locations. Change the caption of
the first label to “Toto, I don’t think we’re in” and of the second label to “A Quote
from the Wizard of Oz” . Delete “Text1” from the Text property of the text box.
Resize and position the labels as needed.
Question 8-3
Create a new project. Change the form’s caption to “Picture Box”. Place a picture box
and a label on the form. Change the label’s Caption property to the sentence shown.
Change the label’s BackColor property to white, and its Font Size property to 14.
Access the picture box’s Picture property and select the picture file PICBOX.BMP
from the Pictures foleder on the CD accompanying this textbook.
download@
http://modul2poli.blogspot.com
B3062/9/1
UNIT 9
OBJECTIVES
download@
http://modul2poli.blogspot.com
B3062/9/2
INPUT
9.0 Introduction
When a Visual Basic program is run, a form and its control appear on the
screen. Normally, nothing happen until the user takes an action, such as
clicking a control or pressing the Tab key. Such an action is called an events.
The three steps to creating a Visual Basic program are as follows:
1. Create the interface, that is, generate, position and size the objects.
2. Set properties, that is, set relevant properties for the objects.
3. Write the code that executes when the events occur.
download@
http://modul2poli.blogspot.com
B3062/9/3
Code consists of statements that carry out tasks. Visual Basic has a repertoire
of over 200 statements and we will use many of them in this text. In this unit,
we limit ourselves to statements that change properties of objects while a
program is running.
Properties of an object are change in code with statements of the form :
ObjectName.property = setting
Where;
ObjectName – the name of the form or a control
Property - one of the properties of the object
Setting - a valid setting for that object.
i. The statement
txtBox.Font.Size = 12
• sets the size of the characters in the text box named txtBox to 12.
download@
http://modul2poli.blogspot.com
B3062/9/4
txtBox.Font.Bold = True
txtBox.Text= “ “
• clear contents of the text box; that invokes the blank setting
Most events are associated with objects. The event clicking cmdButton is
different from the event clicking picBox. These two events are specified
cmdButton_Click and picBox_Click. The statements to be executed when an
event occurs are written in a block of code called an event procedure. The
structure of an event procedure is:
The word Sub in the first line signals the beginning of the event procedure,
and the first line identifies the object and the event occurring to that object.
The last line signals the termination of the event procedure. The statements to
be executed appear between these two lines. The word Private indicates that
the event procedure cannot be invoke by an event from another form. This
will no concern us until much later in the text. The word Sub is an
abbreviation of Subprogram.
download@
http://modul2poli.blogspot.com
B3062/9/5
• clears the contents of the text box when the command button is
clicked.
download@
http://modul2poli.blogspot.com
B3062/9/6
3. Click on the down –arrow button to the right of the procedure box. The
drop-down menu that appears contains a list of all possible event
procedures associated with text boxes. ( Figure 9.3)
download@
http://modul2poli.blogspot.com
B3062/9/7
4. Scroll down the list of event procedures and click on LostFocus. The
lines
Private Sub txtPhrase_LostFocus()
End Sub
Appear in the code window with blinking cursor poised at the beginning
of the blank line.
download@
http://modul2poli.blogspot.com
B3062/9/8
6. Let’s create another event procedure for the text box. Click on the down-
arrow button to the right of the Procedure box, scroll up the list of event
procedures and click on GotFocus. Then type the lines
TxtPhrase.Font.Size = 8
TxtPhrase.Font.Bold = False
between the existing two lines. (see Figure 9.6)
download@
http://modul2poli.blogspot.com
B3062/9/9
download@
http://modul2poli.blogspot.com
B3062/9/10
download@
http://modul2poli.blogspot.com
B3062/9/11
12. Press the tab key. The contents of the text box will be enlarged as in
Figure 9.10. when tab was pressed, the text box lost the focus; that is the
event LostFocus happened to txtPhrase. Thus, the event procedure
txtPhrase_LostFocus was called, and the code inside the procedure is
executed.
13. Click on the command button. This calls the event procedure
cmBold_Click, which converts the text to boldface. See Figure 9.11
download@
http://modul2poli.blogspot.com
B3062/9/12
14. Click on the text box or press the Tab key to move the cursor to the text
box. This calls the event procedure txtPhrase_GotFocus, which restore
the text to its original state.
15. You can repeat steps 11 through 14 as many times as you like. When you
have finished, end the program by pressing Alt+F4, clicking the End icon
on the toolbar, or clicking the Close button (X) on the form.
download@
http://modul2poli.blogspot.com
B3062/9/13
Activity 9A
Describe the contents of the text box after the command buttonis clicked.
download@
http://modul2poli.blogspot.com
B3062/9/14
download@
http://modul2poli.blogspot.com
B3062/9/15
Feedback To Activity 9A
9.4 The name of the control has been given but not the property being assigned.
frmHi = “Hello” needs to be changed to frmHi.Caption = “Hello”.
9.5 Text boxes does not have a caption property. Information to be displayed in
a text box must be assigned to the text property.
9.6 Only 0 and 1 are valid values for the borderStyle property of a label.
download@
http://modul2poli.blogspot.com
B3062/9/16
INPUT
9.3.1 Declaration
Examples:
Dim ProductCode As String
Dim Quantity As Integer
Dim Price As Currency
Dim OrderDate As Date
download@
http://modul2poli.blogspot.com
B3062/9/17
9.3.2. Sequence
End Sub
download@
http://modul2poli.blogspot.com
B3062/9/18
9.3.3 Selection
Example:
Private Sub Form_Activate()
Dim mark As Interger
mark = InputBox (“Enter your mark:”)
If mark > 49 Then
Print “PASS”
End If
End Sub.
If you enter the value 74 for mark, the program will display the
following output:
PASS
Example:
Private Sub Command1_Click()
Dim x As Integer
Dim xsquare As Integer
Print “x”, “xsquare”
For x = 0 To 6
Xsquare = x * x
Print x, xsquare
Next x
End Sub
x xsquare
0 0
1 1
2 4
3 9
4 16
5 25
6 36
download@
http://modul2poli.blogspot.com
B3062/9/20
Activity 9B
9.11 Write the general form to declare variable and constant in a program
of below statement:
9.12 State the three types of loop structure in Visual Basic programming?
download@
http://modul2poli.blogspot.com
B3062/9/21
Feedback To Activity 9B
9.10 Dim (dimension) and Const (constant) statement are use to declare a variable
download@
http://modul2poli.blogspot.com
B3062/9/22
KEY FACTS
download@
http://modul2poli.blogspot.com
B3062/9/23
1.
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 9
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 9-1
Write a program to find the larger of two numbers input by the user?
download@
http://modul2poli.blogspot.com
B3062/9/24
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 9-1
CONGRATULATIONS!!!!….
download@
http://modul2poli.blogspot.com
E3062/10/1
Visual Basic Programming
UNIT 10
OBJECTIVES
download@
http://modul2poli.blogspot.com
E3062/10/2
Visual Basic Programming
INPUT
This unit presents a simple Visual Basic program that requires input, processing and
output. Its interface includes the main form and six controls objects – one text box,
one picture box, two labels and two command buttons. The labels are used for
labeling text box; the text box for entering data; and the picture box, for displaying
results. The two command buttons are used to control the program execution.
a. Plan the project which involves determining what we want the program to do.
b. Design the user interface which is the most visible part of the project. It is
used to interact with the system. You determine the controls that are needed
to enter data, to display results, and to control the program execution.
c. Set the properties of the controls. Determine the controls that you will need
for your project and set the properties (e,g., appearance, name, caption, color)
for each of the controls used.
d. Write the code (program) which represents the action that must be taken
when the user clicks on a command button. Clicking a command button is
called an event and the code represents a response to that event.
e. Test and debug the code. This is done to ensure that the program works
correctly.
download@
http://modul2poli.blogspot.com
E3062/10/3
Visual Basic Programming
We will use an example to illustrate the basic steps needed to do a Visual Basic
program. Let’s say we want to compute and display the grade of a student given
his/her mark. Passing mark will be between 50 to 100 and a fail between 0 to 49.
The user interface shows the visible parts of a Visual Basic program. You use the
interface to interact with the system such as for entering data, controlling the
program and displaying results.
Form
Let’s name the form compute grade and set its Appearance property to 0-Flat.
a. In the main window, select File > New Project (Ctrl+N). You will see the
title Project1 – Form1 (form) in the project title bar and the title Form1 in
the form title bar – Figure 10.1
download@
http://modul2poli.blogspot.com
E3062/10/4
Visual Basic Programming
Labels
Let’s name the label1 for entering mark as Enter your mark (0 – 100) and the
label2 for viewing the results as Your grade is.
download@
http://modul2poli.blogspot.com
E3062/10/5
Visual Basic Programming
a. Highlight the label icon A in the toolbox and double-click on it. Visual Basic
will place the label button label1 in the center of the form. Move and resize
it to a suitable size and location (as shown in Figure 10.3).
b. Click on the label and set the Appearance property to 0-Flat (the
BackColor property will automatically change to white) and the Caption
property to Enter your mark (0 – 100) as shown in Figure 10.3
To create the next label (label2) follow the steps above and set the caption to
Your grade is.
Text box
Create a text box for entering mark.
a. Highlight the text box icon ab in the toolbox and double-click on it.
Visual Basic will place a text box with the text Text1 inside.
b. Click on the text box and set the text property to empty (i.e., clear text1) as
shown in Figure 10.4 (don’t change the other settings).
download@
http://modul2poli.blogspot.com
E3062/10/6
Visual Basic Programming
Picture box
Create the picture box for displaying the results (grade).
a. Highlight the picture box icon (just above the text box icon) in the toolbox
and double-click on it. Visual Basic will place a picture box.
b. Click on the picture box and set the BackColor property to white as shown
in Figure 10.5
Command buttons
Create the command buttons and set their properties as follows:
a. Highlight the command button icon in the toolbox and double-click on it.
Visual basic will place the command button command1 at the center of the
form. Resize the button if necessary and move it to the bottom of the form (as
shown in the Figure 10.6).
b. Set the property Caption to Compute Grade as follows.
download@
http://modul2poli.blogspot.com
E3062/10/7
Visual Basic Programming
Similarly, create the second command button (command2) and give it the
name Exit. Your form should now resemble: (as shown in Figure 10.7)
download@
http://modul2poli.blogspot.com
E3062/10/8
Visual Basic Programming
a. double-click on the command button Compute grade and the code window
shown in figure 10.8 will appear:
Visual basic displays the name of the control Command1 on the top left and
the name of the event Click on the top right. Notice that the Visual Basic still
retains the name Command1 even though we have changed it to Compute
Grade.
b. Enter the code in the code window as shown. Note that Visual Basic
automatically supplies the first and the last line of the code.
download@
http://modul2poli.blogspot.com
E3062/10/9
Visual Basic Programming
Else
Picture1.Print “Pass”
End If
End Sub
Private is a reserved word used to specify that this event cannot be invoked
from another form.
End Sub, the last line in the program signals the end of the program.
Similarly, double-click on the command button Exit and enter the code as below:
There is only one command, End, in this procedure (between the first and the last
line). End simply stops the program execution. This is also an event procedure.
When the user clicks on the Exit command button, the program execution will stop.
If you double-click on the Exit button, you will see the code for both the Compute
Grade button (at the top) and the code for the Exit button (at the bottom) with a line
separating the two as shown below.
download@
http://modul2poli.blogspot.com
E3062/10/10
Visual Basic Programming
End Sub
download@
http://modul2poli.blogspot.com
E3062/10/11
Visual Basic Programming
When you run the program, you will see the screen shown in Figure 10.9
3. Click on the text box at the top and enter the mark. Then click on the Compute
Grade command button. You will see the grade displayed in the Picture box as
shown in the screen below in Figure 10.10
To stop the execution, click on the Exit command button. Visual Basic will take you
back to the code window.
download@
http://modul2poli.blogspot.com
E3062/10/12
Visual Basic Programming
Activity 10A
Question 10.1
a. Rewrite the code with fewer line
PicOutput.Print 1;
PicOutput.Print 2;
PicOutput.Print 1 + 2
b. Complete the table by filling in the value of each variable after each line is
executed.
a b c
Private Sub cmdCompute_Click ( )
a=3
b=4
c=a+b
a=c*a
PicResults.Print a - b
b=b*b
End Sub
download@
http://modul2poli.blogspot.com
E3062/10/13
Visual Basic Programming
b.
a b c
Private Sub cmdCompute_Click ( )
a=3 3
b=4 3 4
c=a+b 3 4 7
a=c*a 21 4 7
PicResults.Print a - b 21 4 7
b=b*b 21 16 7
End Sub
download@
http://modul2poli.blogspot.com
E3062/10/14
Visual Basic Programming
INPUT
download@
http://modul2poli.blogspot.com
E3062/10/15
Visual Basic Programming
Reserved words
Reserved words or keywords have special meaning in Visual Basic. You cannot use
them for any other purpose. Some of the reserved words in the above program are:
Private
Private is a reserved word in Visual Basic. It tells Visual Basic that this procedure
cannot be invoked from another form.
Sub
Stands for subprogram. The term procedure is also used to refer to a subprogram.
Command_Click ( )
Variable declaration
The statement
download@
http://modul2poli.blogspot.com
E3062/10/16
Visual Basic Programming
Comments
Comments in Visual Basic start with an apostrophe (‘). Comments can be placed
anywhere in the program. It can be placed on a separate line as in
‘Read mark input, convert it value, then store in variable mark or after a Visual Basic
command/statement as in
Assignment statement
<variables> = <expression>
The expression on the right-hand side of the equal sign (=) is evaluated and the result
is assigned to the variable on the left-hand side. The variable represents an area of
computer memory that can hold data. The statement
Mark = Va1(Text1.text)
download@
http://modul2poli.blogspot.com
E3062/10/17
Visual Basic Programming
first reads input data from text box (Text1, text), then converts it to a numerical
value, and finally stores the number in the variable mark. The function Va1 does the
conversion.
Object.Property
Text1.text
Methods
Visual Basic provides procedures known as methods for each of the controls it
provides. You can use these methods to change the control object’s properties.
To use a method associated with an object, you suffix a period (.) and the method to
the object as follows:
Object.method ( )
Picture1.Cls
download@
http://modul2poli.blogspot.com
E3062/10/18
Visual Basic Programming
Picture1 is the object and Cls (clear screen) is the method associated with that object.
This method simply clears the picture box.
Another method we have used with the picture1 object is the Print method, which is
used to send output to the picture box as in
Picture1.Print “Pass”
Decision-making
Visual Basic provides several control structures (e.g. If-end, DO-Loop) to support
the decision-making.
One of the control structures is If-Else-If. In the example program, the statement
tests if the mark entered is less than 50, and if it is, it prints Fail in the picture box,
otherwise it prints pass. (if mark is entered 50 or more)
download@
http://modul2poli.blogspot.com
E3062/10/19
Visual Basic Programming
Activity 10B
10.2 Explain what is meant by reserved words or keywords in Visual Basic. States 3
reserved words.
10.3 What is a variables. Show a general form of variable statement assignment in Visual
Basic program.
10.4 Consider the following Visual Basic program;
download@
http://modul2poli.blogspot.com
E3062/10/20
Visual Basic Programming
10.2 Reserved words or keywords have special meaning in Visual Basic that cannot
be use for any other purpose. Examples of reserved words are Sub, End, False,
True and private.
10.3 Variable is a name that is used to refer to an item of data and it represents an
area of computer memory that can hold data. General expressions of variables
statement assignment; <variables> = <expression>
10.4
(i)
(ii)
download@
http://modul2poli.blogspot.com
E3062/10/21
Visual Basic Programming
KEY FACTS
1. One of the key elements in planning a Visual Basic application is deciding what the
user sees. What data will the user entering? Will the applications have places to enter
text and places to display output.
2. The sequence of procedures executed in your program is controlled by events that
the user initiates rather than by a predetermined sequence of procedures in your
program.
download@
http://modul2poli.blogspot.com
E3062/10/22
Visual Basic Programming
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment given
on the next page. If you face any problems, discuss it with your lecturer. Good luck.
The following steps calculate the balance after 3 years when RM100 is deposited
in a savings account at 5% interest compounded annually.
2. Write an event procedure to solve the problem and display the answer in a
picture box. The program should use variables for each of the quantities.
If a car left Kota Bharu at 2 o’clock and arrived in Pulau Pinang at 7 o’clock,
what was its average speed ? The distance between Pulau Pinang from Kota
Bharu is 350 km.
download@
http://modul2poli.blogspot.com
E3062/10/23
Visual Basic Programming
Feedback To Self-Assessment 1
Have you tried the question????? If “YES”, check your answer now:
download@
http://modul2poli.blogspot.com