Xā€”X help please. Increment X by one | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Xā€”X help please. Increment X by one

for (int i = 1; i <= 2; i++) { x = X+1; break; } I want increment x by 2

18th Jun 2024, 4:09 AM
BlueZ
BlueZ - avatar
3 Respostas
+ 1
Why are you breaking a for loop? Also change ā€˜Xā€™ to ā€˜xā€™ for the x = X+1; part. Also tab that part. Hereā€™s a better way to do that (if you need a loop to do this): // Initialize variable x (if needed) int x = 0; // Loop that runs twice (i = 0, i = 1) for (int i = 0; i < 2; ++i) { // Increment x by 1 in each iteration ++x; } If you donā€™t need a loop do this: // Initialize variable x (if needed) int x = 0; // Increment x by 2 directly (equivalent to the loop) x += 2; Also pre-increment (++x) can be more efficient than post-increment (x++) because it avoids the overhead of creating a temporary copy of the variable. Which is why Iā€™m doing that if youā€™re curious, but some people hate that and prefer i++/x++ and for modern compilers the difference is minimal.
18th Jun 2024, 4:20 AM
Xā€”X
Xā€”X - avatar
+ 1
Thanks sir :) šŸ‘
18th Jun 2024, 4:40 AM
BlueZ
BlueZ - avatar
+ 1
Youā€™re welcome my friend. )))
18th Jun 2024, 4:41 AM
Xā€”X
Xā€”X - avatar