DFC 10042problem Solving & Problem Design: Chapter 4: Basic Programming Codes
DFC 10042problem Solving & Problem Design: Chapter 4: Basic Programming Codes
DFC 10042problem Solving & Problem Design: Chapter 4: Basic Programming Codes
What is a comment ?
a comment is a programming language construct used
to embed programmer-readable annotations in
the source code of a computer program.
Those annotations are potentially significant to
programmers but typically ignorable
to compilers and interpreters.
Comments are usually added with the purpose of
making the source code easier to understand. The
syntax and rules for comments vary and are usually
defined in a programming language specification.
a) comment
4
What is a comment ?
An illustration
of Java source
code with prologue
comments indicated
in red and inline
comments in green.
Program code is
in blue.
b) Preprocessor directive
5
What’s a #?
Any line that begins with # symbol is a pre-processor
directive
Processed by preprocessor before compiling
What’s pre-processor?
A utility program, which processes special instructions
are written in a C/C++ program.
Include a library or some special instructions to the
compiler about some certain terms used in the program
Different preprocessor directives (commands) perform
different tasks.
It is a message directly to the compiler.
b) Preprocessor directive
6
How to write/syntax ?
Begin with #
No semicolon (;) is expected at the end of a preprocessor
directive.
Example : #include and #define
Preprocessor
Directives
b) Preprocessor directive
7
Example:
pre-processor Meaning
directive
#include <FILE> Include a header file
#include <iostream> Tells preprocessor to include
the input/output stream
header file <iostream>
This
program
prints
“Hello,
world!”
using cout.
c) Standard Header file
12
Type of
return value int main ( )
{
Function C++ Program statements in here;
body Return 0;
}
d) Function main
15
int main(void)
{
void main(void)
return 0; { main(void)
}
{ main( )
}
{
}
}
e) Reserved Word/Keywords
16
Identifiers as Constant
Entities that appear in the program code as fixed values.
Any attempt to modify a CONSTANT will result in error.
Example of Variables:
i)int is an abbreviation for integer.
Integer store value: 3, 102, 3211, -456, etc.
Example variable : number_of_bars
Newline \n
Horizontal tab \t
h) Punctuation
24
What is statement?
A specification of an action to be taken by the computer as the program executes.
Instruct the program to perform an action
All statements end with a semicolon (;)
Statement has two parts :
Declaration
The part of the program that tells the compiler the names of
memory cells in a program
Executable statements
Program lines that are converted to machine language
instructions and executed by the computer
Examples :
return 0;
END cout<<Total;
cout<< Average;
}
Identify input, process and output from Programming Code 1
31
#include <iostream>
using namespace std;
void main( )
{
int num1, num2, num3;
cout<<“input num1, num2, num3”;
INPUT
cin>> num1, num2, num3;
#include <iostream>
Start using namespace std; INPUT =
void main() none
set Package = 699; {
Discount = 15/100 * Package; float package, discount, NewPrice;
NewPrice = Package –Discount; Package = 699.00;
Print NewPrice; Discount = 15/100 * Package;
NewPrice = Package –Discount;
End
cout>> NewPrice ; PROCESS
}
OUTPUT
Convert Pseudocode to Programming Code
33
sum = 0, i = 1;
while (i<= 6)
{ cout << "input 1 number: ";
cin>> number; PROCESS
}
DISCUSSION
37
#include <iostream>
using namespace std; Question:
void main() Based on program
{ int number, sum, i , average; code given, identify
sum = 0, i = 1; input, output and
process of the
For (i = 1; i<= 20; i++)
program
{ cout << "input weight: ";
cin>> number;
sum = sum + weight;
}
average = sum/20
cout<< " Average of 20 weight="<< average ;
}
Construct pseudo code and flowchart based on given programming
code.
38
i= 1, sum=0
Input weight
Sum+= weight
i ++
Average=Sum/20
i <= 20
false
true Print Average
end
Instruction: Develope pseudo code and flowchart based on programming code given .
40