Vitaliy ◀️TriΔng3l▶️
and match the a, b and c with what's written in your code
Anonymous
Otumian
Check again.. the month
Cesco
Anonymous
Vitaliy ◀️TriΔng3l▶️
not the way you should actually write it
Cesco
Otumian
Month isn't incremented
Vitaliy ◀️TriΔng3l▶️
for(int x=0;month>=0;x++)
What happens in the beginning and at every iteration in this kind of a loop?
Otumian
You keep increasing x but x is not used in the loop condition
Otumian
Month is used rather,
Anonymous
Anonymous
Vitaliy ◀️TriΔng3l▶️
(Also you have an unconditional return inside the loop, so… the loop behaves pretty much like if (month >= 0) {…; return 0;})
Anonymous
And guys to be clear this isn't an assignment
Anonymous
Anonymous
Vitaliy ◀️TriΔng3l▶️
Really?
Run the code in your mind, statement by statement, actually executing every iteration of the loop
Anonymous
Anonymous
Anonymous
Vitaliy ◀️TriΔng3l▶️
For loops are a bit confusing
for (a; b; c) {
stuff();
}
is roughly:
{
a;
while (b) {
stuff();
c; // this is also executed whenever you do "continue" in the for loop
}
}
which is also roughly:
{
a;
nextIteration:
if (b) {
stuff();
// these are done whenever you do "continue" in the for loop
c;
goto nextIteration;
}
}
Anonymous
So I'm running a pointless loop
Vitaliy ◀️TriΔng3l▶️
now try running your code as if it was the last example
Vitaliy ◀️TriΔng3l▶️
if the user has entered month < 0, the loop isn't done
Anonymous
Vitaliy ◀️TriΔng3l▶️
if month >= 0, one iteration is done, which includes a return, which terminates the whole program since it's in main
Anonymous
Vitaliy ◀️TriΔng3l▶️
(one iteration with x being 0)
Anonymous
I'm not getting it to be honest. Can you tell me somewhere I can read about this properly?
Vitaliy ◀️TriΔng3l▶️
Anyway, commonly for loops, when used for iteration over some elements, are written as
for (int index = 0; index < count; ++index) (or index++, doesn't matter in this specific case, at least until you get to operator overloading)
Vitaliy ◀️TriΔng3l▶️
so if count is, for example, 5
Anonymous
Or basically after the first iteration which is 0 it basically terminates the whole code?
Vitaliy ◀️TriΔng3l▶️
the first iteration will be done, with index = 0, which is < 5, and at the end of it, index will be incremented
the second iteration will be done, with index = 1, which is < 5, and at the end of it, index will be incremented
the third iteration will be done, with index = 2, which is < 5, and at the end of it, index will be incremented
the fourth iteration will be done, with index = 3, which is < 5, and at the end of it, index will be incremented
the fifth iteration will be done, with index = 4, which is < 5, and at the end of it, index will be incremented
the sixth iteration will not be done, because 5 is not < 5
Vitaliy ◀️TriΔng3l▶️
Anonymous
Vitaliy ◀️TriΔng3l▶️
return exits the current function and returns to the place after where it was called (and also sending a value to it if needed) — and since it's main, it also exits the program
Anonymous
Vitaliy ◀️TriΔng3l▶️
Anonymous
I must be dumb
Vitaliy ◀️TriΔng3l▶️
I was talking about "while" as a more straightforward and structured way of understanding what "for" is doing
Vitaliy ◀️TriΔng3l▶️
just for you to be able to "compile and run" the code in your brain, statement by statement
Anonymous
Anonymous
Anonymous
One thing I realised was that I need to make a condition where negative numbers are not allowed to use
Anonymous
Secondly maybe I should use month in replacement of x
Anonymous
Thirdly my whole program is all messed up
Anonymous
Anonymous
/warn
Vitaliy ◀️TriΔng3l▶️
Anonymous
Vitaliy ◀️TriΔng3l▶️
if/else, goto, switch/case/default, while, for, do/while, continue, break, return, function calls
Anonymous
Anonymous
Anonymous
https://en.cppreference.com/w/cpp/chrono/year_month
Anonymous
Anonymous
Anonymous
Anonymous
np
DARONG
ok
Gaurav
Param is a course instructor of
c++ programming and currently
he is teaching conversions to his
students. He started with the
basic programs and now he
want to introduce some
complex problems. Before
starting the complex problems,
he wants to start with high
difficulty level problem, such as:
counting the digits of a number.
Help Mr. param to write a
program which will accept one
integer argument and returns
count of digits of that argument
using class to basic conversion.
Gaurav
Andrew
Gaurav
Tell me first
Gaurav
All are silent
Gaurav
Reply me
klimi
Reply me
to discuss C/C++ topics like it is written in rules, c:
PO
Hi
I would like to know if there is anyone who has worked with Gtk+2.0 in c programming?!
Gaurav