CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

Unary, Compound Assignment and Ternary Operators

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn unary, binary and ternary operators, prefix and postfix increment traced line by line, compound assignment, and the ternary if-else. Prefix ++a changes the value then uses it. Postfix a++ uses the old value then changes it.

Follows the syllabus of: ICSE Class 9 Computer Applications

On screen in this lesson

Operators by number of operands

FormOperandsExample
Unaryone-a, ++a, !ok
Binarytwoa + b, a > b
Ternarythreec ? x : y

The unary operators

OperatorNameExample
+ and -unary plus, minus-5
++incrementa++ or ++a
--decrementa-- or --a
!logical NOT!true is false

Prefix vs postfix

FormRuleMemory trick
++a (prefix)change, then use++ comes first
a++ (postfix)use, then changea comes first

Pause and predict

int a = 5;
int b = a++ + ++a;
What do a and b hold at the end?

Trace: b = a++ + ++a

PartValue useda after
startnone5
a++56
++a77
b = 5 + 7127

Compound assignment

OperatorExampleMeans
+=a += 5a = a + 5
-=a -= 2a = a - 2
*=a *= 3a = a * 3
/=a /= 2a = a / 2
%=a %= 4a = a % 4

Quick answers

What does a += 5 mean?

a = a + 5. The calculation and the storing happen in one step.

For a = 5, what does b = a++ + ++a give?

b = 12 and a = 7. Work left to right and track a after each part.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Is a plus plus the same as plus plus a? Sometimes yes, and sometimes the answer changes completely. Board papers love this trap. Today we will learn unary, binary and ternary operators, trace increment and decrement line by line, and use compound assignment and the ternary operator.

An operator is a symbol that works on values, and those values are called operands. In a plus b, plus is the operator, and a and b are operands. A unary operator works on one operand, like minus a. A binary operator works on two operands, like a plus b. A ternary operator works on three operands, and Java has just one of them.

Here are the unary operators you must know. Unary minus changes the sign, so minus five is negative, and unary plus leaves it unchanged. Plus plus is increment, which adds one to a variable. Minus minus is decrement, which subtracts one. The exclamation mark is logical not, which turns true into false, and false into true.

When increment stands alone on its own line, both forms do the same job. The variable a starts at five. A plus plus makes it six. Plus plus a makes it seven. So the output is seven. The difference only appears when the increment is used inside a bigger expression.

Here is the one rule to remember. In prefix form, plus plus a, Java changes the value first, and then uses the new value. In postfix form, a plus plus, Java uses the old value first, and then changes it. The trick is to read left to right, and see which comes first, the operator or the variable.

Let us trace prefix, line by line. First, a is five. Next, b equals plus plus a. Prefix means change first, so a becomes six. Then that new value, six, is stored in b. The output is six and six.

Now postfix, with just one change. Again, a is five. This time, b equals a plus plus. Postfix means use first, so the old value, five, goes into b. Only then does a become six. The output is six and five. One small change, and b is different.

Here is a board favourite. The variable a starts at five. Then b is a plus plus, plus, plus plus a. Pause the video now, and predict what a and b hold at the end. The next slide traces it, part by part.

Now a board style question, with a starting at five. We work left to right, and keep track of a after each part. At the start, a is five. The first part is a plus plus, which uses five, and then a becomes six. The second part is plus plus a, which changes a to seven first, then uses seven. So b is five plus seven, which is twelve, and a is seven.

Let us confirm our trace by running it. Java prints seven, then twelve. It matches our table exactly. In the exam, always draw this small table, and never guess.

Decrement follows the same rule. The variable x starts at ten. First, x minus minus uses ten, then x becomes nine. Next, minus minus x changes x to eight first, and then uses eight. So y is ten minus eight, which is two. The output is eight and two.

A compound assignment operator does a calculation and stores the result in one step. Plus equals adds, so a plus equals five means a equals a plus five. Minus equals subtracts. Star equals multiplies. Slash equals divides. Percent equals stores the remainder.

Let us trace a chain of them. The variable a starts at ten. Plus equals five makes fifteen, and minus equals three makes twelve. Star equals two makes twenty four. Slash equals five makes four, because int division drops the decimal part. Percent equals three leaves the remainder, one. So it prints one.

Now Java's only ternary operator, also called the conditional operator. It is written as a condition, a question mark, one value, a colon, and another value. If the condition is true, the result is the first value. If it is false, the result is the second value. So it works like a short if else, which gives back a value.

Here m is the marks, forty, and r is the result. Read the line like a question. Is m greater than or equal to thirty three? Yes, so r gets the first value, Pass. If m were twenty, r would get Fail. One line does the work of a full if else.

Board papers also ask you to rewrite an if else using the ternary operator. Here a is forty five and b is seventy two. The question is, is a greater than b? No, so big gets the second value, b, which is seventy two. The if else version would take four lines.

Let us revise. Unary operators take one operand, binary take two, and the ternary operator takes three. Prefix increment changes the value first, then uses it. Postfix increment uses the old value first, then changes it. A plus equals five simply means a equals a plus five. And the ternary operator is a short if else that gives back a value.

Courses that teach this

CourseUnit
ICSE Class 9 Computer ApplicationsConditional Constructs in Java

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →