Introduction to Programming
Course Introduction
Programming Languages CSE-112 Credit hours: 2-1
2 classes / week 1 lab / week
Introduction to Programming
Course Contents
Topics Introduction to Programming, IDE, Console output, Good programming practices Algorithms, Pseudo codes, Flow Charts Implicit and Explicit Type conversions, Logical and Mathematical operators Conditions Loops Single and Multidimensional Arrays Functions, Scope, Lifetime, Recursive functions
Introduction to Programming
Course Contents
Topics
Strings Pointers Structures, Unions and Enumerations Indexing, file input output Dynamic Memory Allocation (DMA), Application of DMA in a linked list Classes and Objects Inheritance, Polymorphism, Encapsulation Data Structures : Stacks, queues, trees
Introduction to Programming
Grading Policy
Distribution Percentages Assignments Quizzes 0% 15%
OHT
Project Final
30%
15% 40%
Introduction to Programming
Books
COURSE TEXT BOOK
Object oriented programming in C++(4th edition) Robert Lafore C++ How to program(4th edition) Deitel & Deitel
REFERENCE BOOK
Let us C++ - Yashavant Kanetkar Aikman Series-Programming in C++ object oriented programming - C M Aslam C++ An Introduction to Programming Jesse Liberty, Jim Keogh Programming and Problem Solving with C++ (4th Edition) - Nell Dalle
Introduction to Programming
Introduction
Introduction to Programming
Computer Software
Application Software
Word processors, spread sheets, presentation managers
System Software
Programs that support the execution and development of other programs
Operating systems Translation systems
Introduction to Programming
Translation System
Set of programs used to develop software Translators
Compiler Linker
Examples
Microsoft Visual C++ JBuilder
Introduction to Programming
Computer Languages
Three types of computer languages
Machine Level language
Only language computer directly understands Consists of strings of numbers (0s and 1s)
Assembly language
Abbreviations representing elementary computer operations Requires many instructions to accomplish simple tasks LOAD, ADD, STORE
High level language
Single statements accomplish substantial tasks C++ , Java
Introduction to Programming
Program Writing
Source Program
Compile Library routines Edit Link Other object files Think Load
Execute
Introduction to Programming
IDEs
Integrated Development Environments or IDEs
Supports the entire software development cycle
MS Visual C++, Borland
Provides all the capabilities for developing software
Editor Compiler Linker Loader Debugger Viewer
Introduction to Programming
A Sample Program
// first program in C++
#include <iostream.h> void main (void) {
cout << "Hello ";
}
Introduction to Programming
Executing the Program
// first program in C++ #include <iostream.h> void main (void) { cout << "Hello "; }
Source file Test.cpp
Test.cpp
Compile
Test.obj Link Test.exe
Test.obj + Libraries
Introduction to Programming
A Sample Program
// first program in C++ A comment
#include <iostream.h>
Header File : contains function declarations Start function
void main (void)
{
cout << "Hello "; }
Program Statements
Introduction to Programming
Program Output
Output Terminal?
Console Data File Terminal ports/ sockets
Console Output
cout << In the header file iostream
Introduction to Programming
Escape Sequences
Escape Sequence Description
\n \t \r \a \\ \" Newline. Position the screen cursor to the beginning of the next line. Horizontal tab. Move the screen cursor to the next tab stop. Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Alert. Sound the system bell. Backslash. Used to print a backslash character. Double quote. Used to print a double quote character.
Console Output
How to generate the following output: ****************** Name My name Roll No My roll no ******************
Introduction to Programming
Good Programming Practices
First design the basic flow chart of the program Write the pseudo code
Simple English statements
Algorithm
English and Programming terms
Code the algorithm
Use already given libraries, dont re-invent the wheel
Check for errors
Different types of errors
Example : Even /odd numbers
Introduction to Programming
Program Errors
Compiler Errors
#include <iostream.h> void main (void) { cout << "Hello "
Error Test.cpp 4: Statement Missing ; in function main() Error Test.cpp 4: Compound Statement missing } in function main()
Program Errors
Linker Errors
#include <iostream.h> void Main (void) { cout << "Hello "; }
Linker Error: Undefined Symbol _main
Program Errors
Run-Time Errors
#include <iostream.h> void main (void) { int a=0; int b=10; double c = b/a;
}
These errors dont reveal themselves until the program executes.
Program Errors
Conceptual Errors
#include <iostream.h> void main (void) {
int a=4;
int b=10; int c = b/a; cout << c = << c; } The program is not doing what you expect it to do.
Programming Basics
C++ Data Types
Integer Data types
char short int long
Floating Point Data types
float double long double
Programming Basics
C++ Data Types
Data Type
char short int long float double
No. of Bytes
1 2 4 4 4 8
No. of Bits
8 16 32 32 32 64
long double
10
80
Programming Basics
C++ Data Types
Integer Data Types
Range Unsigned Data Types
Type
short
int long
Sign
signed unsigned
Signed unsigned signed unsigned
Size
Min Value
Max Value
32767 65535
2,147,483,647 4,294,967,295 2,147,483,647 4,294,967,295
16 bits -32768 0
32 bits -2,147,483,648 0 32 bits -2,147,483,648 0
Programming Basics
Declarations
Declarations
Constant Declarations Variable Declarations
Constant Declarations
Constants are used to store values that never change during the program execution.
Using constants makes programs more readable and maintainable.
Programming Basics
Declarations
Constant Declarations
Syntax
const <type> <identifier> = <expression>;
Example
const double PI = 3.1459;
Programming Basics
Declarations
Variable Declarations
Variables are used to store values that can be changed during the program execution
Syntax:
< type > < identifier >; < type > < identifier > = < expression >;
Examples:
int sum; int total = 3445; char answer = 'y'; double temperature = -3.14
Programming Basics
Declarations
A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values Variables are not automatically initialized. For example, after declaration int sum; the value of the variable sum can be anything (garbage).
Thus, it is good practice to initialize variables when they are declared. Once a value has been placed in a variable it stays there until the program deliberately alters it.
Programming Basics
Declarations
Constants and variables must be declared before they can be used When you declare a constant or a variable, the compiler:
Reserves a memory location in which to store the value of the constant or variable. Associates the name of the constant or variable with the memory location.
integer1 45
int integer1 = 45;
Programming Basics
Identifiers
Series of Characters (letters, digits, underscores) Must NOT start with a digit (0 9) Must not be a C++ keyword Case Sensitive
Programming Basics
Lvalues & Rvalues
Lvalues
Expressions that can appear on left side of equation Can be changed (I.e., variables)
x = 4;
Rvalues
Only appear on right side of equation Constants, such as numbers (i.e. cannot write 4 = x;)
Lvalues can be used as Rvalues, but not vice versa
Programming Basics
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // Addition program. #include <iostream.h> // function main void main() { int integer1; int integer2; int sum; begins program execution
// first number to be input by user // second number to be input by user // variable in which sum will be stored
cout << "Enter first integer:\n"; // prompt cin >> integer1; // read an integer cout << "Enter second integer:\n"; // prompt cin >> integer2; // read an integer
sum = integer1 + integer2;
// assign result to sum
cout << "Sum is " << sum << endl; // print sum
} // end function main
Programming Basics
Input Value
Input Source?
Console Data File
Console Input
cin >> In the header file iostream
Programming Basics
Arithmetic Operators
Arithmetic calculations
*
Multiplication
/
Division Integer division truncates remainder
7 / 5 evaluates to 1
%
Modulus operator returns remainder
7 % 5 evaluates to 2
+ and
Addition and Subtraction
Programming Basics
Arithmetic Operators
Rules of operator precedence
Operator(s)
()
Operation(s)
Parentheses
Order of evaluation (precedence)
Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. Evaluated second. If there are several, they re evaluated left to right. Evaluated last. If there are several, they are evaluated left to right.
*, /, or %
Multiplication Division Modulus Addition Subtraction
+ or -
Programming Basics
Arithmetic Operators
Priority of operators
a = 5 + 7 % 2; we may doubt if it really means:
a = 5 + (7 % 2) with result 6 or a = (5 + 7) % 2 with result 0
Parentheses are included when one is not sure
sizeof ( )
returns the size in bytes a = sizeof (char)
Programming Basics
Arithmetic Operators
Arithmetic Assignment Operators a = a + b; a+=b;
void main(void) { int number = 15; number +=10; cout << number << number -=7; cout << number << number *=2; cout << number << number %=2; cout << number << }
Programming Basics
endl; endl; endl; endl;
25 18 36 0
Arithmetic Operators
Increment Operators
count = count + 1; count +=1; count++; OR ++count;
int a = 5; int b = 10; int c = a * ++b;
int a = 5; int b = 10; int c = a * b++;
50
55
Programming Basics
Arithmetic Operators
Decrement Operators
count = count - 1; count -=1; count--; OR --count;
prefix
postfix
Programming Basics
Arithmetic Operators
void main() { int count = 10;
cout cout cout cout cout } << << << << << count << endl; ++count << endl; count << endl; count++ << endl; count << endl;
10 11 11 11 12
Programming Basics
Type Conversion
Automatic Type Conversion Casting
Automatic Type Conversion:
void main (void) { int number = 2; float factor = 1.5; double result = number * factor; cout << Result is: << result; }
Programming Basics
Type Conversion
Casting
void main(void) { short number = 30000; //-32768 to 32767 short result = (number * 10) / 10; cout << Result is: << result; //Result Incorrect number = 30000; result = ( long(number) * 10 ) / 10; //Casting cout << Result is: << result; }
Programming Basics
Relational Operators
To evaluate comparison between two expressions
Result : True / False
Standard algebraic equality operator or relational operator Relational operators
> <
C++ equality Example or relational of C++ operator condition
> < >= <= x > y x < y x >= y x <= y
Meaning of C++ condition
x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y
Equality operators
=
== !=
x == y x != y
x is equal to y x is not equal to y
Programming Basics
Relational Operators
Examples
(7 == 5) would return false (3 != 2) would return true (6 >= 6) would return true
If a=2, b=3 and c=6
(a*b >= c) would return true since it is (2*3 >= 6) (b+4 > a*c) would return false since it is (3+4 > 2*6) ((b=2) == a) would return true
Programming Basics
Equality (==) and Assg (=) Operators
Common error : Does not cause syntax errors
Example
if ( payCode == 4 ) cout << "You get a bonus!;
If == was replaced with =
if ( payCode = 4 ) cout << "You get a bonus!;
PayCode set to 4 (no matter what it was before) Statement is true (since 4 is non-zero) Bonus given in every case
Programming Basics
Logical Operators
NOT, AND, OR : ( !, &&, || )
Operator ! is equivalent to Boolean operation NOT
! (5 == 5) ! (6 <= 4) ! true ! false returns false returns true returns false. returns true.
Logic operators && and || are used when evaluating two expressions to obtain a single result
((5 == 5) && (3 > 6)) returns false (true && false) ((5 == 5) || (3 > 6)) returns true ( true || false ).
Programming Basics
Conditional Operators
condition ? result1 : result2
if condition is true the expression will return result1, if not it will return result2
Examples
7==5 ? 4 : 3 returns 3 since 7 is not equal to 5. 7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2 a>b ? a : b returns the greater one, a or b
Programming Basics
Bitwise Operators
Bitwise Operators ( &, |, ^, ~, <<, >> )
& | ^ ~ << >> AND OR XOR NOT SHL SHR Logical AND Logical OR Logical exclusive OR Complement to one (bit inversion) Shift Left Shift Right
Programming Basics