- 1

“Countdown” (using “while” conditions

//Code a timer that counts down to zero. #include <iostream> using namespace std; int main() { int seconds; cin>>seconds; //your code goes here while (seconds>=0); cout <<seconds <<seconds--; return 0; } //Why am I not getting this???

24th Feb 2021, 7:08 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
5 Answers
+ 4
You are printing every second twice
24th Feb 2021, 7:11 AM
Angelo
Angelo - avatar
+ 2
🎞Tahiti🍷 Semicolon (;) used to break statement so if you use after while condition then inside statement will not execute.
24th Feb 2021, 8:22 AM
A͢J
A͢J - avatar
+ 2
if 'seconds' was assigned with 5 for example, your code output 5, 4, 3, 2 and 1 (post decrement: after variable) if you've instead put decrement operator (double dash) before variable, your output would have been 4 to 0 ;)
25th Feb 2021, 4:43 AM
visph
visph - avatar
+ 1
firstly, don't put semi-colon (;) just after while condition: this will result to an infinite loop... output (cout) only seconds once and I think you must also output each number on a new line: while (seconds > 0) cout << seconds-- << endl; ... as you post decrement, you could do it "onelined", however, if you need more than one statement (many instruction semi-colon separated) you need to put a curly braces block (else block stop at first semi-colon) :P edit: if you need countdown to zero, change the condition to 'seconds >= 0'
24th Feb 2021, 2:52 PM
visph
visph - avatar
+ 1
Angelo , I Am AJ ! , visph Thank you 🙏🏽 You have no idea how helpful this has been. Such small changes/errors make such a big difference. I’m still a little confused on when to decrement with - - in front (- - seconds) vs in back (seconds- -). What would have happened if I has placed the decrement symbol after the variable? Thanks again.
25th Feb 2021, 4:40 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar