+ 2
Can you explain to me the output of this? Thanks in advance!
22 odpowiedzi
+ 7
Arnold Berendez
In a for loop you don't need to increment/decrement the variable inside the loop.
the code written
for (;;I++)
Does that
But in your code you have incremented it twice so the value will increase with 2
So output
// I = 1
// I++ = 2
2
// I = 3
// I ++ = 4
4
// I = 5
// I++ = 6
6
// I = 7 condition false loop breaks.
Hope this helps
Happy coding 😉😉
+ 5
public class Program
{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++){
i++;
System.out.println(i);
}
}
}
See, if you don't know that how code is working, do this! Take a notebook writing all the variables name go through every line and update variable if there is any change. By this you can notice that variable which you wanna see!
First value of i is '1' , then as you can see that condition is true, first block will execute then value will be updated means, first i++ which will make i = 2 then it will print and then i will update by 1 and this pattern is going on!
https://code.sololearn.com/cxyL6uAgboqC/?ref=app
+ 4
Sige, I'll explain in tagalog.
Yung output ng normal na loop is 123456 diba? and kaya nagiging 1 then 2 then 3 or in other words kaya nadadagdagan ng isa (1) yung bawat output, ay dahil sa incremention (i++), and yung ina-output natin is yung "i" itself kaya magiging 123456 sya.
Pero nagdagdag tayo ng i++ sa loob ng loop, or parang twist ganon.
Kaya for this example, yung unang value ng "i" sa loop natin is 1.
Then we increment it inside the loop (i++), so naging 2 na yung value nya, then saka palang natin sya ni-output, so first line will be 2.
Then remember that from the last loop, naging 2 yung ating "i", and for the second loop, again, incremention ulit kasi next loop na so (i++) and ngayon value na ng "i" is now 3. The repeat the process, (i++) ulit sa loob ng loop, therefore 4 na ang value ng "i" then print. Same with the next loop. Kung baga dalawa yung incremention natin (yung isa sa loop, yung isa sa body ng loop) kaya nagiging 2 steps.
>> 2
>> 4
>> 6
Kung magulo pa, feel free to ask.
+ 4
For example:
for (i=1; i<=5; i++) {
System.out.printIn(i);
}
Yung unang output nito is 1, start muna ng loop then for the next loop, saka palang incremention, and 5 yun output for last loop. (For the normal for loop).
- - - - - - - - - - - - - - - -
for (i=1; i<=5; i++) {
i++;
System.out.printIn(i);
}
Pero dito naman technically mauuna yung incremention dahil nagincrement tayo sa body ng loop, so instead na 1 yung unang output (which is the starting value), naging 2 yung ating unang output dahil nag increment tayo.
So tama ka sa statement mo na mauuna yung increment sa loob but also at the same time mahuhuli din yung incremention sa body, kaya naging 6 yung final output kasi normally yung incremention ng loop is sa second loop pa.
+ 3
for (int i = 1; i <= 5; i++){
i++;
System.out.println(i); }
This will print even numbers because:
---LOOP 1---
i = 1
i++ <--- increments therefore i = 2
>> 2
---LOOP 2 (i++ therefore i = 3)---
i = 3
i++ <--- increments therefore i = 4
>> 4
--LOOP 3 (i++ therefore i = 5)---
i = 5
i+= <--- increments therefore i = 6
>> 6
Final Output:
2
4
6
+ 3
It gets 1 and increases it to 1
1+1=2
And so on
There 2 is missed because you can easily increase your loop variable manually. It is like this
For (i=1;i <=n;i+=2)
+ 2
Thanks guys ❤️
+ 2
And by the way, kung tinanggal mo yung i++ sa loob then the output is 12345 not 123456
+ 2
gagamiting muna yung 1 so basically sa unang loop is wala munang incremention except dun sa body ng loop. Kaya value ng "i" sa unang loop is 1 then i++ (incremention ito sa body), then 2 then print.
Then for the next loop, saka palang magiincrement yung mismong loop
so start value ng "i" sa second loop is 3 (remember that the last value of "i" is 2, then increment for the second loop kaya 3),
then i++ (incremention sa body) then 4 then print.
+ 2
Arnold Berendez Nicee po, welcome po. Glad you get it!👍
+ 2
If you want a ouput like this :
1
2
3
4
5
Code should be like :
public class Program
{
public static void main(String[] args) {
for (int i = 0; i <5;){
i++;
System.out.println(i);
}
}
}
Hope you understand!
+ 2
Arnold Berendez if you use
For(int i=1; i<=9; i++)
{
i++; cout << i;
}
There is output
2468
So you dont need i++ twice
+ 1
Kuya niko taga pinas ka pala haha, pwede paki explain nlng in tagalog nalilito pakasi ako beginner lng ako dito hehe.
+ 1
Kasi pag diko nilagyan ng i++ after sa for loop yung output is 123456 pero nag experiment ako sinubukan ko lagyan ng i++ after sa for loop kaya ayun tuloy naguluhan ako.
+ 1
Ahh so una po mag e increment muna yung nasa loob ng for loop bago yung isang increment na nasa labas ng loop?
+ 1
Kuya niko na coconfused parin ako kasi diba i = 1? Tapos nag increment ng 1 sa for loop so 2 na? Tapos may ikalawang increment sa labas ng for loop eh bali 3 na? Tapos kasunod don mag priprint na kaya dapat output first is 3?
+ 1
Pagkatapos ba ng pag increment ng 1 sa loob ng for loop is didiritso na sa println? Kaya naging 2?
+ 1
Ahhh so una po gagamitin pa po yung value ng i na 1 tapos didiritso na kaagad sa increment na nasa body ng loop tapos kaya nag print ng 2?
+ 1
Kaya pagkatapos don i = 2 napo? So sa second loop don na gagamitin yung increment na nasa for loop po? Kaya 2 + 1= 3? Tapos may increment sa body ng loop kaya 4 na? And so on??
+ 1
Ahhhh gettsss konapo hahahahaah thank you so much kuya niko