+ 3
What is whitespace in programming?
Give a suitable example of program with whitespace and without whitespace?
3 Respuestas
+ 3
for(;x<y;)z;
is the same as
for
(
;
x <
y
; ) z
;
+ 3
Whitespace is typically used to improve readability of your code, not only for yourself but for others to.
Eg nested if statement
x=4;
y=2;
if(x==1){
print("hi");
if(y==2){
print("cheese");}
}
At first look, you might think "cheese" will be printed as y==2. Now with whitespace.
if(x==1){
print("hi");
if(y==2){
print("cheese");
}
}
You can now easily see that the second 'if' statement is nested so will only be tested if the previous 'if' is true.
Both codes are the same though.
One caveat with whitespace: Python uses whitespace to interpret code, eg this if statement will produce an error:
if(x==1):
more code here
this wont
if(x==1):
more code here
+ 1
it means that if we have a multiple space or multiple line breaks we could avoid it to one
like this:-
it means if we have a
a
multiple space. or multiple. line
breaks we could avoid it