arnuld wrote:
i am trying to understand the mentioned programme, i modified it a
little so that i could understand it better. here is the code & output:
// Stroustrup - Tour of C++
#include <iostream>
int main() {
int tries = 1;
while(tries < 4) {
std::cout << "Do you want to proceed [y/n]: ";
char get_ans = 0;
std::cin >get_ans;
switch(get_ans) {
case 'y':
return true;
case 'n':
return false;
default:
std::cout << "Sorry, did not get that.\n";
tries = tries + 1;
}
std::cout << "I will take that for a NO.\n";
return false;
}
std::cout << "print of *int main()* \n";
return 0;
}
-------------------OUTPUT ------------------------------
hurd@debian:~/programming/cpp$ g++-3.4 02_232-1.cpp
hurd@debian:~/programming/cpp$ ./a.out
Do you want to proceed [y/n]: y
hurd@debian:~/programming/cpp$ ./a.out
Do you want to proceed [y/n]: n
hurd@debian:~/programming/cpp$ ./a.out
Do you want to proceed [y/n]: j
Sorry, did not get that.
I will take that for a NO.
hurd@debian:~/programming/cpp$
-----------------------------------------------------------
i understand 3 things here:
1.) when i enter 'y' or 'n' /return/ breaks-out from the /main()/, not
from /switch/ or /while/. it is clear as the statement just above
/return 0/ never prints. it means /return/ breaks out from the whole
function & not from the innermost loop. why?
because neither switch nor while are functions. These are loops with a
conditional expression with the purpose of controlling the sequence of
the program.
>
(i expected /return/ to break out of /switch/ only )
2.) from (1), it means i can use /return/ anywhere & it will break-out
from the nested-loops immediately even when i will have 3 nested loops
& i use /return/ in innermost loop.
Yes, return will terminate/exit whatever function it was called in,
regardless of the loops involved. Synctaticly, you could think of the
loops as nothing else but conditional scopes with their own stack.
In other words, there is more going on than meets the eye.
Obviously, if you return from within any scope, all the active scopes
in the function() cease to exist. That is, any automatic variable in
those scope(s) has its d~tor invoked. Returning from an inner scope
does not interrupt that process - on the contrary.
Unlike loops and scopes, functions return a state which may very well
be void. Loops and scopes don't return anything, they just live and
die, but die they do.
To prove the stack:
#include <iostream>
int main()
{
int i(0);
{ // a do_it_once loop
int i(1);
{ // another do_it_once loop
int i(2);
std::cout << "(inner loop) i = " << i << std::endl;
// returning here ends all scopes
}
std::cout << "(mid loop) i = " << i << std::endl;
}
std::cout << "(outer loop) i = " << i << std::endl;
return 0;
}
/*
(inner loop) i = 2
(mid loop) i = 1
(outer loop) i = 0
*/
Try see what happens if you access ::i instead from within an inner
loop. This is important because those scopes are really anonymous
namespaces.
>
3.) /switch/ executes only for once.
4.) there is no use of variable /tries/ here as /return/ alwaa parallel universeys causes
/while/ to execute only once.
To respect the purpose of the program, the above should be:
#include <iostream>
int proceed()
{
std::cout << "lets do it!\n";
// do some work here
return 0;
}
int main() {
int tries(0);
while(tries < 4) {
std::cout << "Do you want to proceed [y/n]: ";
char get_ans = 0;
std::cin >get_ans;
switch(get_ans) {
case 'y':
return proceed();
case 'n':
std::cout << "aborting as requested.\n";
return 0;
default:
std::cout << "Sorry, did not get that.\n";
++tries;
}
}
std::cout << "I will take that for a NO.\n";
return 0;
}