Branches Program Design, Branches and Loops
n
n
Selim Aksoy Bilkent University Department of Computer Engineering
[email protected]
n
Branches are used to select and execute specific sections of the code while skipping other sections Selection of different sections depend on a condition statement We will learn: n n
if statement switch statement
Spring 2004
Branches: “if” Statement
Conditions can be: n
condition
if ( condition ), statement 1 statement 2 ...
true statement group
n
statement group
any real value (0 is false, non-zero is true) combination of relational and logical operators n
n
n
end
n
n
CS 111
3
Branching Examples n
n
n
n
if ( r 100 ) ), disp( [ ‘Grade must be in [0,100] range’ ] ); end if isinf( result ), disp( ‘Result is infinite’ ); end
Spring 2004
Spring 2004
CS 111
isempty() isnumeric(), ischar() isinf(), isnan() exist() CS 111
4
Branching Examples
Examples: n
e.g. ( x > 0 ) & ( x < 10 )
logical functions
n
Spring 2004
2
Branches: “if” Statement n
false
CS 111
5
Water tank example: r = input('Enter the radius of the tank base (in meters):'); if ( r