0% found this document useful (0 votes)
1 views

Lecture-5 (23-09-2023)

The document covers the basics of tokens, operators, and variable declarations in C++ programming. It explains keywords, identifiers, and the rules for naming variables, as well as the syntax for variable declarations and assignment statements. Additionally, it discusses input and output operations using 'cin' and 'cout', and emphasizes the importance of proper indentation and documentation in programming.
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)
1 views

Lecture-5 (23-09-2023)

The document covers the basics of tokens, operators, and variable declarations in C++ programming. It explains keywords, identifiers, and the rules for naming variables, as well as the syntax for variable declarations and assignment statements. Additionally, it discusses input and output operations using 'cin' and 'cout', and emphasizes the importance of proper indentation and documentation in programming.
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
You are on page 1/ 21

Problem Solving and

MA144:
Computer Programming

Lecture-5

Tokens, Operators
Character Set
Tokens
Tokens - the smallest individual units in a program
 Keywords
 Identifiers
 Constants
 Strings
 Operators
 Special symbols
A C++ program is written using these tokens, white spaces,
and the syntax of the language.
The syntax for a programming language is the set of
grammar rules for that language.
Keywords
• Reserved identifiers (names) and cannot be used as names for
the program variables or other user-defined program elements
• Because they have predefined meaning in C++
Keywords (contd…)

 words like cin and cout are not on the list of keywords
 you are allowed to redefine these words,
 hence they are not keywords
 Avoid re-define these words
Identifiers
• Identifiers refer to the names of variables, functions, arrays,
classes, etc., created by the programmer.
• They are the fundamental requirement of any language.
• Each language has its own rules for naming these identifiers.
Rules to follow…
• Only alphabetic characters, digits and underscores are permitted.
• The name cannot start with a digit (but can start with underscore).
• Uppercase and lowercase letters are distinct.
• A declared keyword cannot be used as a variable name.
x 12
_x1 3X
x_1 _abc %change
ABC123z7 data-1
sum myfirst.c
RATE PROG.CPP
count Data abc
data2
Big_Bonus use meaningful identifiers as name
of the identifier
Variable Declarations
First rule of variable use:
Must declare a variable (by specifying its type and name)
before using it anywhere in your program
 All variable declarations should ideally be at the beginning
of the main() or other functions ends with semicolon

Syntax
type var_name_1, var_name_2, var_name_3;
type – what kind of data we will be storing in the variable

Examples int sum, a, b;


float average, x, y, z;
 compiler implements variables as memory locations
 the value of a variable is stored in the memory location
assigned to that variable
 memory is a list of consecutive storage locations,
each having a unique address
 a variable is mapped to a location of the memory,
called its address
 the value of a variable can be changed during the execution of
the program
 A variable can have only one value assigned to it at any given time
during the execution of the program
Assignment Statements
Assignment - assigning a value to a variable

Syntax variable = expression;


The assignment statement instructs the computer that set
the value of the expression on RHS to the variable on LHS

i.e., store the value of the expression in the memory location,


which is named variable

Examples sum=a+b; = is an assignment operator


count=cout+1;
a=4;
Initializing a variable
We can initialize a variable at the time of
variable declaration

int count=5;

int sum=a+b;
Output using cout object
• The operator << is called the insertion or put to operator.
• It inserts (or sends) the contents of the variable on its right to the
object on its left
cout << “Hellow world”;
cout << sum;
Input using cin object
• The operator >> is known as extraction or get from operator.
• It extracts (or takes) the value from the keyboard and assigns it
to the variable on its right
cin >> sum;

cin >> var1 >> var2 >> var3;


Designing Input and Output

Enter two numbers: 10 20


the sum of 10 and 20 is equal to 30
Suggestion
In the laboratory
• As far as possible, do not develop programs for single
set of input data
After generating the output for some input data, check
whether the program should be executed again
with another set of data.
VERY IMPORTANT – Use proper indentation for better
readability of the programs.
MORE IMPORANT – Document every program
and every important statement by way of comments
Escape Sequences • Newline \n
• Horizontal tab \t
• Backslash \\
• Double quote \”

Starting newlines in output

Output:
hai
hai
hai
hai
100
Data Types
• Integer type
• Floating-point type

enter a number: 234567898989


2.34568 × 1011 2.34568e+11
enter a number: 0.0000000034567898989
3.45679× 10−09 3.45679e-09
The exponent after the e definitely must not contain a decimal point.
Some Number Types
Arithmetic Operators
Binary operators : + - * / %
 The % operator can not be applied to floating-point type

You might also like