Page 1 1 Structured Programming Structured Programming KMU 255 KMU 255- - Computer Programming Computer Programming Hacettepe University Hacettepe University Department of Chemical Engineering Department of Chemical Engineering 2012 2012- -2013 Fall Semester 2013 Fall Semester Dr. Eda Dr. Eda Ç Çelik elik- -AKDUR AKDUR Structured Programming: 1 Structured Programming: 1- -Decisions Decisions If condition statements end Page 2 1 Structured Programming Structured Programming KMU 255 KMU 255- - Computer Programming Computer Programming Hacettepe University Hacettepe University Department of Chemical Engineering Department of Chemical Engineering 2012 2012- -2013 Fall Semester 2013 Fall Semester Dr. Eda Dr. Eda Ç Çelik elik- -AKDUR AKDUR Structured Programming: 1 Structured Programming: 1- -Decisions Decisions If condition statements end 2 Structured Programming (Flow Control) Structured Programming (Flow Control) Conditionals: Conditionals: If If/ /elseif elseif/else /else s switch witch/c /case ase, try/catch , try/catch Loops: Loops: while while, , error error, , for, break, for, break, return, continue return, continue Slide reproduced from Selis Önel’s KMU206 lecture notes with permission The if…elseif structure Note that indentation helps to make programs easier to read, esp. if you have long programs. Page 3 1 Structured Programming Structured Programming KMU 255 KMU 255- - Computer Programming Computer Programming Hacettepe University Hacettepe University Department of Chemical Engineering Department of Chemical Engineering 2012 2012- -2013 Fall Semester 2013 Fall Semester Dr. Eda Dr. Eda Ç Çelik elik- -AKDUR AKDUR Structured Programming: 1 Structured Programming: 1- -Decisions Decisions If condition statements end 2 Structured Programming (Flow Control) Structured Programming (Flow Control) Conditionals: Conditionals: If If/ /elseif elseif/else /else s switch witch/c /case ase, try/catch , try/catch Loops: Loops: while while, , error error, , for, break, for, break, return, continue return, continue Slide reproduced from Selis Önel’s KMU206 lecture notes with permission The if…elseif structure Note that indentation helps to make programs easier to read, esp. if you have long programs. 3 The if…elseif structure If control_expr_1 is true (nonzero), then the program executes the statements in Block 1 and skips to the first executable statement following the end. Otherwise, the program checks for the status of control_expr_2. If control_expr_2 is true (nonzero), then the program executes the statements in Block 2 and skips to the first executable statement following the end. If all control expressions are zero, then the program executes the statements in the block associated with the else clause. There can be any number of elseif clauses (0 or more) in an if construct, but there can be at most one else clause. Example 3.2—The Quadratic Equation Write a program to solve for the roots of a quadratic equation. The inputs required by this program are the coefficients a, b, and c of the quadratic eqn. If b 2 -4ac > 0, two distinct real roots If b 2 -4ac = 0, single repeated root If b 2 - 4ac < 0, two complex roots Page 4 1 Structured Programming Structured Programming KMU 255 KMU 255- - Computer Programming Computer Programming Hacettepe University Hacettepe University Department of Chemical Engineering Department of Chemical Engineering 2012 2012- -2013 Fall Semester 2013 Fall Semester Dr. Eda Dr. Eda Ç Çelik elik- -AKDUR AKDUR Structured Programming: 1 Structured Programming: 1- -Decisions Decisions If condition statements end 2 Structured Programming (Flow Control) Structured Programming (Flow Control) Conditionals: Conditionals: If If/ /elseif elseif/else /else s switch witch/c /case ase, try/catch , try/catch Loops: Loops: while while, , error error, , for, break, for, break, return, continue return, continue Slide reproduced from Selis Önel’s KMU206 lecture notes with permission The if…elseif structure Note that indentation helps to make programs easier to read, esp. if you have long programs. 3 The if…elseif structure If control_expr_1 is true (nonzero), then the program executes the statements in Block 1 and skips to the first executable statement following the end. Otherwise, the program checks for the status of control_expr_2. If control_expr_2 is true (nonzero), then the program executes the statements in Block 2 and skips to the first executable statement following the end. If all control expressions are zero, then the program executes the statements in the block associated with the else clause. There can be any number of elseif clauses (0 or more) in an if construct, but there can be at most one else clause. Example 3.2—The Quadratic Equation Write a program to solve for the roots of a quadratic equation. The inputs required by this program are the coefficients a, b, and c of the quadratic eqn. If b 2 -4ac > 0, two distinct real roots If b 2 -4ac = 0, single repeated root If b 2 - 4ac < 0, two complex roots 4 a = input ('Enter the coefficient A: '); b = input ('Enter the coefficient B: '); c = input ('Enter the coefficient C: '); discriminant = b^2 - 4 * a * c; if discriminant > 0 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 x1 = ( -b )/( 2 * a ); disp ('This equation has two identical real roots:'); fprintf ('x1 = x2 = %f\n', x1); else 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 Try this ! Select your code with left click, then right click and select smart indent Page 5 1 Structured Programming Structured Programming KMU 255 KMU 255- - Computer Programming Computer Programming Hacettepe University Hacettepe University Department of Chemical Engineering Department of Chemical Engineering 2012 2012- -2013 Fall Semester 2013 Fall Semester Dr. Eda Dr. Eda Ç Çelik elik- -AKDUR AKDUR Structured Programming: 1 Structured Programming: 1- -Decisions Decisions If condition statements end 2 Structured Programming (Flow Control) Structured Programming (Flow Control) Conditionals: Conditionals: If If/ /elseif elseif/else /else s switch witch/c /case ase, try/catch , try/catch Loops: Loops: while while, , error error, , for, break, for, break, return, continue return, continue Slide reproduced from Selis Önel’s KMU206 lecture notes with permission The if…elseif structure Note that indentation helps to make programs easier to read, esp. if you have long programs. 3 The if…elseif structure If control_expr_1 is true (nonzero), then the program executes the statements in Block 1 and skips to the first executable statement following the end. Otherwise, the program checks for the status of control_expr_2. If control_expr_2 is true (nonzero), then the program executes the statements in Block 2 and skips to the first executable statement following the end. If all control expressions are zero, then the program executes the statements in the block associated with the else clause. There can be any number of elseif clauses (0 or more) in an if construct, but there can be at most one else clause. Example 3.2—The Quadratic Equation Write a program to solve for the roots of a quadratic equation. The inputs required by this program are the coefficients a, b, and c of the quadratic eqn. If b 2 -4ac > 0, two distinct real roots If b 2 -4ac = 0, single repeated root If b 2 - 4ac < 0, two complex roots 4 a = input ('Enter the coefficient A: '); b = input ('Enter the coefficient B: '); c = input ('Enter the coefficient C: '); discriminant = b^2 - 4 * a * c; if discriminant > 0 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 x1 = ( -b )/( 2 * a ); disp ('This equation has two identical real roots:'); fprintf ('x1 = x2 = %f\n', x1); else 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 Try this ! Select your code with left click, then right click and select smart indent 5 if constructs may be nested But, for branches in which there are many mutually exclusive options, use a single if construct with multiple elseif clauses in preference to nested if constructs. It would be much easier… The switch/case Construct • If the value of switch_expr is equal to case_expr_1, then the first code block will be executed, and the program will jump to the first statement following the end of the switch construct. • Similarly, if the value of switch_expr is equal to case_expr_2, then the second code block will be executed, and the program will jump to the first statement following the end of the switch construct. • The otherwise code block, if present, will be executed whenever the value of switch_expr is outside the range of all of the case selectors. Note that at most one code block can be executed. Thus if the switch expression matches more than one case expression, only the first one of them will be executed.Read More

Offer running on EduRev: __Apply code STAYHOME200__ to get INR 200 off on our premium plan EduRev Infinity!