A
arnuld
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?
(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.
3.) /switch/ executes only for once.
4.) there is no use of variable /tries/ here as /return/ always causes
/while/ to execute only once.
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?
(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.
3.) /switch/ executes only for once.
4.) there is no use of variable /tries/ here as /return/ always causes
/while/ to execute only once.