Decision Making
E N D
Presentation Transcript
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles
Decision Making • In addition to looping, the ability to make decisions is an important and powerful feature in programming languages. • Decision making elements emulate our thought processes and logic. • We saw that there is a special symbol for decisions when using flowcharts. • Decision elements allow us to make comparisons, check if a condition is met, or if a group of conditions are met. • In C-language we have three types of decision making elements: • if statements • switch statements • The conditional operator
if statements • Similar to the MATLAB if stament • Syntax if (expression) program statement • Syntax with block of statements if (expression) { program statements } • If the expression is TRUE, the program statement or program statements are executed, otherwise they are skipped.
if statements • The expression can be any valid C r-value expression that evaluates to a numerical solution. • In C-language • an expression evaluating to zero is defined as FALSE. • an expression evaluating to non-zero is defined as TRUE. • Example: int x = 5, y; y = 5; if (x-y){ //x-y = 0 therefore FALSE, program does not enter here printf(“x and y are different”); //etc. }
if statement expressions • As we discussed before, we can form expressions using the arithmetic, relational and logic operators. • Arithmetic: +, -, *, / • Relational: >, >=, <, <=, ==, != • Logical: &&, ||, ! • We can form very complex expression which can be confusing: A && B >= 10 || X+Y/2 – 5 != Z && D • Which operation goes first? Table A.5 in your book (pg. 440) summarizes the operator precedence. So you can figure it out… • However, the intended order may not match the precedence defined by C. • The most appropriate approach is to make generous use of parenthesis. A && (B >= 10) || ( (X+Y/2 – 5) != Z) && D • Don’t be shy! Others will thank you for showing good programming habits.
Some variants: if-else • In many cases, we will have an alternative to the if statement expression. Hence we can use the if-else construct. if (expression) { program statements } else { alternative program statements }
Some variants • Example: int x = 5, y; y = 5; if (x-y){ //x-y = 0 therefore FALSE, program does not enter // here printf(“x and y are different”); //etc. } else { // the alternative gets executed printf(“x and y are different”); }
Some variants: nested if statements • We can always have multiple if statement nested if ( condition1 is TRUE) if (condition2 is TRUE) if (condition3 is TRUE){ DO SOMETHING} • A more practical statement is build using if-else: if (x > 0) if (x > 20 && y != x) { y = x; x = 2*x; } else { x = 20; } We have an if statement … followed by an if-else construct. The else belongs to the inner if OR most recent if.
Some variants: nested if statements • If we want to form an if-else construct with the first if, we need to use brackets to form the correct association : if (x > 0) { if (x > 20 && y != x) { y = x; x = 2*x; } } else { x = 20; } … with an if statement inside the if-else construct We have an “outer” if-else construct… BE VERY CAREFUL WITH YOUR INTERPRETATION OF THE IF-ELSE STATEMENTS
More interesting: the else-if construct • Suppose we want to test the value of x against more than two conditions. Each condition is multiple exclusive. • We can build a nested if-else construct as follows: if (x <= -10) y = -10; else if ( (x > -10) && (x <= 5) ) y = -5; else if ( (x > -5) && (x < 5) ) y = 0; else if ( (x >= 5) && (x < 10) ) y = 5; else y = 10;
The switch statement • In many cases, the nested if-else statements can be expressed in terms of a switch statement. • An expression produces different n values. • For each value there is a different case with a specific set of statements. • A break statement is included at the end of each case to exit the switch structure. • A default case is included for any other values with no specific case. • The default case is optional. switch (expression) { case value1: program statement(s); break; case value2: program statement(s); break; . . . case valuen: program statement(s); break; default: program statement(s); break; }
switch statement switch (expression) { case value1: program statement(s); case value2: program statement(s); break; case value3: program statement(s); break; default: program statement(s); break; } expression == value 1 • What happens if no break statements are included? • If the break statement is not included, the statement the program falls through the next case. • This is maybe useful in some cases, but in general you want to break at the end of each case. no break statement break terminates switch
Conditional operator • A very particular operator from C-language. • A ternary operator implies it uses three expressions and returns a value. • Syntax: condition ? expression1 : expression2 • If condition is TRUE, evaluate expression1. • If condition is FALSE, evaluate expression2.
Conditional operator - Examples • s = (x<0) ? -1 : x*x; • minimum min_value = (a < b) ? a : b; • sign (nested conditional operators) sign =(num < 0) ? -1 : ((num > 0) ? 1:0 ) Not a smiley face