Vitaliy ◀️TriΔng3l▶️
Ooh. So like using an if statement
I mean, what would for (a; b; c) {} look like if it was written via while or goto instead?
Vitaliy ◀️TriΔng3l▶️
and match the a, b and c with what's written in your code
Otumian
Check again.. the month
Vitaliy ◀️TriΔng3l▶️
Goto is not a good practice. I was told that a lot
I mean, if you were running the code in your mind
Anonymous
Vitaliy ◀️TriΔng3l▶️
not the way you should actually write it
Otumian
Month isn't incremented
Anonymous
Month isn't incremented
Ooh. Wait let me think about it
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
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
Vitaliy ◀️TriΔng3l▶️
Yeah that's really good
That's not good, that's pointless in this situation
Vitaliy ◀️TriΔng3l▶️
Really?
Run the code in your mind, statement by statement, actually executing every iteration of the loop
Anonymous
Run the code in your mind, statement by statement, actually executing every iteration of the loop
I'm stuck on control flow so I basically am getting confused guys
Anonymous
(Also you have an unconditional return inside the loop, so… the loop behaves pretty much like if (month >= 0) {…; return 0;})
Oh I get it now. So basically I just ended my program without even going to the else if statement
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
Vitaliy ◀️TriΔng3l▶️
if month >= 0, one iteration is done, which includes a return, which terminates the whole program since it's in main
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
if month >= 0, one iteration is done, which includes a return, which terminates the whole program since it's in main
Since I'm asking it to return 0 after the first iteration the function itself just goes directly to the return statement?
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▶️
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
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
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
Thirdly my whole program is all messed up
I don't know how I didn't realise this sooner
Anonymous
/warn
Anonymous
just for you to be able to "compile and run" the code in your brain, statement by statement
You made me realise I need to study control flow hard. Thanks. I'll correct it after reading about it properly. For now I'm gonna use a switch statement to do it
Vitaliy ◀️TriΔng3l▶️
Vitaliy ◀️TriΔng3l▶️
if/else, goto, switch/case/default, while, for, do/while, continue, break, return, function calls
Anonymous
https://en.cppreference.com/w/cpp/chrono/year_month
Anonymous
> int year; > int month; why?
I want to make a program that calculates the age like 37.75 years old
Anonymous
https://en.cppreference.com/w/cpp/chrono/year_month
Oh so a class? I don't know what that is yet
Anonymous
I want to make a program that calculates the age like 37.75 years old
https://en.cppreference.com/w/cpp/chrono/year_month/operator_arith_2
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.
Andrew
Anyone help me
read rules
Gaurav
read rules
Why you create this group
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:
Anonymous
Anyone help me
You want us to write code for you?
PO
Hi I would like to know if there is anyone who has worked with Gtk+2.0 in c programming?!