See my qes. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

See my qes.

class HelloWorld { public static void main(String[] args) { String s="abcdef"; char c[]=s.toCharArray(); for(int i=c.length-1;i==0;i--){ System.out.println(c[i]);} } } class HelloWorld { public static void main(String[] args) { String s="abcdef"; char c[]=s.toCharArray(); for(int i=c.length-1;i==0;i--){ System.out.println(c[i]);} } } Why "for(int i=c.length-1;i==0;i--)" for loop is not working here with i==0 And working with "for(int i=c.length-1;i>=0;i--)" Why?

11th Apr 2021, 3:49 AM
Rajinder Sharma
Rajinder Sharma - avatar
3 Answers
+ 3
Knowing that <i> is length of string - 1 Condition <i> == 0 is not true. When condition evaluates to false even from the beginning, the loop doess not even get started.
11th Apr 2021, 4:03 AM
Ipang
+ 1
Thnxx Ipang
11th Apr 2021, 4:05 AM
Rajinder Sharma
Rajinder Sharma - avatar
+ 1
No problem 👌
11th Apr 2021, 4:06 AM
Ipang