+ 1

Number pattern

For(int i=0;i<=5;i++){ For(int j=0;j<=i;j++){ } Sout(i); Hi all I wants to know how this for loop is working I try to search in internet but no answer so any one help me out in this Thanks in advance

29th Mar 2018, 2:57 AM
karikalan adhi
karikalan adhi - avatar
11 Réponses
+ 1
I think it works a little bit like this (it's just a theory) x is the kind of number it will output (1, 2, 3, 4, etc) and y is the amount of times it will output the number. so if x = 0 and y = 0 it will output 0 only 1 time, because both x and y are equal, but if x' value is 1, because it came from 0 to 1, it is 1 higher than y, so it will output 1 two times, and because 2 is 3 numbers higher, it will output it 4 times, but if you make the numbers negative and decrease instead of increase, it will just work the same, but it will output a "-" for the numbers, because it's negative. if you set x = 0 and y = 1, it won't output 0 , because it's 1 less than y and will be output 0 times if you set x = 0 and y = -1, it won't output -1, because x can't, but it will start outputting 0 two times, because 0 is 1 higher than -1 *edit* : I'm not sure if I'm correct, but i think I'm pretty close to how it really works
30th Mar 2018, 7:16 AM
Roel
Roel - avatar
0
at first, you forgot a "}" and maybe this will work, even if it doesn't give an output: For(int i=0;i<=5;i++){ For(int j=0;j<=i;j++){ Sout(i); } } and, tabs will help you with understanding where your mistakes are, i mean if you used this For(int i=0;i<=5;i++){ For(int j=0;j<=i;j++){ } Sout(i); you could have seen very fast that you missed a curly bracket
29th Mar 2018, 8:03 AM
Roel
Roel - avatar
0
Hi roel Ya I missed one curly bracket,but I need to know how the for loop is working here can you pls explain me.
30th Mar 2018, 5:02 AM
karikalan adhi
karikalan adhi - avatar
0
in for-loops, for has to be written without uppercase "F"
30th Mar 2018, 6:09 AM
Roel
Roel - avatar
0
this for an example (because i don't understand the function of "Sout") a fully working code where 0 is written 1 time, 1 is written 2 times and so on public class Program { public static void main(String[] args) { for(int i=0;i<=5;i++){ for(int j=0;j<=i;j++){ System.out.println(i); } } } }
30th Mar 2018, 6:11 AM
Roel
Roel - avatar
0
Yes bro but how the output is working that is what I want to know!
30th Mar 2018, 6:57 AM
karikalan adhi
karikalan adhi - avatar
0
ooh you want an explanation how it writes 0 1 1 2 2 2 3 3 3 3 etc?
30th Mar 2018, 6:58 AM
Roel
Roel - avatar
0
Yes bro I want explanation how it print like this,in which way it’s printing like that ?
30th Mar 2018, 7:13 AM
karikalan adhi
karikalan adhi - avatar
0
Really thx for the response and time to spend for me.thx for info bro.
30th Mar 2018, 7:24 AM
karikalan adhi
karikalan adhi - avatar
0
no prob, I like helping people ☺
30th Mar 2018, 7:24 AM
Roel
Roel - avatar
21st Jul 2020, 12:53 AM
shubham kumar
shubham kumar - avatar