+ 3

Plz help me understand this piece of code.

int x=3 while(x++<10) {x+=2} court<<x

2nd Feb 2018, 1:46 PM
FullCoder ALCHEMIST
FullCoder ALCHEMIST - avatar
4 Respuestas
+ 18
6 //loop1 , 3 <10 , 4+2=6 9 //loop2, 6 <10, 7+2 =9 12 //loop3 , 9 <10 , 10+2 =12 //13 will be the final value of x as 12 is not smaller than 10 , so loop stops but 12++ is 13 /* don't much familiar with c++ syntax , so don't know whether to put ; or not , btw mine explanation is about what will happen in loop & what will be the value of x achieved after each cycle ☺*/
2nd Feb 2018, 2:04 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 16
same i was thinking @John sir //that ; is missing & added in /**/of my answer , haha c++ is also like java or java is like c++ ☺ //btw U write lovely comments with full explanation
2nd Feb 2018, 2:15 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
You have a bunch of compile errors: 1: int x=3; // Added missing ; 2: while(x++<10) 3: {x+=2;} // Added missing ; 4: cout<<x; //Assumed court was cout add missing ; 1 x becomes 3 2 x or 3 is < 10 so execute loop after assigning 4 to x 3 add 2 to x for 6 2 x or 6 is < 10 so execute loop after assigning 7 to x 3 add 2 to x for 9 2 x or 9 is < 10 so execute loop after assigning 10 to x 3 add 2 to x for 12 2 x or 12 is > 10 so skip loop after assigning 13 to x 4 output x or 13
2nd Feb 2018, 2:11 PM
John Wells
John Wells - avatar
+ 3
The x variable of type integer (whole number) is initialized to 3. Then, a loop is entered. The condition of the loop increases the value of x by one, and compares the previous value of x (before adding the one) to the number 10. If the previous value of x is less than 10, the loop body is executed. In the loop body, the number 2 is added to the value of x. The last line redirects (<<) the value of x to the standard output stream (cout), and therefore displays it in the console.
2nd Feb 2018, 2:04 PM
SplittyDev
SplittyDev - avatar