While loop in Python and C++ - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: While loop in Python and C++ (/thread-27726.html) |
While loop in Python and C++ - smjt2000 - Jun-18-2020
I have a question related to Loops in python and C++(While)
What's the difference between executing loops as integers in both languages ? Like python wouldn't accept zero as an integer(in a loop) and it wouldn't execute on the other hand C/C++ would make it true ? And Why does we see an error when we try to enter character in python but a skipped loop in c++ ? while int(input()): my code(moderator note: This also looks better in python tags) c++ : int x; while(cin>>x) { my code } RE: While loop in Python and C++ - Larz60+ - Jun-18-2020 in python: while int(input()): # my codeuse 4 spaces for indentatioon RE: While loop in Python and C++ - bowlofred - Jun-18-2020 Sorry. It's been too long since I've done C++, and your code didn't immediately compile. So I can't tell you why the C++ would accept a character. (I think it shouldn't since x is declared int). But in the C++ code, cin is returning a value representing success, not the value of the input (that goes to x). So entering a "false" value still enters the loop. In python, the input returns the value that was entered. This is passed to int() that tries to convert it. So if you enter a zero, it becomes the number zero, and that is false. There is no question of the input failing. It will block, succeed, or throw an exception. In C++ a character can't be sent to the declared int variable, so the >> operator fails and returns false to the test. In Python, int() doesn't fail. It either succeeds or throws an exception. You can catch the ValueError exception in a try/except block if you want to continue even when a non-integer is passed in. RE: While loop in Python and C++ - deanhystad - Jun-18-2020 The two act differently because they are doing completely different things. "cin >> x" returns fail() or !fail(). Since "x" is an "int" it returns !fail() as long as you keep entering integers. The value for x is ignored. If you enter something that is not an integer cin >> returns fail(). Your C++ loop will continue to execute as long as you keep entering integers. The python code tests the value entered and uses that to determine when the loop ends. Zero is the only integer that evaluates to False, so entering zero is the only way to end the loop. All other numbers evaluate to True for the while loop test. If you enter something other than an integer the python program throws an exception. This is normal for Python programs and should be thought of as the rough equivalent of returning fail() in the C++ program. In Python it is more common to try code and catch an exception. In C++ it is more common to provide a status return value that the program has to check. RE: While loop in Python and C++ - smjt2000 - Jun-18-2020 in Python when i enter 0 the loop skipped but in C++ loop will continue in Python when i enter a character I see a ValueError but in C++ the will skipped this C++ code is a part of code in univercity and I wrote the Python code for comparison sorry I can't write better in English Python: while int(input()): print("-------") print("out of loop")Enter 1,2,3,0 : Enter 1,2,3,E :
C++:#include<iostream> #include<conio.h> using namespace std; void main() { int i=1; while(cin>>i) { cout<<"------\n"; } cout<<"press any key to continue ... "; _getch(); }Enter 1,2,3,0 : needs more inputs ...Enter 1,2,3,E :
RE: While loop in Python and C++ - jefsummers - Jun-19-2020 In line 1 (Python) you input a value. If that value tests to True (anything other than 0), the loop proceeds. If the 0 is entered (False) the loop terminates. If the entered value is not a number you trip a ValueError. That's the Python behavior. Can't vouch for the C++. Learned C but skipped C++ as each version seemed OS specific so moved to Java before Python. |