Module 2 Day 1 - Sample Program and Layout of A Program
Module 2 Day 1 - Sample Program and Layout of A Program
0
COMPUTER Introduction to a
Programming Language
SCIENCE 2
Lesson Code 2.1
PREPARED BY: Sample Program and
MAUREEN D. AGRAZAMENDEZ Layout of a Program
OBJECTIVES
• Review Flowcharts
• Identify and define the basic structure of a program;
• Create a simple program following the basic structure of a program;
• write simple computer programs in C++.
• write simple input and output statements.
REVIEW: FLOWCHARTS
FLOWCHART
• Flowchart is a graphical representation of an algorithm.
• Programmers often use it as a program-planning tool
to solve a problem.
• It makes use of symbols which are connected among
them to indicate the flow of information and processing
• The process of drawing a flowchart for an algorithm is
known as “flowcharting”.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Terminal:
• The oval symbol indicates Start, Stop and
Halt in a program’s logic flow.
• A pause/halt is generally used in a program
logic under some error conditions.
• Terminal is the first and last symbols in the
flowchart.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Input/Output:
• A parallelogram denotes any function of
input/output type.
• Program instructions that take input from
input devices and display output on output
devices are indicated with parallelogram in
a flowchart.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Processing:
• A box represents arithmetic instructions.
• All arithmetic processes such as adding,
subtracting, multiplication and division are
indicated by action or process symbol.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Decision
• Diamond symbol represents a decision
point.
• Decision based operations such as yes/no
question or true/false are indicated by
diamond in flowchart.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Connectors:
• Whenever flowchart becomes complex or it
spreads over more than one page, it is useful to
use connectors to avoid any confusions.
• It is represented by a circle.
• Flow lines:
• Flow lines indicate the exact sequence in which
instructions are executed.
• Arrows represent the direction of flow of
control and relationship among different
symbols of flowchart.
RULES FOR CREATING
FLOWCHARTS
• A flowchart is a graphical representation of an algorithm.it should follow some rules while
creating a flowchart
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: The decision symbol in the flowchart cannot be associated with the arrow line.
C++
WELCOME TO C++
• C++—a powerful computer programming language that’s appropriate for technically
oriented people with little or no programming experience, and for experienced programmers
to use in building substantial information systems.
• You’re already familiar with the powerful tasks' computers perform. Using this PL, you’ll
write instructions commanding computers to perform those kinds of tasks.
WHAT IS C++?
• C++ evolved from C, which was developed by Dennis Ritchie at Bell Laboratories.
• C is available for most computers and is hardware independent.
• C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell
Laboratories.
• C++ provides a number of features that “spruce up” the C language, but more importantly, it
provides capabilities for object-oriented programming.
FIRST PROGRAM IN C++:
PRINTING A LINE OF TEXT
Create a flowchart that displays
“Hello World!” to the screen
SAMPLE PROGRAM IN C++
Comments
#include <iostream> // allows program to output data to the screen
using namespace std;
Preprocessing Directive
A library is a collection
#include <iostream> // allows program to output of predefined
data to theroutines
screenor
using namespace std; programs that a program
can use. These routines
// function main begins program execution
or programs perform
int main()
{ specific tasks in a
cout << “Hello World!"; // display messageprogram without writing
the codes that do it.
return 0; // indicate that program ended successfully
} // end function main
SAMPLE PROGRAM IN C++
statements
#include <iostream> // allows program to output These
datalines
toof the screen
codes are called a
using namespace std; statements. Statements are
fragments of a C++ program that
// function main begins program execution are executed in sequence.
int main() Statements always end with a
semicolon ( ; ). Each function
{
may contain as may statement
cout << “Hello World!"; // display messagedepending on how the program
return 0; // indicate that program ended successfully
will run.
} // end function main
Common Programming Error
SAMPLE PROGRAM IN C++
The Stream Insertion Operator
The << operator is referred to as the
#include <iostream> // allows program to output stream
datainsertion
to the screen
operator. When this
using namespace std; program executes,
the value to the operator’s right, the
// function main begins program execution right operand, is inserted in the output
int main() stream.
Notice that the operator points in the
{
direction of where the data goes. The
cout << “Hello World!"; // display messageright operand’s
return 0; // indicate that program ended successfully
characters normally print exactly as
} // end function main they appear between the double quotes
SUMMARY
• In summary, the general structure of a C+
+ program will always contain a
preprocessor directive, the main function
and program statements. The general
structure of a very simple program could
be shown as:
• You may also include comments in your
program to make it more readable and
could help other programmers understand
your program. You may use single line or
multi-line comments.