Definition: An if statement contains a condition that evaluates to True or False. (True and False are Boolean values.) If the condition evaluates to True, the indented block of code will be evaluated. If the condition evaluates to False, the indented block of code will be skipped.

Example 2 below contains and both if and else blocks!

Works like a programming function, that is, you need a colon : and all command lines in block are indented. (See also page: if)

Example 1

x=1
if x<3 :

print 'x=', x
print 'The value of x is less than 3.'
x=x+2

print 'We are now out of the first conditional block and x=', x
if x<3 :

print 'x=', x
print 'The value of x is less than 3.'
x=x+2

print 'We are now out of the second conditional block and now x=', x

Result:

x= 1
The value of x is less than 3.
We are now out of the first conditional block and x= 3
We are now out of the second conditional block and x= 3 (see top 2 blocks of code of My Sage Page)

Example 2 - if ... else... combination

x=1
if x<3 :

print 'x=', x
print 'The value of x is less than 3 but we are going to increase it by 3.'
x=x+3

else :

print 'The value of x is greater than or equal to 3.'

print 'We are now out of the first conditional block and x=', x
if x<3 :

print 'x=', x
print 'The value of x is less than 3 but we are going to increase it by 3.'
x=x+3

else :

print 'The value of x is greater than or equal to 3.'

print 'We are now out of the second conditional block and now x=', x

Result:

x= 1
The value of x is less than 3 but we are going to increase it by 2.
We are now out of the first conditional block and x= 4.
The value of x is greater than or equal to 3.
We are now out of the second conditional block and x= 4 (see My Sage Page)

Returns True if the two objects are equal and False if they are not equal. Notice that == performs a comparison and not an assignment like = does.

x <> y

Returns True if the objects are not equal and False if they are equal.

x != y

Returns True if the objects are not equal and False if they are equal.

x < y

Returns True if the left object is less than the right object and False if the left object is not less than the right object.

x <= y

Returns True if the left object is less than or equal to the right object and False if the left object is not less than or equal to the right object.

x > y

Returns True if the left object is greater than the right object and False if the left object is not greater than the right object.

x >= y

Returns True if the left object is greater than or equal to the right object and False if the left object is not greater than or equal to the right object.

Operator on booleans

Description

b1 and b2

Returns True if both booleans are true and False otherwise.

b1 or b2

Returns True if either or both booleans are true and False only if both are false.

not b1

Returns True if boolean is false and False if boolean is true.

Home -> Control -> Conditionals ->

ifDefinition:Anifstatement contains aconditionthat evaluates toTrueorFalse. (TrueandFalseareBoolean values.) If the condition evaluates to True, the indented block of code will be evaluated. If the condition evaluates to False, the indented block of code will be skipped.Example 2below contains and bothifandelseblocks!Conditionalswhileloops to determine when to end the loopforloops to determine which values of the counter should be evaluated## EXAMPLES ON MY SAGE PAGE: Conditionals in Sage

ifcondition:colon :andall command lines in block are indented. (See also page:if)Example 1if x<3 :

print 'The value of x is less than 3.'

x=x+2

if x<3 :

print 'The value of x is less than 3.'

x=x+2

Result:The value of x is less than 3.

We are now out of the first conditional block and x= 3

We are now out of the second conditional block and x= 3 (see top 2 blocks of code of My Sage Page)

Example 2- if ... else... combinationif x<3 :

print 'The value of x is less than 3 but we are going to increase it by 3.'

x=x+3

if x<3 :

print 'The value of x is less than 3 but we are going to increase it by 3.'

x=x+3

Result:The value of x is less than 3 but we are going to increase it by 2.

We are now out of the first conditional block and x= 4.

The value of x is greater than or equal to 3.

We are now out of the second conditional block and x= 4 (see My Sage Page)

Other ifsExample 3-see Example 5 on wikipage: forOperator on objectsDescription<>yOperator on booleansDescriptionReference: Sage_for_Newbies, pp.28-33 (Ted Kosan)

Keywords:sage, conditional, condition, if, else, while, loops, for, control, repeating