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++
#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


Messages In This Thread
While loop in Python and C++ - by smjt2000 - Jun-18-2020, 07:41 PM
RE: While loop in Python and C++ - by Larz60+ - Jun-18-2020, 08:46 PM
RE: While loop in Python and C++ - by bowlofred - Jun-18-2020, 09:08 PM
RE: While loop in Python and C++ - by deanhystad - Jun-18-2020, 09:14 PM
RE: While loop in Python and C++ - by smjt2000 - Jun-18-2020, 09:16 PM
RE: While loop in Python and C++ - by jefsummers - Jun-19-2020, 01:43 AM

Forum Jump:

User Panel Messages

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