Programming Fundamentals
Course Instructor: Dr. Romana Farhan
Lecture # 1
Course Learning outcomes
S.
N
o
Course Learning Outcome (CLO’s) Bloom’s
Learnin
g Level
1 Learn skills to propose solutions for real
life problems in terms of computer
programs.
C2
2 Analyze programs with standard notation/
methods to identify their structures.
C3
3 Develop programs using high level C3
Evaluation Criteria
Evaluation Criteria Percentage
Quiz (3-4) 15%
Assignment (2-3) 10%
Mid 25%
Final 50%
Computer
Languages
Compiler Assembler
Linker Algorithms Flow chart
Program
design
Process
Object
oriented
programmin
g
Testing and
Debugging
Syntax
error
Logical
error
Runtime
error
CONTENTS
Programming is like Legos
Small number of primitive constructs!
You can build huge structures using these pieces.
Computer Languages
• High level language resemble human
language. It is easy for human beings to
write programs in it e.g., C++, C#, Java,
Python, Pascal etc.
High level Languages
• One to one correspondence with machine
language. Use symbols or mnemonics to
represent instructions and data.
Assembly Language
• It is executed directly by hardware. Machine
instruction consists of binary e.g., 0s and
1s. .
Machine Language
CONTD…
Any high-level language program must
be translated into machine language
before the computer can understand and
follow the program
Compiler
• A program that translates a high-level language like
C++ to a machine language that the computer can
directly understand and execute.
• The input to the compiler is a program in high level
language (source program or source code) and the
translated version produced by the compiler is called
the object program or object code.
CONTD…
The magic of Compiler
Assembler
• The Assembler is a Software that converts an assembly
language code to machine code.
• We can also name an assembler as the compiler of assembly
language.
• An assembler works by assembling and converting the source
code of assembly language into object code or an object file
that constitutes a stream of zeros and ones of machine code,
which are directly executable by the processor.
CONTD…
Linker
The object code for your C++ program must be
combined with the object code for routines (such as
input and output routines) that your program uses. This
process of combining object code is called linking and
is done by a program called a linker.
Linker
Algorithms
A sequence of precise instructions which leads to a
solution is called an algorithm.
To qualify as an algorithm, a set of instructions must
completely and unambiguously specify the steps to be
taken and the order in which they are taken.
Example: Sum of two numbers
Flow chart
a graphical representation of a computer program in
relation to its sequence of functions (as distinct from
the data it processes).
Flow Chart Symbols
Example: Sum of two numbers
Program design process
Testing and Debugging
Mistake in a program is usually called a bug, and the
process of eliminating bugs is called debugging.
Syntax error
Violation of the syntax (that is, the
grammar rules) of the programming
language, such as omitting a semicolon.
Example
• He eats lunch the…
• I going am school to…
Logical error
The program will run perfectly but it
will not behave in desired manner. It is
most difficult to spot logical errors. Can
only be resolved by careful testing.
Example
If you intend to write:
“A dog bit a man…”
BUT mistakenly end up with
“A man bit a dog…”
Runtime Error
There are certain kinds of errors that the
computer system can detect only when a
program is run, these are called run-
time errors.
Example
If the computer attempts to divide a
number by zero, that is normally a
run-time error.
Brainstorming Time
Why study C++ language??
What are latest job trends in
C++??
What are major applications
of C++ in the field of
computing ??
 ….
 …..
Variables, Data types
and Input/output
constructs
Outline
Layout of
simple ++
program
Variables
Identifier
s
Assignme
nt
statemen
ts
Uninitialize
d variables
Output
using
cout
Input
using cin
Escape
sequence
s
Data
types
Integers
Floating
point
Type char
Class
string
Type bool
Arithmetic
operators and
expressions
Commen
ts
Naming
constant
s
Layout of a simple C++
program
Sample Program
• // This is a simple C++ program.
#include <iostream>
using namespace std;
int main()
{
cout<<“Welcome to Computer
Programming course”;
return 0;
}
CONTD…
#include<iostream>
• A program includes various programming elements
that are already defined in the standard C++ library.
• In order to use such pre-defined elements in a
program, an appropriate header/directives must be
included in the program.
• Directives always begin with the symbol #.
• <iostream> is the name of a library that contains the
definitions of the routines that handle input from
the keyboard and output to the screen.
• Do not include extra space between the < and the
iostream file name or between the end of the file
name and the closing >.
• using namespace std;
– This line says that the names
defined in iostream are to be
interpreted in the “standard way”.
• int main()
– It tells that your main part of a
program starts here.
• { }
– Braces mark beginning and end of
the main function.
• return 0;
– The last line in the program. It
marks end of the program.
Variables
• Variable is the basic storage unit in a program. It is
a name given to a memory location.
• The compiler assigns a memory location to each
variable name in the program. The value of the
variable, in a coded form, is kept in the memory
location assigned to that variable.
• We do not know what addresses the compiler will
choose for the variables in our program.
• Data held in a variable is called its value or a
literal; Number/data held by a C++ variable can be
changed.
• A C++ variable is guaranteed to have some value in
it, if only a garbage number left in the computer’s
memory by some previously run program.
Names:
Identifiers
• Identifiers are used as names for variables
and other items in a C++ program.
• To make your program easy to understand,
you should always use meaningful names
for variables.
• Rules for naming variables:
– You cannot use a C++ keyword (reserved
word) as a variable name.
– Variable names in C++ can range from 1
to 255 characters.
– All variable names must begin with a
letter of the alphabet (a-z, A-Z) or an
CONTD…
• After the first initial letter, variable
names can also contain letters and
numbers.
• No spaces or special characters
allowed.
• C++ is case Sensitive. Uppercase
characters are distinct from lowercase
characters.
• Examples:
– A, a_1, x123 (legal)
– 1ab, da%, 1-2, (not acceptable)
– Test, test, TEST (case-sensitive)
Variable
declarations
• Every variable in a C++ program must be
declared before the variable can be used.
• When you declare a variable, you are
telling the compiler—and, ultimately, the
computer—what kind of data you will be
storing in the variable, and what size of
memory location to use for the variable.
• Each declaration ends with a semicolon (;).
• When there is more than one variable in a
declaration, the variables are separated by
commas.
• The kind of data that is held in a variable is
called its type and the name for the type,
such as int or double, is called a type name.
Syntax
• The syntax for a programming languages is
the set of grammar rules for that language.
• The syntax for variable declarations is as
follows:
• Syntax
– Type_Name Var_Name_1, Var_Name_2, ...;
• Examples
– int count, sum, number_of_person;
– double distance;
Assignment
statements
• In an assignment statement, first the
expression on the right-hand side of the
equal sign is evaluated, and then the
variable on the left-hand side of the equal
sign is set equal to this value.
• In an assignment statement, the
expression on the right-hand side of the
equal sign can simply be another variable
or a constant.
• Syntax
– Variable = Expression;
• Examples
– sum=a; //variable
– distance = rate * time; //expression
Uninitialized
variables
• Variable that has not been given a
value is said to be uninitialized.
• One way to avoid an uninitialized
variable is to initialize variables at
the same time they are declared.
• You can initialize some, all, or none
of the variables in a declaration that
lists more than one variable.
• Examples:
• int count=0; double avg=99.9; int
a=10, b, c=0;
C++ Keywords/Reserved words
Output using cout
• The values of variables as well as strings of
text may be output to the screen using
cout.
• The arrow notation << is often called the
insertion operator.
• You can simply list all the items to be output
preceding each item to be output with the
arrow symbols <<.
• Strings must be included in double quotes.
• Examples:
– cout<<“This is our first c++ program”;
– cout<<“The sum is”<<sum;
– cout<<“distance is”<<(time * speed);
Input using cin
• A cin statement sets variables equal to
values typed in at the keyboard.
• cin is a predefined variable that reads data
from the keyboard with the extraction
operator (>>).
• Syntax
• cin >> Variable_1 >> Variable_2 >> ... ;
• Example
• cin >> number >> size;
• cin >> time;
Data Types
• Data types are used to tell the variables
the type of data it can store.
• Whenever a variable is defined in C++, the
compiler allocates some memory for that
variable based on the data-type with which
it is declared.
• Every data type requires a different amount
of memory.
Integer types
• The integer data type basically represents
whole numbers (no fractional parts).
• The reason is threefold.
– First, some things in the real world are
not fractional.
– Second, the integer data type is often
used to control program flow by counting.
– Third, integer processing is significantly
faster within the CPU than is floating
point processing.
CONTD…
Floating point types
• The floating-point family of data types
represents number values with fractional
parts.
• They are technically stored as two integer
values: a mantissa and an exponent.
• They are always signed.
• A floating_point number can also be a scientific
number with an "e" to indicate the power of 10:
Type char
• Values of the type char are single
symbols such as a letter, digit, or
punctuation mark.
• A variable of type char can hold any
single character on the keyboard e.g., ’A'
or '+' or an 'a’.
• Note that uppercase and lowercase
versions of a letter are considered
different characters.
• The text in double quotes that are output
using cout are called string values.
• Be sure to notice that string constants
are placed inside of double quotes, while
Type
bool
• Expressions of type bool are called Boolean
after the English mathematician George
Boole, who formulated rules for mathematical
logic.
• Boolean expressions evaluate to one of the
two values, true or false.
• Boolean expressions are used in branching
and looping statements.
Comment
• In C++ the symbols // are used to indicate
the start of a comment.
• All of the text between the // and the end of
the line is a comment.
• The compiler simply ignores anything that
follows // on a line.
• Anything between the symbol pair /* and
the symbol pair */ is considered a comment
and is ignored by the compiler. Unlike the //
comments, /* to */ comments can span
several lines,
THE END

C++ Introduction to basic C++ IN THIS YOU WOULD KHOW ABOUT BASIC C++

  • 1.
    Programming Fundamentals Course Instructor:Dr. Romana Farhan Lecture # 1
  • 2.
    Course Learning outcomes S. N o CourseLearning Outcome (CLO’s) Bloom’s Learnin g Level 1 Learn skills to propose solutions for real life problems in terms of computer programs. C2 2 Analyze programs with standard notation/ methods to identify their structures. C3 3 Develop programs using high level C3
  • 3.
    Evaluation Criteria Evaluation CriteriaPercentage Quiz (3-4) 15% Assignment (2-3) 10% Mid 25% Final 50%
  • 4.
    Computer Languages Compiler Assembler Linker AlgorithmsFlow chart Program design Process Object oriented programmin g Testing and Debugging Syntax error Logical error Runtime error CONTENTS
  • 6.
    Programming is likeLegos Small number of primitive constructs!
  • 7.
    You can buildhuge structures using these pieces.
  • 8.
    Computer Languages • Highlevel language resemble human language. It is easy for human beings to write programs in it e.g., C++, C#, Java, Python, Pascal etc. High level Languages • One to one correspondence with machine language. Use symbols or mnemonics to represent instructions and data. Assembly Language • It is executed directly by hardware. Machine instruction consists of binary e.g., 0s and 1s. . Machine Language
  • 10.
    CONTD… Any high-level languageprogram must be translated into machine language before the computer can understand and follow the program
  • 11.
    Compiler • A programthat translates a high-level language like C++ to a machine language that the computer can directly understand and execute. • The input to the compiler is a program in high level language (source program or source code) and the translated version produced by the compiler is called the object program or object code.
  • 12.
  • 13.
    The magic ofCompiler
  • 14.
    Assembler • The Assembleris a Software that converts an assembly language code to machine code. • We can also name an assembler as the compiler of assembly language. • An assembler works by assembling and converting the source code of assembly language into object code or an object file that constitutes a stream of zeros and ones of machine code, which are directly executable by the processor.
  • 15.
  • 16.
    Linker The object codefor your C++ program must be combined with the object code for routines (such as input and output routines) that your program uses. This process of combining object code is called linking and is done by a program called a linker.
  • 17.
  • 18.
    Algorithms A sequence ofprecise instructions which leads to a solution is called an algorithm. To qualify as an algorithm, a set of instructions must completely and unambiguously specify the steps to be taken and the order in which they are taken.
  • 19.
    Example: Sum oftwo numbers
  • 20.
    Flow chart a graphicalrepresentation of a computer program in relation to its sequence of functions (as distinct from the data it processes).
  • 21.
  • 22.
    Example: Sum oftwo numbers
  • 23.
  • 24.
    Testing and Debugging Mistakein a program is usually called a bug, and the process of eliminating bugs is called debugging.
  • 25.
    Syntax error Violation ofthe syntax (that is, the grammar rules) of the programming language, such as omitting a semicolon.
  • 26.
    Example • He eatslunch the… • I going am school to…
  • 27.
    Logical error The programwill run perfectly but it will not behave in desired manner. It is most difficult to spot logical errors. Can only be resolved by careful testing.
  • 28.
    Example If you intendto write: “A dog bit a man…” BUT mistakenly end up with “A man bit a dog…”
  • 29.
    Runtime Error There arecertain kinds of errors that the computer system can detect only when a program is run, these are called run- time errors.
  • 30.
    Example If the computerattempts to divide a number by zero, that is normally a run-time error.
  • 31.
    Brainstorming Time Why studyC++ language?? What are latest job trends in C++?? What are major applications of C++ in the field of computing ??  ….  …..
  • 32.
    Variables, Data types andInput/output constructs
  • 33.
    Outline Layout of simple ++ program Variables Identifier s Assignme nt statemen ts Uninitialize dvariables Output using cout Input using cin Escape sequence s Data types Integers Floating point Type char Class string Type bool Arithmetic operators and expressions Commen ts Naming constant s
  • 34.
    Layout of asimple C++ program
  • 35.
    Sample Program • //This is a simple C++ program. #include <iostream> using namespace std; int main() { cout<<“Welcome to Computer Programming course”; return 0; }
  • 36.
    CONTD… #include<iostream> • A programincludes various programming elements that are already defined in the standard C++ library. • In order to use such pre-defined elements in a program, an appropriate header/directives must be included in the program. • Directives always begin with the symbol #. • <iostream> is the name of a library that contains the definitions of the routines that handle input from the keyboard and output to the screen. • Do not include extra space between the < and the iostream file name or between the end of the file name and the closing >.
  • 37.
    • using namespacestd; – This line says that the names defined in iostream are to be interpreted in the “standard way”. • int main() – It tells that your main part of a program starts here. • { } – Braces mark beginning and end of the main function. • return 0; – The last line in the program. It marks end of the program.
  • 38.
    Variables • Variable isthe basic storage unit in a program. It is a name given to a memory location. • The compiler assigns a memory location to each variable name in the program. The value of the variable, in a coded form, is kept in the memory location assigned to that variable. • We do not know what addresses the compiler will choose for the variables in our program. • Data held in a variable is called its value or a literal; Number/data held by a C++ variable can be changed. • A C++ variable is guaranteed to have some value in it, if only a garbage number left in the computer’s memory by some previously run program.
  • 39.
    Names: Identifiers • Identifiers areused as names for variables and other items in a C++ program. • To make your program easy to understand, you should always use meaningful names for variables. • Rules for naming variables: – You cannot use a C++ keyword (reserved word) as a variable name. – Variable names in C++ can range from 1 to 255 characters. – All variable names must begin with a letter of the alphabet (a-z, A-Z) or an
  • 40.
    CONTD… • After thefirst initial letter, variable names can also contain letters and numbers. • No spaces or special characters allowed. • C++ is case Sensitive. Uppercase characters are distinct from lowercase characters. • Examples: – A, a_1, x123 (legal) – 1ab, da%, 1-2, (not acceptable) – Test, test, TEST (case-sensitive)
  • 41.
    Variable declarations • Every variablein a C++ program must be declared before the variable can be used. • When you declare a variable, you are telling the compiler—and, ultimately, the computer—what kind of data you will be storing in the variable, and what size of memory location to use for the variable. • Each declaration ends with a semicolon (;). • When there is more than one variable in a declaration, the variables are separated by commas. • The kind of data that is held in a variable is called its type and the name for the type, such as int or double, is called a type name.
  • 42.
    Syntax • The syntaxfor a programming languages is the set of grammar rules for that language. • The syntax for variable declarations is as follows: • Syntax – Type_Name Var_Name_1, Var_Name_2, ...; • Examples – int count, sum, number_of_person; – double distance;
  • 43.
    Assignment statements • In anassignment statement, first the expression on the right-hand side of the equal sign is evaluated, and then the variable on the left-hand side of the equal sign is set equal to this value. • In an assignment statement, the expression on the right-hand side of the equal sign can simply be another variable or a constant. • Syntax – Variable = Expression; • Examples – sum=a; //variable – distance = rate * time; //expression
  • 44.
    Uninitialized variables • Variable thathas not been given a value is said to be uninitialized. • One way to avoid an uninitialized variable is to initialize variables at the same time they are declared. • You can initialize some, all, or none of the variables in a declaration that lists more than one variable. • Examples: • int count=0; double avg=99.9; int a=10, b, c=0;
  • 45.
  • 46.
    Output using cout •The values of variables as well as strings of text may be output to the screen using cout. • The arrow notation << is often called the insertion operator. • You can simply list all the items to be output preceding each item to be output with the arrow symbols <<. • Strings must be included in double quotes. • Examples: – cout<<“This is our first c++ program”; – cout<<“The sum is”<<sum; – cout<<“distance is”<<(time * speed);
  • 47.
    Input using cin •A cin statement sets variables equal to values typed in at the keyboard. • cin is a predefined variable that reads data from the keyboard with the extraction operator (>>). • Syntax • cin >> Variable_1 >> Variable_2 >> ... ; • Example • cin >> number >> size; • cin >> time;
  • 48.
    Data Types • Datatypes are used to tell the variables the type of data it can store. • Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. • Every data type requires a different amount of memory.
  • 49.
    Integer types • Theinteger data type basically represents whole numbers (no fractional parts). • The reason is threefold. – First, some things in the real world are not fractional. – Second, the integer data type is often used to control program flow by counting. – Third, integer processing is significantly faster within the CPU than is floating point processing.
  • 50.
  • 51.
    Floating point types •The floating-point family of data types represents number values with fractional parts. • They are technically stored as two integer values: a mantissa and an exponent. • They are always signed. • A floating_point number can also be a scientific number with an "e" to indicate the power of 10:
  • 52.
    Type char • Valuesof the type char are single symbols such as a letter, digit, or punctuation mark. • A variable of type char can hold any single character on the keyboard e.g., ’A' or '+' or an 'a’. • Note that uppercase and lowercase versions of a letter are considered different characters. • The text in double quotes that are output using cout are called string values. • Be sure to notice that string constants are placed inside of double quotes, while
  • 53.
    Type bool • Expressions oftype bool are called Boolean after the English mathematician George Boole, who formulated rules for mathematical logic. • Boolean expressions evaluate to one of the two values, true or false. • Boolean expressions are used in branching and looping statements.
  • 54.
    Comment • In C++the symbols // are used to indicate the start of a comment. • All of the text between the // and the end of the line is a comment. • The compiler simply ignores anything that follows // on a line. • Anything between the symbol pair /* and the symbol pair */ is considered a comment and is ignored by the compiler. Unlike the // comments, /* to */ comments can span several lines,
  • 55.