CS 111 - 2/28/17 If Statements - one form of Selection Statements We select whether a block of code is executed or not. Syntax: of the if-then statement if : If the is true, then execute the . If the is false, skip the . Most use the relational operations: > >= < <= == != Example: Absolute Value if ( value1 < 0 ) : value1 = -value1 Syntax: of the if-then-else statement if : else : If the is true, execute Otherwise, execute Example: Determine the larger of two values val1 = 6 val2 = 17 if ( val1 > val2 ) : max = val1 else : max = val2 ================================== when in the "main()" function, if we need to quit a program we can use the return statement: if ( x != y ) : print "Error message" return .... When not in the "main()" function, if we need to quit a program, we can use the sys.exit() function. Normally a parameter is given such as: sys.exit(0) This parameter could be used "report" the kind of termination. We don't worry about this, so any parameter value is fine. Note: on some python systems, the entire system will terminate when sys.exit( ) is used (not just your program).