0% found this document useful (0 votes)
27 views5 pages

Notes 1

Chapter 1 provides a comprehensive overview of C++ programming, covering fundamental concepts such as character sets, tokens, keywords, identifiers, literals, operators, and data types. It explains control statements, including selection and iteration statements, as well as the structure of a C++ program and various types of expressions. Additionally, it discusses type conversion and jump statements that alter the flow of control in a program.

Uploaded by

123sayyidnoushad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Notes 1

Chapter 1 provides a comprehensive overview of C++ programming, covering fundamental concepts such as character sets, tokens, keywords, identifiers, literals, operators, and data types. It explains control statements, including selection and iteration statements, as well as the structure of a C++ program and various types of expressions. Additionally, it discusses type conversion and jump statements that alter the flow of control in a program.

Uploaded by

123sayyidnoushad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 1

Review of C++ Programming

Character set : Fundamental unit of C++ language. It consists of letters, digits, special characters and white spaces.
Tokens : Building blocks of C++ programs. Classified into keywords, identifiers, literals,Punctuators and operators.
Keywords: Reserved words that convey specific meaning to the language compiler. Eg : if, break , for etc...
Identifiers : User-defined words to identify memory locations, statements, etc. Identifiers include variables, labels,
function names, etc.
• The first character must be letter or underscore
• Keyword cannot be used as an identifier.
• Special characters or white spaces cannot be used
• Identifier for memory location is called variable.
• Identifier for a statement is called label.
• Identifier for group of statements is called function name.
Literals (Constants) : Tokens that do not change their value during the program run.
Classified into integer constants, floating point constants, character constants and string constants.
• Integer literals : Whole numbers Eg : 25 , -45 , +12 etc
• Floating point literals : Numbers having fractional parts Eg : 12.5 , -2.50 etc
• Character literals : A character in single quotes Eg: ‘A’ , ‘b’ , ‘8’ ,
(Escape sequences are character constants used to represent non graphic symbols. Eg: ‘\ n’ , ‘\t’ etc )
• String literals : One or more characters within double quotes. Eg: “A”, “break” , “123” etc

Punctuators: Special symbols that have syntactic or semantic meaning to the compiler.
Eg: #, : , ’ , ” ,() ,[]
Operators: Operators are the tokens that trigger some kind of operations. The operations applied
on a set of data called operands. Based on the number of operands, operators are classified into three,
Unary operator, Binary operator and Ternary operator.
A unary operator acts on a single operand.
Eg : Unary + , Unary - , ++ (increment operator), -- (decrement operator), logical NOT (!)
A binary operator acts on two operands.
Eg : Arithmetic operators ( + , - , * , / , % )
Relational operators ( < , <= , > , >= , == , != )
Logical operators ( Logical AND ( &&) , Logical OR ( ||)
Arithmetic assignment operator ( += , -= , *= , /= , %= )
A ternary operator(? :) acts on three operands.
Eg: conditional operator ( ? : )
Based on the nature of operation operators are classified into three Arithmetic, Relational and Logical operators
a). Arithmetic operator are used for addition, subtraction, multiplication and division(+, - , * , / , %).
C++ provides a special operator modulus operator(%) for getting remainder of division operation.
For example 10 % 2 returns 0.
b) Relational operators
Relational operators are used to compare values. They produces only ‘True’ of ‘False’ values.
The following are the relational operators in C++. (<, <=, >,>=,!=, == <> )
c). Logical operator Logical operators allows us to combine two or more conditions. They are often referred to
as Boolean operators. The logical operators are &&(logical AND),||(logical OR),and !(logical NOT).
There are some other operators such as,
d). Input-Output operators
The Input-Output operators are used to take input and display output. The operator(>>) is used for taking input, and
the operator(<<) is used to display the output. The >> operator is called extraction or get from operator. The operator
<< is called insertion or put to operator. These are binary operators.
The input operator is used with standard input stream, cin and the output operator is used with standard output stream,
cout. The input operator takes the value through cin and stores in a variable.
cin >> number;
cout << number;
Cascading of I/O operators
The multiple use of input or output operators in a single statement is called cascading of I/O operators.
Eg. cin>>a>>b;
e). Assignment operator (=)
The assignment operator is used to assign a value to a variable.
For example a=10 Assigns the value 10 to a
The symbol = assigns a value, where as the symbol == compare two values(Relational Operator).
Data types
These are means to identify the type of data and associated operations. Data types are classified into fundamental and
user-defined data types.
Fundamental data types include int , char , float , double and void.
Variable : Identifier ( Name) given to memory location
• Variable name – the name given to memory location
• L value – the memory address
• R value – The value stored in the variable ( content)
Type modifiers
The keyword signed , unsigned , short and long are the type modifiers. They are used with data types to modify the
size and range of data.
Expressions
Operators and operands are combined to form expressions. Classified into arithmetic expressions, relational
expressions and logical expressions.
Arithmetic expression is divided into integer expression and real expression.
Integer expression - Only integer data and integer results.
Real expressions – Only floating point data and float type result.

Arithmetic expression Eg : a+b , a*b
a. Integer expression Eg ; 5 + 4 , 10 * 5
b. Real expression Eg : 2.5 + 3.0 , 5.0 / 2.5
Relational and logical expressions return True or False as outputs.
• Relational expression Eg : a <= b , a==5
• Logical expression Eg : ( a<=5 ) && ( b<3 )

Type conversion The process of converting the current data type of a value into another type.
Two types - implicit and explicit conversions. In implicit type conversion, complier is responsible for the
conversion. Also known as type promotion.
In explicit conversion, user is responsible for the conversion. Also known as type casting.
Statements : Smallest executable unit of a C++ program. Every C++ statement end with semi colon (;)
Declaration statement : Specifies the type of data that will be stored in a variable.
Syntax : data_type variable name ; Eg : float n1; (
Variable initialization statement (eg: int a=5; const float pi=3.14;)
Input statement Specifies an input operation.
Syntax : cin>>variable ; Eg : cin>> n1;
Output statement Specifies an output operation
Synatax : cout << data ; Eg : cout<< “Welcome” ; cout << n1;
Assignment statement stores value to a variable
Syntax : variable = value; Eg : n1 = 100 ;
Increment and a++; (postfix form) ++a; (prefix form) : Same as a = a + 1;
Decrement a--; (postfix form) --a; (prefix form) : Same as a = a – 1;
Structure of a C++ program
#include < header file >
using namespace std;
int main ()
{
statements ;
....................
return 0 ;
}
Control statements
Control statements are used to alter the normal sequence of execution of a program. They are classified
into two types,
a) Decision Making or Selection Statements
b). Iteration Statements
a). Selection statements
The Selection statements are based on a condition. They are also known as branching statements. The flow of control
depends on the result of a particular condition. C++ supports three types of decision statements,
a) if statement
b) if – else statement
c) Nested if
d) if else ladder
e) Switch statement
a). if statement
The if statement is a single path statement, which executes statement only if the condition is true.
Syntax :
if(condition or expression)
statement1;
If the condition is true then the statement1 will be executed.
if (mark >=18)
cout <<”You have Passed “ ;
In the above example the message You have passed will be displayed only when the condition is true
ie, mark >=18 is true.
b). if – else statements
The if... else statement is used to test a logical condition and choose one of the alternatives based on the result of
logical condition.
The syntax : eg:
If (condition or expression) if( a> b)
statement 1; cout << “First number is largest: ” ;
else else
statement 2; cout <<”Second number is largest: ” ;
c). Nested if
An if – else statement can be placed inside another if – else statement, is called nesting of if.
Example
if (mark >=80)
{
if (age>18)
cout<<”Eligible for higher studies” ;
else
cout <<”Not Eligible” ;
}
d). else-if ladder
The else-if ladder helps to select one out of many alternative block of statements.
Syntax : Eg.
if (test-expression) if ( percentage>=90)
{
Statement 1; cout<<”A+”;
}
else if (test-expression) else if ( percentage>=80)
{
Statement 2; cout<<”A”;
}
else if (test-expression) else if ( percentage>=70)
{
Statement 3; cout<<”B+”;
}
else else if ( percentage>=60)
{
Statements; cout<<”B”;
} else if ( percentage>=50)

cout<<”C+”’
else
cout<<”Low Score”’
Switch statement
The Switch enables to select one among several alternatives. It is a multipath statement.
Syntax: Eg.
Switch (expression) switch(day_no)
{ {
case value1 : case 1 :
{ {
Statement 1; cout<<”Sunday ”;
break; break;
} }
case value2 : case 2:
{ {
Statement 2; cout<<”Monday”;
break; break;
} }
case value3 : case 3:
{ {
Statement 3; cout<<”Tuesday”;
break; break;
} }
............................

default : default :
{
{ cout<<”Invalid entry”;
Statement n;
break; break;
} }
} }
The expression is evaluated and statements are executed according to the value . If no match is
found, then default statement is executed.

Iteration statements / Looping statements


Statements that allow repeated execution of a set of one or more statements.
Components of a Looping statement
• Initialisation
• Test expression / Condition
• Body of the loop
• Update expression
Iteration statements are classified into two: entry- controlled and exit-controlled.
In entry-controlled loop, test expression is evaluated before the execution of the loop-body.
Eg : for , while
In exit-controlled loop, condition is checked only after executing the loop- body.
Eg : do .... while

a). for statement :


The for statement is the most commonly used statement in C++.It is an entry controlled loop.
Syntax :
for(initialisation ; test expression ; updation)
int main()
{ { int i;
Body of loop; for(i=0;i<10;i++)
} cout<<i;
}
b). While Statement
The while ... loop is an entry controlled loop. The condition is checked first and if it is true the
loop will be executed.
Syntax:
while (expression)
{
Body of loop;
Update expression;
}
Example: Program to print first 10 even numbers
#include<iostream>
int main()
{ int i;
i=2;
while(i<=10)
{
cout<<i;
i=i+2;
}
}
c). do ... while loop
The do while loop executes statement before the expression is tested. Exit control loop
Syntax:
do
{
Statements ;
}
while (expression);

Example: Program to print first 10 even numbers


#include<iostream>
int main()
{
int i;
i=2;
do
{
cout<<i;
i=i+2;
} while(i<=10);
}
Conditional Operator (?:)
The conditional operator is a ternary operator(ie, it takes three operands).The general form is
condition? true-part: false-part;
The condition is a boolean expression. If it is true the result is the true part, else the result is the false-part .
Eg. (a>b)? cout<< a : cout << b;
Jump Statements
Jump statements are used to transfer the program control from one place to another.
C++ provides four jump statements
• goto – transfer the program control to a label
• break – break transfer the program control outside the loop or switch
• continue - continue transfer the program control to the next iteration
• return - used to transfer control back to the calling program or to come out of a function.

You might also like