Python Forum
While loop in Python and C++
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop in Python and C++
#1
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
        }
Reply
#2
in python:
while int(input()):
    # my code
use 4 spaces for indentatioon
Reply
#3
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.
Reply
#4
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.
Reply
#5
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 :
Output:
1 ------- 2 ------- 3 ------- 0 out of loop
Enter 1,2,3,E :
Output:
1 ------- 2 ------- 3 ------- E
Error:
ValueError: invalid literal for int() with base 10: '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 :
Output:
1 ------ 2 ------ 3 ------ 0 ------
needs more inputs ...


Enter 1,2,3,E :
Output:
1 ------ 2 ------ 3 ------ E press any key to continue ...
Reply
#6
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020