What to do to increment by 2 instead of 1 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What to do to increment by 2 instead of 1 ?

public class Program { public static void main(String[] args) { int test = 5; ++ test; System.out.println(test); } } what should I do to get the output 7 instead of 6?

8th Sep 2018, 4:44 AM
kp kalia
kp kalia - avatar
8 Answers
+ 23
you can replace ++test; with test+=2; it will do the job done for you
8th Sep 2018, 4:46 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 11
use ++test ; ++test; OR test + = 2; OR test = test + 2; that your choice Now
9th Sep 2018, 3:54 PM
The Shiva
The Shiva - avatar
+ 10
You have written ++test. Here ++operator increments the value of test by 1 .You can also write like this test += 1; it means test = test+1; For incrementing the value of test by 2,3 or by any number you just need to write how much times you want to increment it . For 2 you should write test+=2.
9th Sep 2018, 3:17 AM
Purab Gupta
Purab Gupta - avatar
+ 5
for doing so remove the line ++ test and in place of it place test =test+2; or test+=2;
9th Sep 2018, 6:29 AM
Vivek Mishra
Vivek Mishra - avatar
+ 2
you can use ++ + ++ pre increment by 2 num=num+2
21st Sep 2018, 11:57 AM
Keahav Keyal
Keahav Keyal - avatar
+ 2
int test = 5; text += 2; Print out test it will give u 7
13th Oct 2018, 6:46 AM
Opayele Hammed Gbolagade
Opayele Hammed Gbolagade - avatar
+ 2
Use test+=2; Or the number of steps instead of 2
26th Nov 2018, 5:35 PM
Madhusudan Babar
Madhusudan Babar - avatar
+ 1
another cool way: use ++++test;
14th Sep 2018, 12:44 PM
Anton Savchenko
Anton Savchenko - avatar