+ 5
is there are any differences in if statement of c++ and python?
5 ответов
+ 6
yes syntax is different but working is same
//c++ if statement
if(a<10) // here indentation is not compulsory
cout<<"smaller than 10";
else
cout<<"greater than 10";
//python if
if(a<10): //here indentation is required
print "smaller than 10"
else
print "greater than 10"
+ 15
Well, the syntax is different, but the logic is similar.
+ 4
Not to be too fussy, but in Python,
- 'if' doesn't need parentheses, i.e. 'if a < 10:' works
- 'if' and 'else' both need colons at the end
- print "something" works in Python 2.x but Python 3.x (SL Code Playground uses 3.5) needs parentheses, i.e. print("something")
+ 2
python:
if a < 10 and b == 8:
print(true)
else:
print(false)
c++:
//no whitespace in here required
if(a < 10 && b == 8)
{ //not required for just one statement
cout << true;
}
else
{
cout << false;
}
0
//c++ if statement
if(a<12) // here indentation is not compulsory but curly braces are if there are more than one if
// or else statement .hence,
{
If (a<10) {
cout<<"smaller than 10";
}
}
else {
cout<<"greater than 10";
}
//python if
if a<10: //here indentation is required thus no need for curly braces
print "smaller than 10"
else :
print "greater than 10"
/* In python especially indentation (spacing) does matter. Don't use tab cause though tab gives you 4 spaces for python it's v confusing to differ b/w 4 spaces and tab or either change settings in code editors like auto expand tabs or replace by space if you still want to use tab but best practice is to use spaces when you are writing python programs. Whereas in java , JavaScript or C actual spacing doesn't matter
And some other differences as some other members indicated*/