+ 25

Why is 1 in the code the wrong answer and what is the right one and why ?

" The for loop" exercise is: Fill in the blanks to print even values from 0 to 20. for (int x=0; x<=20 x+=1){ cout <<x<< endl; }

17th Nov 2016, 6:28 PM
Jasmin
Jasmin - avatar
17 Réponses
+ 17
thank you 😊
17th Nov 2016, 6:37 PM
Jasmin
Jasmin - avatar
+ 16
the answer lies in "To print even values" so you have to add x+=2 to print: 2 4 6 8 10 ... up to 20
17th Nov 2016, 6:32 PM
Frédéric Charette
Frédéric Charette - avatar
+ 14
okay, yes I understand it I thought about 1, 2, 3 ... till 20 :)
17th Nov 2016, 6:36 PM
Jasmin
Jasmin - avatar
+ 10
If x+=1 is used, x is incremented by 1 in every iteration. So, every number from 0 to 20 is displayed. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 If x+=2 is used, x is incremented by 2 in every iteration. So, every even number from 0 to 20 is displayed. 0 2 4 6 8 10 12 14 16 18 20 And, semicolon is missing before incrementation.
19th Nov 2016, 3:20 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 5
your question has been answered but I thought you might also want to know that although 'x+=1' works correctly, you might want to use 'x++' in place of that because its more generally accepted and easier to type
17th Nov 2016, 8:55 PM
Cody Arthur
Cody Arthur - avatar
+ 5
need to add 2 to print even value.. x += 2 ;
18th Nov 2016, 5:05 AM
MOHAN BD
MOHAN BD - avatar
+ 4
If this was python you could use an if statement to test if modulus 2 ==0 and print it out or cout in this case. But in the case of c++ , using modulus with if statement will just add another statement.... Think of it as waste of resources.
18th Nov 2016, 6:12 AM
Cheruiyot Denis
Cheruiyot Denis - avatar
+ 2
simple is that we want even numbers that why we increment i value by 2.
24th Nov 2016, 10:23 AM
Shubham Yerpude
Shubham Yerpude - avatar
+ 1
4-2=2; 6-4=2; 8-6=2...So it's 2.
31st Jan 2017, 3:27 PM
shiromoku
shiromoku - avatar
+ 1
your for its wrong missing semicolons for (int x=0; x<=20; x+=1) { ... }
4th Apr 2017, 5:20 PM
Cosmin Voin
Cosmin Voin - avatar
0
hey jas remember that the compiler could pass some programmer's mistakes for (int x=0; x<=20 x+=1){ needs a ";" after 20 and must be like this for (int x=0; x<=20; x+=2){ that is one reason many people hates "C" language 😢 😆😆😆
19th Dec 2016, 2:26 PM
Walter Gilberto Yaxcal Chon
0
c language is more complex than other languages so carefully go through it, and put x+=2 to get your result. if(x=0;x<=20;x+=2)
25th Dec 2016, 10:26 AM
Saurav pratap
Saurav pratap - avatar
0
it says to give even numbers and ur starting value is 0 so u have to aplly +2 to get even numbers
27th Dec 2016, 5:14 PM
Abhishek Saxena
Abhishek Saxena - avatar
0
the increment should be done by 2 not 1
31st Jan 2017, 2:46 PM
sai pranav
sai pranav - avatar
- 2
I want to help u..but I can't help..because I don't know a lot of c++ ..I am really sorry
23rd Dec 2016, 3:05 AM
MEDHIR
MEDHIR - avatar
- 3
+Jasmin , did you learned web languges 1st or c++ and oop 1st ?,
3rd Dec 2016, 3:02 PM
Bashar
Bashar - avatar