[Solved] Why loop always run two times if character is not 'q' | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

[Solved] Why loop always run two times if character is not 'q'

anyone can explain this code of java for(int i=0;(char) system. In. Read ()! ='q';i++) System. out. println(i)

19th Nov 2021, 11:18 AM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar
3 Answers
+ 11
Gaurav Dixit 🇮🇳 this is because when you press an enter key , it's counted as two characters , then the loop will run two times and wait for another input, if you enter two characters then it will run 3 times ,and so on if you press only an enter key then it will run only one time
19th Nov 2021, 12:06 PM
Joobin Jacob ◆ ܝܘܼܒܝܼܢ ܝܹܩܲܒ ◆‎
19th Nov 2021, 12:33 PM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar