Branches and Loops (1/42 Branches >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: √if statement √ switch statement CPHAW try/catch statement @月协大学 TONGJI UNIVERSITY
Branches and Loops (1/42) Branches ➢ 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: ✓ if statement ✓ switch statement ✓ try/catch statement
Branches and Loops (2/42 Branch:if >if'Statement condition false if condition ) statement 1 statement 2 true statement end group @日济大学 TONGJI UNIVERSITY
Branches and Loops (2/42) Branch: if ➢ ‘if’ Statement condition statement group true false if ( condition ), statement 1 statement 2 ... end
Branches and Loops (3/42 Branch:if Conditions can be: any real value (0 is false,non-zero is true) combination of relational and logical operators ·e.g.(x>0)&(x<10) √logical functions ·isempty() isnumericO,ischar() isinf(),isnan() exist() CPHAW @月协大学 TONGJI UNIVERSITY
Branches and Loops (3/42) Branch: if ➢ Conditions can be: ✓ any real value (0 is false, non-zero is true) ✓ combination of relational and logical operators • e.g. ( x > 0 ) & ( x < 10 ) ✓ logical functions • isempty() • isnumeric(), ischar() • isinf(), isnan() • exist()
Branches and Loops (4/42 Branch:if Examples: √if(rl00), disp([Grade must be in [0,100]range']) end √if isinf(result), disp(Result is infinite'); end @日济大学 AW TONGJI UNIVERSITY
Branches and Loops (4/42) Branch: if Examples: ✓ if ( r 100 ) ), disp( [ ‘Grade must be in [0,100] range’ ] ); end ✓ if isinf( result ), disp( ‘Result is infinite’ ); end
Branches and Loops (5/42 Branch:if >Water tank example: r=input('Enter the radius of the tank base(in meters):); if(r0), disp([There is'num2str(space)'m3 extra space']); else @月停大学 disp(Tank is full'); TONGJI UNIVERSITY end
Branches and Loops (5/42) Branch: if ➢ Water tank example: r = input('Enter the radius of the tank base (in meters):'); if ( r 0 ), disp( [ 'There is ' num2str(space) 'm3 extra space' ] ); else disp( 'Tank is full' ); end
Branches and Loops (6/42 Branch:if > if-else if(condition), statement 1 true condition false statement 2 statement group 1 else statement statement statement 1 group 1 group 2 statement 2 statement group 2 end PHAW @日济大学 TONGJI UNIVERSITY
Branches and Loops (6/42) Branch: if condition statement group 1 true false statement group 2 if ( condition ), statement 1 statement 2 ... else statement 1 statement 2 ... end ➢ if-else statement group 1 statement group 2
Branches and Loops(7/42 Branch:if > if-else if(condition 1 ) statement1 statement 2 statement true condition false group 1 1 elseif condition 2 ) statement 1 statement group 1 true condition false statement2 statement 2 group 2 else statement statement group 2 group 3 statement 1 statement statement 2 group 3 CPHAW end @月儕大学 TONGJI UNIVERSITY
Branches and Loops (7/42) Branch: if condition 1 statement group 1 true false condition 2 statement group 2 statement group 3 true false if ( condition 1 ), statement 1 statement 2 ... elseif ( condition 2 ), statement 1 statement 2 ... else statement 1 statement 2 ... end statement group 1 statement group 2 statement group 3 ➢ if-else
Branches and Loops (8/42 Branch:if > Example: Finding roots of the quadratic equation "ax2+bx +c=0" Pseudocode: d b2-4ac ifd>0, two real roots else if d=0, two identical roots else two complex roots @月济大学 TONGJI UNIVERSITY
Branches and Loops (8/42) Branch: if ➢ Example: Finding roots of the quadratic equation “ax2 + bx + c = 0” ➢ Pseudocode: d = b2 – 4ac if d > 0, two real roots else if d == 0, two identical roots else two complex roots
Branches and Loops (9/42 Branch:if Prompt the user for the coefficients of the equation disp ("This program finds the roots of a quadratic ') disp ('equation of the form A*X 2 +B*X+C=0.) a input ('Enter the coefficient A:'); b=input (Enter the coefficient B:) c=input ('Enter the coefficient C:') Calculate discriminant discriminant =b2-4 a c; Solve for the roots,depending on the value of the discriminant if discriminant >0%there are two real roots,so... x1 =(-b +sgrt(discriminant))/(2 a ) x2 =(-b-sgrt(discriminant))/(2 *a ) disp ('This equation has two real roots:) fprintf ('xI =%fn',x1); fprintf ('x2 =%fn',x2): elseif discriminant =0%there is one repeated root,so... x1=(-b)/(2*a方 disp ('This equation has two identical real roots:) fprintf ('xI =x2 =%f n',x1); else there are complex roots,so... real_part =(-b)/(2 a); imag part=sqt(abs(discriminant))/(2*a)方 disp ("This equation has complex roots:) fprintf('x1 =%f+i%fn',real_part,imag_part ) 细月济大学 TONGJI UNIVERSITY fprintf('x2=%f-i%fn',real_part,imag_part ) end
Branches and Loops (9/42) Branch: if % Prompt the user for the coefficients of the equation disp ('This program finds the roots of a quadratic '); disp ('equation of the form A*X^2 + B*X + C = 0. '); a = input ('Enter the coefficient A: '); b = input ('Enter the coefficient B: '); c = input ('Enter the coefficient C: '); % Calculate discriminant discriminant = b^2 - 4 * a * c; % Solve for the roots, depending on the value of the discriminant if discriminant > 0 % there are two real roots, so... x1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); x2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); disp ('This equation has two real roots:'); fprintf ('x1 = %f\n', x1); fprintf ('x2 = %f\n', x2); elseif discriminant == 0 % there is one repeated root, so... x1 = ( -b ) / ( 2 * a ); disp ('This equation has two identical real roots:'); fprintf ('x1 = x2 = %f\n', x1); else % there are complex roots, so ... real_part = ( -b ) / ( 2 * a ); imag_part = sqrt ( abs ( discriminant ) ) / ( 2 * a ); disp ('This equation has complex roots:'); fprintf('x1 = %f +i %f\n', real_part, imag_part ); fprintf('x2 = %f -i %f\n', real_part, imag_part ); end
Branches and Loops (10/42 Branch:if if-else statement and nested if statement Example:Assigning letter grades Range Grade 100≥grade>95 A 95≥grade>86 B 86≥grade>76 C 76≥grade>66 D 66≥grade>0 F How can we compute the letter corresponding to a given numeric grade? 翻日济大学 TONGJIUNIVERSITY
Branches and Loops (10/42) Branch: if ➢ if-else statement and nested if statement Example: Assigning letter grades How can we compute the letter corresponding to a given numeric grade? Range Grade 100 grade > 95 A 95 grade > 86 B 86 grade > 76 C 76 grade > 66 D 66 grade > 0 F