02 IntroductionToProgramming
02 IntroductionToProgramming
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
Bulb Yes
burned Replace bulb
out?
No
Repair lamp
Basic Flowcharting Symbols
Symbol Name Description
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?”
– represented by parallelograms
Display message
Display message
Multiply Hours
“How many by Pay Rate.
END
START
• Processes
work?”
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:
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:-
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
2-23
Important Details
• C++ is case-sensitive.
or
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;
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
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.
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