0% found this document useful (0 votes)
3 views28 pages

Computer STKBHT

The document provides an overview of Object-Oriented Programming (OOP), explaining its core concepts such as inheritance, encapsulation, and polymorphism, along with practical examples in Java. It emphasizes the advantages of OOP, including modularity, code reuse, and effective problem-solving. Additionally, it touches on the significance of the Unicode system and Java tokens, detailing their roles in programming and character encoding.

Uploaded by

saatwikbhatia37
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)
3 views28 pages

Computer STKBHT

The document provides an overview of Object-Oriented Programming (OOP), explaining its core concepts such as inheritance, encapsulation, and polymorphism, along with practical examples in Java. It emphasizes the advantages of OOP, including modularity, code reuse, and effective problem-solving. Additionally, it touches on the significance of the Unicode system and Java tokens, detailing their roles in programming and character encoding.

Uploaded by

saatwikbhatia37
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/ 28

As the name suggests, Object-Oriented

Programming or OOPs refers to languages that


use objects in programming. Object-oriented
programming aims to implement real-world
entities like inheritance, hiding, polymorphism etc
in programming. The main aim of OOP is to bind
together the data and the functions that operate
on them so that no other part of the code can
access this data except that function. In this
article, we will understand all the concepts of
OOP’s along with an example.
Let’s assume that we have a bird class and we
are creating a list of birds. Let’s understand the
OOP’s concepts used in this bird creation.
Inheritance: For any bird, there are a set of
predefined properties which are common for all
the birds and there are a set of properties which
are specific for a particular bird. Therefore,
intuitively, we can say that all the birds inherit the
common features like wings, legs, eyes, etc.
Therefore, in the object-oriented way of
representing the birds, we first declare a bird class
with a set of properties which are common to all
the birds. By doing this, we can avoid declaring
these common properties in every bird which we
create. Instead, we can simply inherit the bird
class in all the birds which we create. The
following is an example of how the concept of
inheritance is implemented.
// Java program to demonstrate
// the bird class

// Implementing the bird class


public class Bird {

// Few properties which


// define the bird
String color;
int legs;

// Few operations which the


// bird performs
public void eat()
{
System.out.println(
"This bird has eaten");
}

public void fly()


{
System.outp.println(
"This bird is flying");
}
}
After the bird class is implemented, if we wish to
create a pigeon, then we simply inherit the above
Bird class.
Advertisement

// Java program to demonstrate the


// Inheritance

// Creating the Pigeon class which


// extends the bird class
public class Pigeon extends Bird {

// Overriding the fly method


// which makes this pigeon fly
public void fly()
{
System.out.println(
"Pigeon flys!!!!");
}
}
Encapsulation: Now, we have defined the
properties of the bird class and the attributes
which the birds have like colour, wings, legs can
be initialized by creating an object of the bird
class. However, if we simply are able to change
the properties of the bird class just by the
reference of the object, then the attributes lose the
information by which it was initially initialized.
For example, let’s say we have initially created a
pigeon with a grey colour by creating a
constructor, any user with the instance of the
object of the pigeon can change this colour to red
or black by simply referring the attribute with “this”
keyword. Therefore, in order to avoid this, we
enclose the properties in the methods. These
methods are called the getters and setters of the
attributes. The idea is to simply enclose the
initialization and retrieval of the attributes in a
method instead of directly referring the attribute
directly. This also gives an advantage because
the setters give us complete control in setting the
value to the attribute and help us to restrict the
unnecessary changes. For example, if a pigeon is
created(born) with a grey colour, it doesn’t change
until the pigeon dies. So, a user who is using
simply shouldn’t be able to change the colour as
per his wish. The following is the implementation
of the getters and setters for the above Bird class.

// Java program to demonstrate


// the bird class

// Implementing the bird class


public class Bird {

// Few properties which


// define the bird
String color;
int legs;

// Implementing the getters and


// setters for the color and legs.

public void setColor(String color)


{
this.color = color;
}

public String getColor()


{
return this.color;
}

public void setLegs(String legs)


{
this.legs = legs;
}

public String getLegs()


{
return this.legs;
}

// Few operations which the


// bird performs
public void eat()
{
System.out.println(
"This bird has eaten");
}

public void fly()


{
System.outp.println(
"This bird is flying");
}
}
Polymorphism: The word polymorphism is made
of two words poly and morph, where poly means
many and morphs means forms. In programming,
polymorphism is a feature that allows one
interface to be used for a general class of actions.
In the above concept of a bird and pigeon, a
pigeon is inherently a bird. And also, if the birds
are further categorized into multiple categories
like flying birds, flightless birds, etc. the pigeon
also fits into the flying bird’s category. And also, if
the animal class is further categorized into plant-
eating animals and meat-eating animals, the
pigeon again comes into the plant-eating animal’s
category. Therefore, the idea of polymorphism is
the ability of the same object to take multiple
forms. There are two types of polymorphism:
1. Compile Time Polymorphism: It is also
known as static polymorphism. This type of
polymorphism is achieved by function
overloading or operator overloading. It
occurs when we define multiple methods
with different signatures and the compiler
knows which method needs to be executed
based on the method signatures.
2. Run Time Polymorphism: It is also known
as Dynamic Method Dispatch. It is a
process in which a function call to the
overridden method is resolved at Runtime.
This type of polymorphism is achieved by
Method Overriding. When the same method
with the same parameters is overridden with
different contexts, the compiler doesn’t have
any idea that the method is overridden. It
simply checks if the method exists and
during the runtime, it executes the functions
which have been overridden.
In java, we can also upcast and downcast the
objects. The core idea behind this concept is also
polymorphism. The idea is that the object of the
bird can have the value of the pigeon because it
inheriting the features of the bird. Therefore, a
parent object can also be initialized with the child
properties if both the objects extend each other in
the following way:
Bird b = new Pigeon();
Object-oriented programming is such a fundamental part of software development
that it’s hard to remember a time when people used any other approach. However,
when objected-oriented programming, or OOP, first appeared in the 1980s, it was a
radical leap forward from the traditional top-down method.

These days, most major software development is performed using OOP. Thanks to
the widespread use of languages like Java and C++, you can’t develop software for
mobile unless you understand the object-oriented approach. The same goes for web
development, given the popularity of OOP languages like Python, PHP and Ruby.
That said, many developers start off with top-down languages like Visual Basic or
JavaScript.

You may be used to breaking down large problems into sub-problems and solving
them in separate units of code. Or you may have experience with functional
programming, which treats elements of code as precise mathematical functions, and
prevents them from affecting other elements — that is, no side effects. Come to grips
with OOP, however, and you’ll see that it offers a whole new way of solving
problems.

With OOP, instead of writing a program, you create classes. A class contains both
data and functions. When you want to create something in memory, you create an
object, which is an instance of that class. So, for example, you can declare a
Customer class, which holds data and functions related to customers. If you then
want your program to create a customer in memory, you create a new object of the
Customer class.

The advantages of object-oriented programming lie in this kind of encapsulation.


Here’s a look at some of OOP’s top benefits:

1. Modularity for easier troubleshooting


When working with object-oriented programming languages, you know exactly where
to look when something goes wrong. “Oh, the car object broke down? The problem
must be in the Car class!” You don’t have to go line-by-line through all your code.

That’s the beauty of encapsulation. Objects are self-contained, and each bit of
functionality does its own thing while leaving the other bits alone. Also, this
modularity allows an IT team to work on multiple objects simultaneously while
minimizing the chance that one person might duplicate someone else’s functionality.
2. Reuse of code through inheritance
Suppose that in addition to your Car object, one colleague needs a RaceCar object,
and another needs a Limousine object. Everyone builds their objects separately but
discover commonalities between them. In fact, each object is just a different kind of
Car. This is where the inheritance technique saves time: Create one generic class
(Car), and then define the subclasses (RaceCar and Limousine) that are to inherit
the generic class’s traits.

Of course, Limousine and RaceCar still have their unique attributes and functions. If
the RaceCar object needs a method to “fireAfterBurners” and the Limousine object
requires a Chauffeur, each class could implement separate functions just for itself.
However, because both classes inherit key aspects from the Car class, for example
the “drive” or “fillUpGas” methods, your inheriting classes can simply reuse existing
code instead of writing these functions all over again.

What if you want to make a change to all Car objects, regardless of type? This is
another advantage of the OOP approach. Make a change to your Car class, and all
car objects will simply inherit the new code.

3. Flexibility through polymorphism


Riffing on this example, you now need just a few drivers, or functions, like “driveCar,”
driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with
LimousineDrivers, but other things, like RaceHelmets and BeverageSponsorships,
are unique.

This is where object-oriented programming’s polymorphism comes into play.


Because a single function can shape-shift to adapt to whichever class it’s in, you
could create one function in the parent Car class called “drive” — not “driveCar” or
“driveRaceCar,” but just “drive.” This one function would work with the
RaceCarDriver, LimousineDriver and so on. In fact, you could even have
“raceCar.drive(myRaceCarDriver)” or “limo.drive(myChauffeur).”

4. Effective problem solving


Many people avoid learning OOP because the learning curve seems steeper than
that for top-down programming. But take the time to master OOP and you’ll find it’s
the easier, more intuitive approach for developing big projects.
Object-oriented programming is ultimately about taking a huge problem and breaking
it down to solvable chunks. For each mini-problem, you write a class that does what
you require. And then — best of all — you can reuse those classes, which makes it
even quicker to solve the next problem.

This isn’t to say that OOP is the only way to write software. But there’s a reason that
languages like C++, C# and Java are the go-to options for serious software
development.
Unicode System
Unicode provides a unique number for every character, no matter what the platform,
no matter what the language, no matter what the program. Example – A = U + 0041,
B = U + 0042, C = U + 0043, D = U + 0044.
The Unicode system has been adopted by such industry leaders like Apple, HP, IBM,
Just Systems, Microsoft, Oracle and many others. Unicode is required by modern
standards such as XML, JAVA, ECMAScript (JavaScript), COBRA 3.0, WML, LDAP
etc., and is the official way to implement ISO/IEC 10646. It is supported in many
Operating Systems, all modern browsers, and many other products. The emergence
of the Unicode standard, and the availability of tools supporting it, are among the
most significant recent global software technology trends.
Incorporating Unicode into client-server or multi-tiered applications and websites
offers significant cost savings over the use of legacy character sets. Unicode enables
a single software product or a single website to be targeted across multiple
platforms, languages and countries without re-engineering. It allows data to be
transported through many different systems without corruption.
Unicode and ASCII code are both ways that computer languages store characters as
numbers. ASCII stands for “American Standard Code for Information Interchange”
and it allows encoding for 128 characters. This is fine for English language, but not
enough for others. Unicode can handle 100,000 characters, so by using this encoding
scheme, JAVA allows programmers to work with printed languages from around the
world.

Why we need Unicode?


Before Unicode was invented, there were hundreds of different encoding systems
for assigning this number. No single encoding could contain enough characters: For
example, the European Union alone requires several different encodings to cover all
its languages. Even for a single language like English no single encoding was
adequate for all the letters, punctuation, and technical symbols in common use.
These encoding systems also collide with one another. That is, two encodings can
use the same number for two different characters, or use different numbers for the
same character. Any given computer (especially servers) needs to support many
different encodings; yet whenever data is passed between different encodings or
platforms, the data always runs the risk of corruption.
Java Tokens
In Java, the program contains classes and methods. Further, the methods contain the
expressions and statements required to perform a specific operation. These statements
and expressions are made up of tokens. In other words, we can say that the expression
and statement is a set of tokens. The tokens are the small building blocks of a Java
program that are meaningful to the Java compiler. Further, these two components
contain variables, constants, and operators. In this section, we will discuss what is
tokens in Java.

What is token in Java?


The Java compiler breaks the line of code into text (words) is called Java tokens. These
are the smallest element of the Java program. The Java compiler identified these words
as tokens. These tokens are separated by the delimiters. It is useful for compilers to
detect errors. Remember that the delimiters are not part of the Java tokens.

1. token <= identifier | keyword | separator | operator | literal | comment


For example, consider the following code.

1. public class Demo


2. {
3. public static void main(String args[])
4. {
5. System.out.println("javatpoint");
6. }
7. }

In the above code snippet, public, class, Demo, {, static, void, main, (, String, args,
[, ], ), System, ., out, println, javatpoint, etc. are the Java tokens.

Play Video

The Java compiler translates these tokens into Java bytecode. Further, these bytecodes
are executed inside the interpreted Java environment.
Literals, Identifiers, Punctuators,
Separators, Operators in Java
Types of Tokens
Java token includes the following:

o Keywords
o Identifiers
o Literals
o Operators
o Separators
o Comments

Keywords: These are the pre-defined reserved words of any programming language.
Each keyword has a special meaning. It is always written in lower case. Java provides
the following keywords:

01. abstract 02. Boolean 03. byte 04. break 05. class

06. case 07. catch 08. char 09. continue 10. default

11. do 12. double 13. else 14. extends 15. final

16. finally 17. float 18. for 19. if 20. implements

21. import 22. instanceof 23. int 24. interface 25. long

26. native 27. new 28. package 29. private 30. protected

31. public 32. return 33. short 34. static 35. super

36. switch 37. synchronized 38. this 39. thro 40. throws
41. transient 42. try 43. void 44. volatile 45. while

46. assert 47. const 48. enum 49. goto 50. strictfp

Identifier: Identifiers are used to name a variable, constant, function, class, and array.
It usually defined by the user. It uses letters, underscores, or a dollar sign as the first
character. The label is also known as a special kind of identifier that is used in the goto
statement. Remember that the identifier name must be different from the reserved
keywords. There are some rules to declare identifiers are:

o The first letter of an identifier must be a letter, underscore or a dollar sign. It cannot
start with digits but may contain digits.
o The whitespace cannot be included in the identifier.
o Identifiers are case sensitive.

Some valid identifiers are:

1. PhoneNumber
2. PRICE
3. radius
4. a
5. a1
6. _phonenumber
7. $circumference
8. jagged_array
9. 12radius //invalid

Literals: In programming literal is a notation that represents a fixed value (constant)


in the source code. It can be categorized as an integer literal, string literal, Boolean
literal, etc. It is defined by the programmer. Once it has been defined cannot be
changed. Java provides five types of literals are as follows:

o Integer
o Floating Point
o Character
o String
o Boolean
Literal Type

23 int

9.86 double

false, true boolean

'K', '7', '-' char

"javatpoint" String

Null any reference type

Operators: In programming, operators are the special symbol that tells the compiler
to perform a special operation. Java provides different types of operators that can be
classified according to the functionality they provide. There are eight types
of operators in Java, are as follows:

o Arithmetic Operators
o Assignment Operators
o Relational Operators
o Unary Operators
o Logical Operators
o Ternary Operators
o Bitwise Operators
o Shift Operators

Operator Symbols

Arithmetic +,-,/,*,%

Unary ++ , - - , !

Assignment = , += , -= , *= , /= , %= , ^=
Relational ==, != , < , >, <= , >=

Logical && , ||

Ternary (Condition) ? (Statement1) : (Statement2);

Bitwise &,|,^,~

Shift << , >> , >>>

Separators: The separators in Java is also known as punctuators. There are nine
separators in Java, are as follows:

Pause

Unmute

1. separator <= ; | , | . | ( | ) | { | } | [ | ]
Note that the first three separators (; , and .) are tokens that separate other tokens,
and the last six (3 pairs of braces) separators are also known as delimiters. For
example, Math.pow(9, 3); contains nine tokens.
o Square Brackets []: It is used to define array elements. A pair of square brackets
represents the single-dimensional array, two pairs of square brackets represent the
two-dimensional array.
o Parentheses (): It is used to call the functions and parsing the parameters.
o Curly Braces {}: The curly braces denote the starting and ending of a code block.
o Comma (,): It is used to separate two values, statements, and parameters.
o Assignment Operator (=): It is used to assign a variable and constant.
o Semicolon (;): It is the symbol that can be found at end of the statements. It separates
the two statements.
o Period (.): It separates the package name form the sub-packages and class. It also
separates a variable or method from a reference variable.

Comments: Comments allow us to specify information about the program inside our
Java code. Java compiler recognizes these comments as tokens but excludes it form
further processing. The Java compiler treats comments as whitespaces. Java provides
the following two types of comments:

o Line Oriented: It begins with a pair of forwarding slashes (//).


o Block-Oriented: It begins with /* and continues until it founds */.
Data Types and Sizes

D provides fundamental data types for integers and floating-point constants.


Arithmetic may only be performed on integers in D programs. Floating-point
constants may be used to initialize data structures, but floating-point arithmetic is
not permitted in D. D provides a 32-bit and 64-bit data model for use in writing
programs. The data model used when executing your program is the native data
model associated with the active operating system kernel. You can determine the
native data model for your system using isainfo -b.

The names of the integer types and their sizes in each of the two data models are
shown in the following table. Integers are always represented in twos-complement
form in the native byte-encoding order of your system.

Table 2–2 D Integer Data Types

Type Name 32–bit Size 64–bit Size

char 1 byte 1 byte

short 2 bytes 2 bytes

int 4 bytes 4 bytes

long 4 bytes 8 bytes

long long 8 bytes 8 bytes

Integer types may be prefixed with the signed or unsigned qualifier. If no sign
qualifier is present, the type is assumed to be signed. The D compiler also provides
the type aliases listed in the following table:

Table 2–3 D Integer Type Aliases

Type Name Description

int8_t 1 byte signed integer

int16_t 2 byte signed integer

int32_t 4 byte signed integer

int64_t 8 byte signed integer


Type Name Description

intptr_t Signed integer of size equal to a pointer

uint8_t 1 byte unsigned integer

uint16_t 2 byte unsigned integer

uint32_t 4 byte unsigned integer

uint64_t 8 byte unsigned integer

uintptr_t Unsigned integer of size equal to a pointer

These type aliases are equivalent to using the name of the corresponding base type
in the previous table and are appropriately defined for each data model. For
example, the type name uint8_t is an alias for the type unsigned char.
See Chapter 8, Type and Constant Definitions for information on how to define
your own type aliases for use in your D programs.

D provides floating-point types for compatibility with ANSI-C declarations and


types. Floating-point operators are not supported in D, but floating-point data
objects can be traced and formatted using the printf() function. The floating-point
types listed in the following table may be used:

Table 2–4 D Floating-Point Data Types

Type Name 32–bit Size 64–bit Size

float 4 bytes 4 bytes

double 8 bytes 8 bytes

long double 16 bytes 16 bytes

D also provides the special type string to represent ASCII strings. Strings are
discussed in more detail in Chapter 6, Strings.

• Previous: Identifier Names and Keywords


• Next: Constants

• © 2010, Oracle Corporation and/or its affiliates


Increment, Decrement operators, Dot
Operators, Assignment Operators

increment and decrement operators :


Increment and decrement operators are unary operators. We can only apply
these operators on a single operand, hence these operators are called as unary
operators.

we can apply these unary operators on all primitive types except Boolean.

Increment operator (++):


the increment operator is an operator which is used to increase the value of a
variable by 1, on which it is applied.

Again these increment operators are two types:

• Pre increment (++x)


• Post increment (x++)

Pre Increment Operator:


If an Increment operator is used in front of an operand, then it is called as Pre
Increment operator.

Syntax:

++x : which increments the value by 1 of ‘x’ variable.


Example:

class PreIncrement {

public static void main(String[] args) {


int x = 10;
int y = ++x;
System.out.println("y value is: " + y);
}

}
Output:

y value is: 11
On the above example, pre increment operator is applied on x operand, here
first the value of x will be incremented by 1 and then the incremented value will
be assigned to the variable y .
As per example, the initial value of ‘x’ is 10. After applying pre-increment
operator on ‘x’ the value of ‘x’ is incremented by 1 (i.e., 11) and that value is
assigned to the variable ‘y’. So that the final value of ‘y’ is 11.
Post Increment Operator:
If an Increment operator is used after an operand, then is called Post Increment
operator.

Syntax:

x++ : which increase the value by 1 of variable ‘x’.


Example:

class PostIncrement {

public static void main(String[] args) {


int x = 10;
int y = x++;
System.out.println("y value is: " + y);
}

}
Output:

y value is: 10
Post increment operator is applied on ‘x’, here the case is exact opposite of pre
increment, first the value of variable ‘x’ is assigned to the variable ‘y’ and then
the value of ‘x’ is incremented by 1 .

As per example, the initial value of ‘x’ is 10. After applying post-increment
operator the current values of ‘x’ (i.e, 10) is assigned to y, and then the value
of ‘x’ is incremented by 1. So when displaying variable ‘y’ it is showing as 10.

Decrement Operator (- -):


The Decrement operator is an operator which is used to decrease the value of
the variable by 1, on which it is applied.

Like increment operators, decrement operators are also 2 types,

• Pre decrement (- -x)


• Post decrement (x- -)

Pre Decrement Operator:


If a decrement operator is used in front of an operand, then it is called Pre
decrement operator.

Syntax:

–x : which decrease the value by 1 of variable ‘x’ .


Example:

class PreDecrement {
public static void main(String[] args) {
int x = 10;
int y = --x;
System.out.println("y value is: " + y);
}
}
Output:

y value is: 9
Pre decrement operator is applied on ‘x’, first, the value of ‘x’ will be
decremented by 1 and then the decremented value will be assigned to the
variable ‘y’.

As per example, the initial value of ‘x’ is 10. After applying pre decrement
operator on ‘x’, the value of ‘x’ is decremented by 1 (i.e., 9) and that value is
assigned to the variable ‘y’. So, when we display the variable ‘y’ it is showing as
9.

Post Decrement Operator:


If a decrement operator is used after an operand, then it is called Post
decrement operator.

x- – : which decrease the value by 1 of variable ‘x’ .


Example:
class PostDecrement {
public static void main(String[] args) {
int x = 10;
int y = x--;
System.out.println("y value is: " + y);
}
}
Output:

y value is: 10
Post decrement operator is applied on ‘x’, here the case is the complete
opposite of pre decrement operator, first, the value of variable ‘x’ is assigned to
the variable ‘y’ and then the value of ‘x’ is decremented by 1.
Syntax:
As per example, the initial value of ‘x’ is 10. After applying post decrement
operator on variable ‘x’ the current values of ‘x’ (i.e, 10) is assigned to ‘y’, and
then the value of ‘x’ is decremented by 1. So when displaying the value of ‘y’ it is
showing as 10.

Limitations of Increment and Decrement Operators:


• We can apply Increment and decrement operators only for variables but
not for constant values. If we apply, then we will get compile time error.

class IncAndDecOperatorsDemo {

public static void main(String[] args) {


int x = 10;
int y = ++10;
System.out.println("Value of y : " + y);

• We can’t apply the nesting on increment and decrement operators.

class IncAndDecOperatorsDemo {
public static void main(String[] args) {
int x = 100;
int y = --(--x);
System.out.println("Value of y : " + y);

• We can’t apply increment and decrement operators on final variables.

class IncAndDecOperatorsDemo {

public static void main(String[] args) {


final int x = 100;
int y = ++x;
System.out.println("Value of y : " + y);

• We can’t apply increment and decrement operators on boolean types.

class IncAndDecOperatorsDemo {

public static void main(String[] args) {


boolean b = true;
--b;
System.out.println(b);

Examples on Increment and Decrement Operators:


Example 1:
class IncrementOperator {

public static void main(String[] args) {


int x = 10;
x = x++;
x = x++;
x = x++;
x = x++;
x = x++;
System.out.println("The Value of x is : " + x);

}
Output:

Value of x : 10
The Step by Step Analysis on Output:

STEP 1 : Initial value of ‘x’ = 10;

STEP 2 : The value of ‘x’ is post incremented and assigned again to ‘x’.

The variable ‘x’ will be incremented first but the previous ‘x’ value (10) is
assigned again to ‘x’ variable, and the incremented (11) value will be used after
assigning. But in this example, the next value of ‘x’ is overridden by previous
value (10) always.

STEP 3: The value of ‘x’ is post incremented and assigned to ‘x’ only.

STEP 4: The value of ‘x’ is post incremented and assigned to ‘x’ only.

STEP 5: The value of ‘x’ is post incremented and assigned to ‘x’ only.

Example 2:
class IncrementOperator {

public static void main(String[] args) {


int x = 1;
x = x++ + ++x + x++ + ++x + ++x;
System.out.println("Value of x : " + x);

}
Output:

Value of x : 18
The Step by Step Analysis on Output:

STEP 1 : initial ‘x’ value is 1

STEP 2 : x++ (1)

STEP 3 : ++x (2+1=3)

STEP 4 : x++ (3+1 (Post increment) =3)

STEP 5 : ++x (4+1 = 5)

STEP 6 : ++x (5+1=6)

STEP 7 : Add values from STEP 2 to STEP 6 (1+3+3+5+6)

STEP 8 : Answer is18

You might also like