Lecture - Notes - I Intro To Programming
Lecture - Notes - I Intro To Programming
High level
Fourth Generation Programming Language (4GL)
languages
Compiler /
interpreter
• Assembly language.
• Procedural language.
• non-procedural language.
e.g. SQL
5th Generation Language (5GL)
e.g. Prolog
Programming terms
• Data
• Data type
• Variable
• Constant
• Identifier
– Variable name
• Operator / Operand
– … ????
• Reserved word
• Statement
• Expression
– … ????
• Compiler / Interpreter
– … ????
Programming terms cont.
• Algorithm
• Flowchart
• Bug
• Debugging
– … ???
• Comment
• Imperative language
• Declarative language
• 30+ more reserved words that are new to C++ (not in C).
program structure
1. // my first program in C++
2. #include <iostream>
3. using namespace std;
4. int main ()
5. {
6. cout << "Hello World!";
7. return 0;
8. }
Hello World!
program structure cont.
a comment line:
&
/* ….multiple
lines
commenting …. */
program structure cont.
#include <iostream>
a preprocessor line
• most programs use the C++ standard library hence the line is
included in most of the programs.
program structure cont’d
int main ()
return 0;
• cout does not add a line break after its output unless explicitly
indicated.
Standard Input (cin)
• >> is followed by a variable that will store the data that will
extracted from the stream.
Standard Input (cin)
• cin can only process the input from the keyboard once the enter
key has been pressed.
• cin can be used to request for more than one datum input from
the user.
examples
Hello World
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Hello World!
Sum of two integers
1. #include <iostream>
3. int main() {
9. return 0;
10. }
Variable declaration
• all variables must be declared before use.
format
data_type var,var,…
e.g.
int i,j,k;
float length,height;
char firstname;
Flowchart
Algorithm
Flowchart
https://www.tutorialspoint.com/programming_methodologies/programmin
g_methodologies_flowchart_elements.htm
Flowchart drawing tips
• only one start and one stop symbol
• Arithmetic operator
• Relational operator
• Logical operator
• Bitwise operator
Assignment Operator
• used to give a variable the value of an expression.
e.g.
pin = ‘P’
Arithmetic Operator
• 6 primary operators:
• Addition +
• Subtraction / negation -
• Division /
• Modulus %
• Multiplication *
increment/decrement
• prefix form:
++i; i=i+1;
--i; i=i-1;
• postfix form:
i++; i=i+1;
i--; i=i-1;
• equal to ==
• not equal to !=
• logical OR ||
• logical NOT !
Program Constructs
• used to control the order in which the program statements are
executed.
• sequence
• selection
int main() {
return 0;
}
Selection
• provides for selection between alternatives based on some
condition(s).
int main() {
int number, remainder;
return 0;
}
Flowchart exercise
• Draw a flowchart for a program that checks if the entered
number is even or odd
Selection constructs
if
if – else
switch
if statement
syntax
If (test expression)
{
block of statements;
}
- statements will be executed if expression is true
int main() {
int number;
cout << “enter an integer: ";
cin >> number;
if ( number > 0) {
cout << "entered number is positive";
}
cout << “no positive number is entered.";
return 0;
}
If-else statement
• executes block of statements inside the if - if test expression
is true.
syntax
If (test expression) {
block of statements;
}
else {
block 1 of statements;
}
If-else example
a program to check if a number is even or odd
int main() {
int number, remainder;
cout << "Enter number : ";
cin >> number;
remainder = number % 2;
if (remainder == 0){
cout << number << "is an even integer ";
}
else {
cout << number << "is an odd integer ";
}
return 0;
}
tutorial
Let us write a program that prompts a user to enter
three integers.
- using if statement
return 0;
}