Lecture 2
Lecture 2
Lecture 2
Lecture Outline
• Basic Programming Concepts: Input, Processing and Output
• Data Types
• Variables
• Identifier Naming Conventions
• Reserved Words
• Algorithms
• Pseudocode
• Flowcharts
Introduction
• Programming is the process of designing and implementing
logic to accomplish a specific computer task.
• It involves analyzing, generating algorithms, implementing
algorithms in a particular programming languages, and
testing to make sure the program works correctly.
• Every task that happens inside a computer system involves
processing some input data into a form of output using an
algorithm (instructions)
• We shall discuss what happens during input, processing , and
output process.
Syntax
• The ways that specific words and symbols are used by each
language is called its syntax.
• The actual code depends on what language you are using.
• Every language has its specific syntax
#include <iostream>
using namespace std;
• In C++ the syntax is as follows:
int main( ) {
cout << "Hello World!";
return 0;
}
Syntax
• Line 1: #include <iostream>
#include <iostream> • Is a header file library that lets
using namespace std; us work with input and output
objects, such as cout.
• Header files add functionality
int main( ) { to C++ programs.
cout << "Hello World!"; • Line 2: using namespace std
• Allows us to use names for
return 0; objects and variables from the
} standard library.
Syntax
• Line 3: A blank line.
#include <iostream> • C++ ignores white space.
• But we use it to make the code
using namespace std; more readable.
• Line 4: int main().
int main( ) { • This is called a function. Any
cout << "Hello World!"; code inside its curly brackets {}
will be executed.
return 0;
• Line 5: cout is an object used
} together with the insertion
operator (<<) to output/print
text.
Syntax
• Primitive data types are the most basic types of data available
within any programming language.
• They may include types such as integer, double, float, character, byte,
long, short, signed, unsigned, and Boolean.
• Two (2) derived data types will also be studied in this course,
namely:
• String
• Array
Declare Statement
• When we declare variables using statements like the following:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
• Many programming languages allow you to declare several variables
in one statement, all variables must have the same data type.
int num1, num2;
string firstName, lastName, fullName;
Basic Data Types
1) Start
2) Write “Enter number: ”
3) Input myNumber
4) Set myAnswer = myNumber * 2
5) Write myAnswer
6) End
Example 1: Number-doubling program
// This is a comment – Single Line #include <iostream>
Comments using namespace std;
1) Start
2) //Prompt for first and second number
3) Write “Enter first and second numbers”
4) Input num1, num2
5) Calculate sum = num1 + num2
6) Write “The results is “ + sum
7) End
Example 2
1) Start #include <iostream>
2) input myNumber
3) set myAnswer = myNumber * 2 Input myNumber
4) write myAnswer
Set myAnswer = myNumber * 2
5) end
write myAnswer
end
Example 2
Pseudocode:
1) Start
2) Prompt for user temperature in Fahrenheit
3) Input temperature
4) Calculate temperature in Celsius by subtracting 32 from
Fahrenheit and multiplying by 5 and dividing the result by 9
5) Display the temperature in Celsius
6) End
Example 2
start
Flow chart:
Prompt for user temperature in Fahrenheit
Input temperature
end