0

Someone can please answer why it gives different result when I write System.out.println outside the body in case of for loops.

18th Apr 2020, 5:45 AM
Animesh Dwivedi
Animesh Dwivedi - avatar
8 odpowiedzi
0
can u give example
18th Apr 2020, 5:47 AM
durian
durian - avatar
0
In case of writing table of any number if we write System.out.println outside the body like in this way for{ tableOf=tableOf*i } System.out.println(tableOf); }
18th Apr 2020, 5:52 AM
Animesh Dwivedi
Animesh Dwivedi - avatar
0
You could share your code with us to be easy bring you an answer.
18th Apr 2020, 5:53 AM
Diego Fernandez
Diego Fernandez - avatar
0
public class Program { public static void main(String[] args) { int tableOf=10; for(int i=1;i<=10;i++){ tableOf*=i; } System.out.println(tableOf ); } }
18th Apr 2020, 5:56 AM
Animesh Dwivedi
Animesh Dwivedi - avatar
0
Because you are printing the statement out from the loop. You are only printing the end of the loop but no show the increment of the for.
18th Apr 2020, 6:05 AM
Diego Fernandez
Diego Fernandez - avatar
0
But in case Fibonacci numbers we have to actually write it outside from the loop
18th Apr 2020, 6:08 AM
Animesh Dwivedi
Animesh Dwivedi - avatar
0
Here there is a example of a Fibonacci numbers, the outprint is just into the loop for. public class Fibonacci { public static void main(String[] args) { int n = 10, t1 = 0, t2 = 1; System.out.print("First " + n + " terms: "); for (int i = 1; i <= n; ++i) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; } } }
18th Apr 2020, 6:12 AM
Diego Fernandez
Diego Fernandez - avatar
0
Thanks for clearing that
18th Apr 2020, 6:16 AM
Animesh Dwivedi
Animesh Dwivedi - avatar