1-Lecture 2 C++ programming Basics
1-Lecture 2 C++ programming Basics
C++ PROGRAMMING
BASICS
• Reference Book
• C++ programming from problem analysis to program design 5 th
edition by D.S Malik
Processing a C++ Program
• Source program
• A program written in a high-level language.
• preprocessor directives
• In a C++ program, statements that begin with the symbol # are called
preprocessor directives.
• Object program
• The machine language version of the high-level language program.
• Linker
• A program that combines the object program with other programs
• Loader
• A program that loads an executable program into main memory.
Linker
Processing a C++ Program
Writing C++ program
• C++ program is stored on disk with extension .cpp
• Code is written in the editor which is then compiled
Linked to
compile library
Source Machine code/ Executable code
program object code
C++ statements
•It is fundamental unit, it is instruction for the computer to do
something.
Example
Preprocessor
directive
#include<iostream>
using namespace std;
Main function
void main()
{
C++
cout<<“my first C++ program”<<endl; statements
}
Declaration of a variable
Variables
• Quantity whose value changes during the execution of program
• Variable name represents memory location in computer.
• Data is stored in the memory location
• Consist of alphabets and digits
• Syntax
Type list of variables;
• Examples:
int x;
int a, xyz, k_w;
float b, ab;
char n;
Declaration of a variable
Variables Naming rules
1. Only Alphabets, Digits and Underscores are permitted.
2. Identifier name cannot start with a digit.
3. Keywords cannot be used as a name.
4. Upper case and lower case letters are distinct.
5. Special Characters are not allowed.
6. Blank spaces are not allowed.
7. Variable names can not start from a number.
example:
int 2sum; invalid because variable name is start with a
number
Declaration of a variable
Valid Variables Names
Identifier Note
Name Capital Letter and Small Letters are Allowed
name Small Letters are allowed
Digits and Underscore is allowed along with
name_1
alphabets
Keywords are allowed but we have to change case of
Int
any letter or complete word
Keywords are allowed but we have to change case of
INT
any letter or complete word
Underscore at the first position is allowed in C++
_SUM
language
sum_of_numbers We can concatenate multiple words with underscore
Best Style to concatenate multiple words (Changing
firstName
case of First Letter of Successive Word)
Declaration of a variable
Invalid Variables Names
Identifier Explanation
• Keywords/reserved words
Keywords are one type of token, That is fixed or pre define in
programming language.
E.g. main, include, int
Initialization of a variable
• When a variable is declared, memory is assigned to it.
• Value in that memory location is also assigned to the
variable.
• Assigning value to a variable at the time of declaration is
called initialization of the variable.
• Example
int ab=0;
float c=0.5;
int a=2 , b= 6 , c=10;
Example
#include<iostream>
#include<conio>
using namespace std;
void main()
{
int a=2,b=3; Variable
initialization
float x=3.2;
char name[15] = “Sara khan”;
Example:
int a = 10;
a > 5 ? cout << "true" : cout << "false“
Comma operators
• used to separate variable names and to separate
expressions
• In case of expressions, the value of last expression is
produced and used.
• int a,b,c; // variables declaration using comma operator
a=b++, c++; // a = c++ will be done.
Relational operators
• six relational operators in C++: ==, !=, >, <, >=, <=
• == returns true if both the left side and right side are equal
• != returns true if left side is not equal to the right side of
operator.
• > returns true if left side is greater than right.
• < returns true if left side is less than right side.
• >= returns true if left side is greater than or equal to right
side.
• <= returns true if left side is less than or equal to right
side.
Logical operators
• Logical Operators are used with binary variables
• mainly used in conditional statements and loops for
evaluating a condition.
• Logical operators in C++ are: &&, ||, !
• b1&&b2 will return true if both b1 and b2 are true else it
would return false.
• b1||b2 will return false if both b1 and b2 are false else it
would return true.
• !b1 would return the opposite of b1, that means it would
be true if b1 is false and it would return false if b1 is true.
Arithmetic operators
• Operators are the symbols that represent arithmetic
operations.
• Following arithmetic operators are used in c++
operat Meaning
or
+ Addition
- Subtraction
* Multiplication
/ Division
% For remainder
Arithmetic expression
• Combination of variables, constants and arithmetic
operators.
• Assignment operator = is used to assign the calculated
value of the expression to the receiving variable
• Example
int a = 2, b = 3, ans;
ans = a * b + 10;
Basic input/output
• cout stands for console output, it is used to display the
output on computer screen.
• e.g. cout<<“hello”<<endl;
Get from
operator (>>)
The “endl” manipulator
• The “endl” stands for end of line. It has same effect as the
escape “\n”.
• Is predefined in <iostream.h> header file.
• Example
void main()
{
cout<<“I am a “<<endl<<“Pakistani”;
}
Escape sequence
• Special non-printing character used to control printing on
the output device.
• They are written in single and double quotes.
• It is a combination of back slash \ and the code character.
Common escape Function
sequences
\ a Beep(alarm)
\ b Backspace
\ n New line
\ t Tab
\\ Backslash
\’ Single
quotation
\” Double
quotation
Compound assignment statement
• The assignment statement used to assign one value to
many variables
• E.g x = y = 10;
***Learning C++***
***QUIZ-1***