0% found this document useful (0 votes)
26 views14 pages

Unit II+-c++

This document provides an overview of fundamental concepts in C++, including tokens, keywords, identifiers, constants, data types, variables, operators, and control structures. It details the syntax and usage of various programming constructs such as if statements, switch statements, loops (while, do-while, for), and the different types of operators. Additionally, it explains the rules for naming identifiers and the significance of constants in programming.

Uploaded by

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

Unit II+-c++

This document provides an overview of fundamental concepts in C++, including tokens, keywords, identifiers, constants, data types, variables, operators, and control structures. It details the syntax and usage of various programming constructs such as if statements, switch statements, loops (while, do-while, for), and the different types of operators. Additionally, it explains the rules for naming identifiers and the significance of constants in programming.

Uploaded by

sangita bhusare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit II: Periods: 15

 Introduction to Tokens,
 Keywords,
 Identifiers & Constants,
 Basic Data Types,
 Variables
 Operators in C++,
 Decision Control & Loop Control Structures: If, If-else, Nested If, Else-if
 ladder, switch, goto Statement, break statement, while, do-while, for loop

Introduction to Tokens
The smallest individual units in program are known as tokens. C++ has the following tokens.
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators

C++ keywords
When a language is defined, one has to design a set of instructions to be used for communicating with
the computer to carry out specific operations. The set of instructions which are used in programming,
are called keywords. These are also known as reserved words of the language. They have a specific
meaning for the C++ compiler and should be used for giving specific instructions to the computer.
These words cannot be used for any other purpose, such as naming a variable. C++ is a case-sensitive
language, and it requires that all keywords be in lowercase. C++ keywords are:
Identifiers
An identifier is a name assigned to a function, variable, or any other user-defined item. Identifiers can
be from one to several characters long.

Rules for naming identifiers:


 Variable names can start with any letter of the alphabet or an underscore. Next comes a letter,
a digit, or an underscore.

 Uppercase and lowercase are distinct.

 C++ keywords cannot be used as identifier.

Constants
Constants refer to fixed values that the program cannot alter. Constants can be of any of the basic data
types. The way each constant is represented depends upon its type. Constants are also called literals.
We can use keyword const prefix to declare constants with a specific type as follows:

const type variableName = value;


e.g,
const int LENGTH = 10;
Basic Data types
Data type defines size and type of values that a variable can store along with the set of operations that
can be performed on that variable. C++ provides built-in data types that correspond to integers,
characters, floating-point values, and Boolean values. There are the seven basic data types in C++ as
shown below:

Type Meaning
char(character) holds 8-bit ASCII characters
wchar_t(Wide character) holds characters that are part of large character sets
int(Integer) represent integer numbers having no fractional part
float(floating point) stores real numbers in the range of about 3.4x10–38to
3.4x10 38,with a precision of seven digits.

double(Double floating point) Stores real numbers in the range from 1.7x10
–308 to1.7x10308 with a precision of 15 digits.
bool(Boolean) can have only two possible values: true and false.
Void Valueless

C++ allows certain of the basic types to have modifiers preceding them. A modifier alters the
meaning of the base type so that it more precisely fits the needs of various situations. The data type
modifiers are: signed, unsigned, long and short

This figure shows all combinations of the basic data types and modifiers along with their size and
range for a 16-bit word machine.
Variable
A variable is a named area in memory used to store values during program execution. Variables are
run time entities. A variable has a symbolic name and can be given a variety of values. When a
variable is given a value, that value is actually placed in the memory space assigned to the variable.
All variables must be declared before they can be used. The general form of a declaration is:

type variable_list;

Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more
identifier names separated by commas. Here are some declarations:

int i,j,l;
short int si;
unsigned int ui;

double balance, profit, loss;

Operator in C++
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C++ is rich in built-in operators. Generally, there are six type of operators:
Arithmetical operators, Relational operators, Logical operators, Assignment operators, Conditional
operators, Comma operator.

Arithmetical operators
Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation.

Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

You can use the operators +, -, *, and / with both integral and floating-point data types. Modulus or
remainder % operator is used only with the integral data type.
Relational operators
The relational operators are used to test the relation between two values. All relational operators are
binary operators and therefore require two operands. A relational expression returns zero when the
relation is false and a non-zero when it is true. The following table shows the relational operators.

Relational Operators Meaning

< Less than

<= Less than or equal to

== Equal to

> Greater than

>= Greater than or equal to

!= Not equal to

Logical operators
The logical operators are used to combine one or more relational expression. The logical operators are

Operators Meaning

|| OR

&& AND

! NOT
Assignment operator
The assignment operator '=' is used for assigning a variable to a value. This operator takes the
expression on its right-hand-side and places it into the variable on its left-hand-side. For example:

m = 5;

The operator takes the expression on the right, 5, and stores it in the variable on the left, m.
x = y = z = 32;
This code stores the value 32 in each of the three variables x, y, and z. In addition to standard
assignment operator shown above, C++ also support compound assignment operators.

Compound Assignment Operators


Operator Example Equivalent to
+= A+=2 A=A+2
-= A-=2 A=A–2
%= A%=2 A=A%2
/= A/ = 2 A=A/2
*= A*=2 A=A*2

Increment and Decrement Operators

C++ provides two special operators viz '++' and '--' for incrementing and decrementing the value of a
variable by 1. The increment/decrement operator can be used with any type of variable but it cannot
be used with any constant. Increment and decrement operators each have two forms, pre and post.

The syntax of the increment operator is:


Pre-increment: ++variable
Post-increment: variable++
The syntax of the decrement operator is:

Pre-decrement: ––variable
Post-decrement: variable––

In Prefix form first variable is first incremented/decremented, then evaluated


In Postfix form first variable is first evaluated, then incremented / decremented.

Conditional operator
The conditional operator ?: is called ternary operator as it requires three operands. The format of the
conditional operator is :

Conditional_ expression ? expression1 : expression2;


If the value of conditional expression is true then the expression1 is evaluated, otherwise expression2
is evaluated.

int a = 5, b = 6;

big = (a > b) ? a : b;
The condition evaluates to false, therefore big gets the value from b and it becomes 6.

The comma operator


The comma operator gives left to right evaluation of expressions. When the set of expressions has to
be evaluated for a value, only the rightmost expression is considered.

int a = 1, b = 2, c = 3, i; // comma acts as separator, not as an operator

i = (a, b); // stores b into i would first assign the value of a to i, and then assign value of b to variable i.
So, at the end, variable i would contain the value 2.

The sizeof operator


The sizeof operator can be used to find how many bytes are required for an object to store in memory.
For example

sizeof (char) returns 1


sizeof (float) returns 4

Control Structures

Control structures allows to control the flow of program’s execution based on certain conditions C++
supports following basic control structures:

1) Selection Control structure

2) Loop Control structure


1) Selection Control structure:
Selection Control structures allows to control the flow of program’s execution depending upon the
state of a particular condition being true or false .C++ supports two types of selection statements :if
and switch. Condition operator (?:) can also be used as an alternative to the if statement.

A.1) If Statement:
The syntax of an if statement in C++ is:

if(condition)
{
// statement(s) will execute if the condition is true
}
If the condition evaluates to true, then the block of code inside the if statement will be executed. If it
evaluates to false, then the first set of code after the end of the if statement (after the closing curly
brace) will be executed.

Flowchart showing working of if statement

// A Program to find whether a given number is even or odd using if … else statement
#include<iostream.h>

#include<conio.h>
int main()

{ int n;

cout<<”enter number”;
cin>>n;

if(n%2==0)
cout<<”Even number”;
else

cout<<”Odd number”;
return 0;

A.2) The if...else Statement


The syntax is shown as:
if(condition){
// statement(s) will execute if the condition is true
}
else{
// statement(s) will execute if condition is false
}
If the condition evaluates to true, then the if block of code will be executed, otherwise else block of
code will be executed.

Flowchart

A.3) if...else if...else Statement


An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement.

The Syntax is shown as:


if(condition 1){
// Executes when the condition 1 is true
}
else if(condition 2){
// Executes when the condition 2 is true
}
else if(condition 3){
// Executes when the condition 3 is true
}
else {
// executes when the none of the above condition is true.
}

A.4) Nested if Statement


It is always legal to nest if-else statements, which means you can use one if or
else if statement inside another if or else if statement(s).
The syntax for a nested if statement is as follows:
if( condition 1){
// Executes when the condition 1 is true
if(condition 2){
// Executes when the condition 2 is true
}
}
B) Switch
C++ has a built-in multiple-branch selection statement, called switch, which successively tests the
value of an expression against a list of integer or character constants. When a match is found, the
statements associated with that constant are executed. The general form of the switch statement is:

switch (expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
default
statement sequence
}
The expression must evaluate to a character or integer value. Floating-point expressions, for example,
are not allowed. The value of expression is tested, in order, against the values of the constants
specified in the case statements. When a match is found, the statement sequence associated with that
case is executed until the break statement or the end of the switch statement is reached. The default
statement is executed if no matches are found. The default is optional and, if it is not present, no
action takes place if all matches fail.

The break statement is one of C++'s jump statements. You can use it in loops as well as in the switch
statement. When break is encountered in a switch, program execution "jumps" to the line of code
following the switch statement.

Flowchart
2) Loop control structures
A loop statement allows us to execute a statement or group of statements multiple times. Loops or
iterative statements tell the program to repeat a fragment of code several times or as long as a certain
condition holds. C++ provides three convenient iterative statements: while, for, and do-while.

While loop
A while loop statement repeatedly executes a target statement as long as a given condition is true. It is
an entry-controlled loop.

The syntax of a while loop in C++ is:

while(condition){
statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any non-zero value. The loop iterates while the condition is true. After each
execution of the loop, the value of test expression is changed. When the condition becomes false,
program control passes to the line immediately following the loop.

Flowchart

// A program to display numbers from 1 to 100


#include<iostream.h>

#include<conio.h>
int main(){

int i=1;
while(i<=100){
cout<<i ;

i++;

return 0;

}
The do-while Loop
The do-while loop differs from the while loop in that the condition is tested after the body of the
loop. This assures that the program goes through the iteration at least once. It is an exit-controlled
loop.

do{ stat
ement(s)
;
}while( condition );

The conditional expression appears at the end of the loop, so the statement(s) in the loop execute
once before the condition is tested. If the condition is true, the flow of control jumps back up to
do, and the statement(s) in the loop execute again. This process repeats until the given condition
becomes false.

// A program to display numbers from 1


to 100 #include<iostream.h>

#include<conio
.h> int main( )
{

int
i=1
;
do

{cout<<i ; i+
+;

}
while(i<=10
0); return 0;

}
For Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times. The syntax of a for loop in C++ is:

for ( init; condition;


increment ){ statement(s);
}

Here is the flow of control in a for loop:

1. The init step is executed first, and only once. This step allows you to declare and initialize any
loop control variables. You are not required to put a statement here, as long as a semicolon
appears.

2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the
body of the loop does not execute and flow of control jumps to the next statement just after the
for loop.

3. After the body of the for loop executes, the flow of control jumps back up to the increment
statement. This statement allows you to update any C++ loop control variables. This statement
can be left blank, as long as a semicolon appears after the condition.

4. The condition is now evaluated again. If it is true, the loop executes and the process repeats
itself (body of loop, then increment step, and then again condition). After the condition
becomes false, the for loop terminates.

Flow Diagram

// A program to display numbers from 1 to


100 #include<iostream.h>
#include<coni
o.h> int
main() {
int i ;

for (i=1;i<=100;i++)

cout
<<i ;
retur
n 0;
}

You might also like