Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

02 IntroductionToProgramming

The document provides an introduction to programming concepts including programs, programming languages, algorithms, flowcharts, pseudocode, and basic elements of a program. It discusses the main components of a program including keywords, identifiers, input/output, arithmetic operations, logic, and control structures. The three basic control structures are sequence, selection, and repetition. Sequence structures perform a series of actions in order. Selection structures selectively execute statements based on a condition. Repetition structures repeat statements until a stop condition is met.

Uploaded by

ZHEN-HONG LEE
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

02 IntroductionToProgramming

The document provides an introduction to programming concepts including programs, programming languages, algorithms, flowcharts, pseudocode, and basic elements of a program. It discusses the main components of a program including keywords, identifiers, input/output, arithmetic operations, logic, and control structures. The three basic control structures are sequence, selection, and repetition. Sequence structures perform a series of actions in order. Selection structures selectively execute statements based on a condition. Repetition structures repeat statements until a stop condition is met.

Uploaded by

ZHEN-HONG LEE
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

INTRODUCTION

TO
PROGRAMMING
UCCD1004 Programming Concept and Practices
Programs and Programming Languages
• Program
A set of instructions directing a computer to perform a task
For example, operating system, web browser, video games,
Adobe PDF….

• Programming Language
A language used to write programs
For example, C++, Java, HTML, Javascript, Python……
Programs and Programming Languages

Types of languages
– Low-level: used for communication with computer hardware
directly.
– High-level: closer to human language
Syntax vs. Semantic

• Syntax refers to whether a statement is accepted by the


programming language or not.

• Semantic refers to the meaning of the instructions.


Debugging
Debugging, in computer programming, is an art that involves
identifying a problem, isolating the source of the problem, and
then either correcting the problem or determining a way to work
around it.

The final step of debugging is to test the correction or


workaround and make sure it works.
Algorithm
• The steps needed to solve a problem is known as algorithm. It
is a process or set of rules to be followed in calculations or
other problem-solving operations, especially by a computer.

• However to be specific, in computer science, an algorithm


usually means a small procedure that solves a recurrent
problem.
Flowchart
Lamp doesn’t work
• A flowchart is a type of diagram
that represents an algorithm,
workflow or process, showing Lamp No
plugged Plug in lamp
the steps as boxes of various in?
kinds, and their order by
connecting them with arrows. Yes

Bulb Yes
burned Replace bulb
out?

No

Repair lamp
Basic Flowcharting Symbols
Symbol Name Description

Terminator Represents the start or end of a program or


module

Process Represents any kind of processing function;


for example, a computation

Input / Output Represents an input or output operation

Decision Represents a program branch point

Connector Indicates an entry to, or exit from, a program


segment
START Terminal

Basic Flowchart Symbols Display message


“How many
hours did you
work?”

• Terminals Read Hours

– represented by rounded rectangles Display message


“How much do
– indicate a starting or ending point you get paid per
hour?”

Read Pay Rate

START
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
END
Display Gross
Pay
Terminal
END
Basic Flowchart Symbols
START

Display message
“How many
hours did you
work?”

• Input/Output Operations Read Hours

– represented by parallelograms
Display message

– indicate an input or output operation “How much do you


get paid per hour?”
Input/Output
Operation

Read Pay Rate

Display message
Multiply Hours
“How many by Pay Rate.

hours did you Read Hours Store result in


Gross Pay.
work?”
Display Gross
Pay

END
START

Basic Flowchart Symbols Display message


“How many
hours did you

• Processes
work?”

– represented by rectangles Read Hours

– indicates a process such as a mathematical Display message

computation or variable assignment “How much do you


get paid per hour?”

Read Pay Rate


Multiply Hours
by Pay Rate. Multiply Hours
Store result in Process
by Pay Rate.
Store result in
Gross Pay. Gross Pay.

Display Gross
Pay

END
Pseudocode
• Pseudocode is an informal high-level description of the operating
principle of a computer program or other algorithm.
Initialize total to zero
Initialize counter to zero
Input the first grade
while the user has not as yet entered the sentinel
add this grade into the running total
add one to the grade counter
input the next grade (possibly the sentinel)
if the counter is not equal to zero
set the average to the total divided by the counter
print the average
else
print 'no grades were entered'
What Is a Program Made Of?
Common elements in programming languages:

– Key Words – int, double, const, … … ...


– Programmer-Defined Identifiers
– Operators – maths(+, -, *, /), … … ...
– Punctuation - !, “, #, $, %, &, … … …
– Syntax
Keywords

• Aka. Reserved words

• They cannot be redefined within any program; they cannot be


used for anything other than their intended use.
For example: int, float, double, char, const, void, return
Identifiers
• It is names of things that appear in programs, such as variables,
constants, and functions. All identifiers must obey C++’s rules.

• Identifier can be made of only letters, digits and the underscore


character; no other symbols are permitted.
Illegal Identifiers
Example Program
#include <iostream>
using namespace std;

int main()
{
    int num1 = 5, num2, sum;
    num2 = 12.5;
    
    sum = num1 + num2;
    cout << "The sum is " << sum;
    system(“pause”);
    return 0; Output:
} The sum is 17
Input, Processing and Output
• Basic operations includes:-

– Input: get data from the keyboard or from some file or other


device
– Output: write and display
– Arithmetic: perform calculations
– Logic: check conditions and execute accordingly
– Iterative: repeat sequence of operations, usually with little
variation
Control Structures
In 1960s computer scientists proved there are only 3 basic control structures
(also called constructs) needed to create any program or algorithm!
■ Sequence –in sequential order.
– The simplest of control structures – start at the beginning and continue
in sequential order.
■ Selection – selectively execute statements
– Also called a branch or decision
– requires a condition to determine when to execute statements.
■ Repetition – repeat statements more than once
– Also called a loop
– needs a stop condition, i.e, the program will continue to loop until some
condition is met.
Sequence Structure
• a series of actions are performed in sequence
• The pay-calculating example was a sequence flowchart.
Decision Structure
• In the flowchart segment below, the question “is marks > 50?”
is asked. If the answer is no, then process Fail is performed. If
the answer is yes, then process Pass is performed.

NO YES
Marks >
50?

Process Process
Fail Pass
Repetition Structure
• In the flowchart segment, the question “is x < y?” is asked. If
the answer is yes, then Process A is performed. The question
“is x < y?” is asked again. Process A is repeated as long as x is
less than y. When x is no longer less than y, the repetition stops
and the structure is exited.

YES
x < y? Process A
Special Characters
Character Name Description
// Double Slash Begins a comment
# Pound Sign Begins preprocessor directive

< > Open, Close Brackets Encloses filename used in


#include directive
( ) Open, Close Parentheses Used when naming function

{ } Open, Close Braces Encloses a group of statements


" " Open, Close Quote Encloses string of characters
Marks
; Semicolon Ends a programming statement

2-23
Important Details

• C++ is case-sensitive.

• Uppercase and lowercase characters are different characters.


Eg: ‘Main’ is not the same as ‘main’.

• Every { must have a corresponding }, and vice-versa.


The cout Object
• Displays information on computer screen
• Use << to send information to cout
cout << "Hello, there!";

• Can use << to send multiple items to cout


cout << "Hello, " << "there!";

or

cout << "Hello, ";


cout << "there!";
2-25
// A simple C++ program
#include <iostream>
using namespace std;

int main()
{
    cout << "Programming is great fun!";
    cout << "Programming is " << "great fun!";
    cout << "Programming is ";
    cout << "great fun!";
    system("pause");

    return 0;
}
Starting a New Line
• To get multiple lines of output on screen
- Use endl
cout << "Hello, there!" << endl;
cout << "Programming is great fun!" << endl <<  endl;

- Use \n in an output string


cout << "Hello, there!\n";
cout << "Programming is great fun!\n\n";

2-27
The #include Directive
• Inserts the contents of another file into the program

• Is a preprocessor directive
• cout is not part of the C++ language

• Example: No ; goes
here

#include <iostream>
Formatting Output

• Can control how output displays for numeric and string data
– size
– position
– number of digits
• Requires iomanip header file
Stream Manipulators

• Used to control features of an output field


• Some affect just the next value displayed
– setw(x): Print in a field at least x spaces wide. Use more
spaces if specified field width is not big enough.
Stream Manipulators
• Some affect values until changed again
– fixed: Use decimal notation (not E-notation) for floating-
point values.
– setprecision(x):
• When used with fixed, print floating-point value using x
digits after the decimal.
• Without fixed, print floating-point value using x
significant digits.
– showpoint: Always print decimal for floating-point values.
– left, right: left-, right justification of value
If a number is too large to print using the number of digits specified
with setprecision, many systems print it in scientific notation.
Eg. 145678.99 -- 1.4568e+005

To prevent this, you use fixed which indicate that floating point output
should be printed in fixed point or decimal.
cout << fixed;

When the setprecision used together with fixed, it specifies the number
of digits to be displayed after the decimal point rather than the total
number of digits to be displayed.
cout << fixed << setprecision(2);
Eg. 321.57  321.57
showpoint indicate that a decimal point should be printed for a floating
point number even if the value being displayed has no decimal digits.

double amount = 125.0;


cout << setw(6) << amount << endl;
cout << showpoint;
cout << setw(6) << amount << endl;
cout << fixed << showpoint << setprecision(2);
cout << setw(6) << amount << endl;

125
125.000
125.00
Manipulator Examples
const float e = 2.718;
float price = 18.0; Displays
cout << setw(8) << e << endl; ^^^2.718
cout << left << setw(8) << e << endl; 2.718^^^
cout << setprecision(2);
cout << e << endl; 2.7
cout << fixed << e << endl; 2.72
cout << right << setw(6) << price; ^ 18.00

You might also like