430 likes | 528 Views
if Lesson 1 Outline. Boolean Expr Completely Parenthesized #1 Boolean Expr Completely Parenthesized #2 Boolean Expr Completely Parenthesized #3 Boolean Expr Completely Parenthesized #4 Boolean Expr Completely Parenthesized #5 BAD Condition #1 BAD BAD BAD Condition Example
E N D
ifLesson 1 Outline Boolean Expr Completely Parenthesized #1 Boolean Expr Completely Parenthesized #2 Boolean Expr Completely Parenthesized #3 Boolean Expr Completely Parenthesized #4 Boolean Expr Completely Parenthesized #5 BAD Condition #1 BAD BAD BAD Condition Example GOOD Condition Example Kinds of Statements Inside if Block Statements Inside if Block No Declarations Inside if Block Absolute Value Example #1 Absolute Value Example #2 A More Complicated if Example #1 A More Complicated if Example #2 A More Complicated if Example #3 A More Complicated if Example #4 A More Complicated if Example Runs #1 A More Complicated if Example Runs #2 A More Complicated if Example Runs #3 Compound Statement a.k.a. Block #1 Compound Statement a.k.a. Block #2 • if Lesson 1 Outline • Absolute Value • Absolute Value Definition • Absolute Value Implementation • What Does This Mean? • Branching with if • Example if Blocks • if Condition • if Block and Statement Terminators • if Block Indentation • if Flowchart • The Meaning of if #1 • The Meaning of if #2 • The Meaning of if #3 • The Meaning of if #4 • The Meaning of if #5 • The Meaning of if #6 • if Example #1 • if Example #2 • if Example Flowchart • Block Open/Close Comments for if Block if Lesson 1 CS1313 Spring 2014
Absolute Value Consider the function a(y) = | y | So we know that if Lesson 1 CS1313 Spring 2014
if y is negative otherwise Absolute Value Definition How is | y | defined? Well, you could always define it as the nonnegative square root of y2: But here’s another definition: if Lesson 1 CS1313 Spring 2014
if y is negative otherwise Absolute Value Implementation Here’s an implementation of absolute value in C: if (y < 0) { absolute_value_of_y = -y; } /* if (y < 0) */ else { absolute_value_of_y = y; } /* if (y < 0)...else */ if Lesson 1 CS1313 Spring 2014
What Does This Mean? if (y < 0) { absolute_value_of_y = -y; } /* if (y < 0) */ else { absolute_value_of_y = y; } /* if (y < 0)...else */ • Evaluate the condition(y < 0), which is a Boolean expression, resulting in either true (1) or false (0). • If the condition evaluates to true, then execute the statement inside the if clause. • Otherwise, execute the statement inside theelseclause. if Lesson 1 CS1313 Spring 2014
Branching withif Branchingis a way to selectbetween possible sets of statements. In C, the most common kind of branching is theifblock: if (condition) { statement1; statement2; … } if Lesson 1 CS1313 Spring 2014
ExampleifBlocks if (a > b) { printf("Wow, a is greater than b!\n"); } /* if (a > b) */ if (my_height < your_height) { shortest_height = my_height; } /* if (my_height < your_height) */ if (entree_size_code== large_code) { entree_price = large_stir_fried_tofu_price; } /* if (entree_size_code== large_code) */ if Lesson 1 CS1313 Spring 2014
ifCondition if (condition) { statement1; statement2; … } The condition is a Boolean expression completely enclosed in parentheses. The conditionis a Boolean expression, so it evaluates either to true (1) or to false (0). The Boolean expression that constitutes the condition MUSTbe completely enclosed in parentheses. if Lesson 1 CS1313 Spring 2014
ifBlock and Statement Terminators if (condition) { statement1; statement2; … } Theifstatement is followed by a block open { rather than by a statement terminator (semicolon). Statements inside theifclauseare followed by statement terminators (semicolons) as appropriate, just as if they were not inside theifclause. The block close } at the end of theifblock ISN’Tfollowed by a statement terminator (semicolon). if Lesson 1 CS1313 Spring 2014
ifBlock Indentation if (condition) { statement1; statement2; … } Statements inside theifclauseare indented additionally, beyond the indentation of theifstatement and its associated block close. In CS1313, the statements inside theifclause are indented an additional 4 spaces beyond theifstatement and its associated block close. In CS1313, you are ABSOLUTELY FORBIDDEN to use tabs for indenting in your source code. if Lesson 1 CS1313 Spring 2014
ifFlowchart statement_before; if (condition) { statement_inside1; statement_inside2; … } statement_after; A diamond indicates a branch. if Lesson 1 CS1313 Spring 2014
The Meaning ofif#1 Inmy_number.c, we saw something like this: if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ What does this mean? if Lesson 1 CS1313 Spring 2014
Condition The Meaning ofif#2 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ First, the condition ((users_number < minimum_number) || (users_number > maximum_number)) is evaluated, resulting in either true (1) or false (0). AGAIN: The condition is a Boolean expression completely enclosed in parentheses. if Lesson 1 CS1313 Spring 2014
Condition The Meaning ofif#3 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ Second, in the event that the condition evaluates to true (1), then the sequence of statement(s) insidetheifclause – that is, between the block open of theifstatement and the associated block close – are executed in order. Otherwise, these statements are skipped. if Lesson 1 CS1313 Spring 2014
Condition The Meaning ofif#4 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ Finally, regardless of whether the condition evaluates to true (1) or false (0), execution picks up at the next statement immediately afterthe block close of the ifclause, and continues along from there. if Lesson 1 CS1313 Spring 2014
Condition The Meaning ofif#5 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ In the event that the condition evaluates to true (1) – that is, IFit’s the case thatusers_numberis less thanminimum_numberORit’s the case that users_numberis greater thanmaximum_number– then the statement printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); is executed, in which case the output is: Hey! That’s not between 1 and 10! if Lesson 1 CS1313 Spring 2014
Condition The Meaning ofif#6 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ On the other hand, ifusers_numberlies betweenminimum_numberandmaximum_number– that is, if the condition evaluates to false (0) – then theprintfstatement is not executed, and therefore no output is produced by theifblock. if Lesson 1 CS1313 Spring 2014
ifExample #1 #include <stdio.h> int main () { /* main */ const int computers_number = 5; int users_number; printf("Pick an integer:\n"); scanf("%d", &users_number); if (users_number < computers_number) { printf("That’s unbelievable! Your number is\n"); printf(" less than mine!\n"); printf("Well, okay, maybe it’s believable.\n"); } /* if (users_number < computers_number) */ printf("And now I’m sick of you.\n"); printf("Bye!\n"); } /* main */ if Lesson 1 CS1313 Spring 2014
ifExample #2 % gcc -o isless isless.c % isless Pick an integer: 6 And now I’m sick of you. Bye! % isless Pick an integer: 5 And now I’m sick of you. Bye! % isless Pick an integer: 4 That’s unbelievable! Your number is less than mine! Well, okay, maybe it’s believable. And now I’m sick of you. Bye! if Lesson 1 CS1313 Spring 2014
if Example Flowchart printf("Pick an integer:\n"); scanf("%d", &users_number); if (users_number < computers_number) { printf("That’s unbelievable! Your number is\n"); printf(" less than mine!\n"); printf("Well, okay, maybe it’s believable.\n"); } /* if (users_number < computers_number) */ printf("And now I’m sick of you.\n"); printf("Bye!\n"); if Lesson 1 CS1313 Spring 2014
Block Open/Close Comments for if Block if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ NOTICE: • The block open of thisifblock doesn’t have a comment on the same line. • The block close of thisifblock does have a comment on the same line, and that comment contains theifstatement, or a truncated version of it, EXCLUDING its block close. if Lesson 1 CS1313 Spring 2014
Condition Boolean Expr Completely Parenthesized #1 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ The condition ((users_number < minimum_number) || (users_number > maximum_number)) is a Boolean expression completely enclosed in parentheses. How do we know this? if Lesson 1 CS1313 Spring 2014
Condition Boolean Expr Completely Parenthesized #2 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ First, this subexpression (users_number < minimum_number) is a Boolean expression, specifically a relational expression, so it evaluates to a Boolean value – true (1) or false (0). if Lesson 1 CS1313 Spring 2014
Condition Boolean Expr Completely Parenthesized #3 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ Second, this subexpression (users_number > maximum_number) is a Boolean expression, specifically a relational expression, so it evaluates to a Boolean value – true (1) or false (0). if Lesson 1 CS1313 Spring 2014
Condition Boolean Expr Completely Parenthesized #4 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ The condition (users_number < minimum_number) || (users_number > maximum_number) is a pair of Boolean subexpressions, specifically relational expressions, joined by a Boolean operation, OR (||). So the expression as a whole is a Boolean expression. if Lesson 1 CS1313 Spring 2014
Condition Boolean Expr Completely Parenthesized #5 if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ The condition ((users_number < minimum_number) || (users_number > maximum_number)) is a pair of Boolean subexpressions, specifically relational expressions, joined by a Boolean operation, OR (||), then enclosed in parentheses. So: The condition is a Boolean expression completely enclosed in parentheses. if Lesson 1 CS1313 Spring 2014
Condition BAD Condition #1 if (users_number < minimum_number) || (users_number > maximum_number) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ What if the condition (users_number < minimum_number) || (users_number > maximum_number) were a Boolean expression but were not completely enclosed in parentheses? The compiler would treat this as an error! It would be WRONG WRONG WRONG. if Lesson 1 CS1313 Spring 2014
BAD BAD BAD Condition Example % cat condnotenclosed.c #include <stdio.h> int main () { /* main */ const intminimum_number = 1; const intmaximum_number = 10; intusers_number = 0; if (users_number < minimum_number) || (users_number > maximum_number) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if (users_number < minimum_number) || ... */ } /* main */ % gcc -o condnotenclosedcondnotenclosed.c condnotenclosed.c: In function main: condnotenclosed.c:9: error: expected expression before || token Notice that the compiler is VERY UNHAPPY. if Lesson 1 CS1313 Spring 2014
GOOD Condition Example % cat condenclosed.c #include <stdio.h> int main () { /* main */ const intminimum_number = 1; const intmaximum_number = 10; intusers_number = 0; if ((users_number < minimum_number) || (users_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); } /* if ((users_number < minimum_number) || ... */ } /* main */ % gcc -o condenclosedcondenclosed.c % condenclosed Hey! That’s not between 1 and 10! Notice that the compiler is now HAPPY! if Lesson 1 CS1313 Spring 2014
Kinds of Statements InsideifBlock Between theifstatement’s block open and the associated block close, there can be any kind of executablestatements, and any number of them. For example: • printfstatements; • scanfstatements; • assignment statements; • ifblocks. There are several other kinds of executable statements that can occur inside anifblock, some of which we’ll learn later in the semester. if Lesson 1 CS1313 Spring 2014
Statements InsideifBlock In the event that theifcondition evaluates to true (1), then the statements inside theifblock will be executed one by one, in the order in which they appear in theifblock. if Lesson 1 CS1313 Spring 2014
No Declarations InsideifBlock Notice that anifblock SHOULDN’Tcontain declaration statements, because theifstatement is an executable statement, and ALLdeclarations MUSTcome before ANYexecutable statements. if Lesson 1 CS1313 Spring 2014
Absolute Value Example #1 % cat absval.c #include <stdio.h> int main () { /* main */ float input_value, output_value; printf("I'm going to calculate the absolute\n"); printf(" value of a value that you input.\n"); printf("Please input the value.\n"); scanf("%f", &input_value); if (input_value < 0) { output_value = -input_value; } /* if (input_value < 0) */ else { output_value = input_value; } /* if (input_value < 0)...else */ printf("The absolute value of %f is %f.\n", input_value, output_value); } /* main */ if Lesson 1 CS1313 Spring 2014
Absolute Value Example #2 % gcc –o absval absval.c % absval I'm going to calculate the absolute value of a value that you input. Please input the value. 5 The absolute value of 5.000000 is 5.000000. % absval I'm going to calculate the absolute value of a value that you input. Please input the value. -5 The absolute value of -5.000000 is 5.000000. if Lesson 1 CS1313 Spring 2014
A More Complicatedif Example#1 #include <stdio.h> #include <stdlib.h> int main () { /* main */ const intint_code = 1; const intfloat_code= 2; const intprogram_failure_code= -1; float float_input_value, float_output_value; intint_input_value, int_output_value; intdata_type_code; if Lesson 1 CS1313 Spring 2014
A More ComplicatedifExample #2 printf("I’m going to calculate the"); printf(" absolute value\n"); printf(" of a number that you input.\n"); printf("Would you like to input "); printf("an int or a float?\n"); printf(" (Enter %d for an int ", int_code); printf("or %d for a float.)\n", float_code); scanf("%d", &data_type_code); if ((data_type_code != int_code) && (data_type_code != float_code)) { printf("ERROR: I don’t recognize the "); printf("data type code %d.\n", data_type_code); exit(program_failure_code); } /* if ((data_type_code != int_code) ... */ Idiotproofing if Lesson 1 CS1313 Spring 2014
A More ComplicatedifExample #3 if (data_type_code == int_code) { printf("Please input the int.\n"); scanf("%d", &int_input_value); if (int_input_value < 0) { int_output_value = -int_input_value; } /* if (int_input_value < 0) */ else { int_output_value = +int_input_value; } /* if (int_input_value < 0)...else */ printf("The absolute value of "); printf("%d is %d.\n", int_input_value, int_output_value); } /* if (data_type_code == int_code) */ if Lesson 1 CS1313 Spring 2014
A More ComplicatedifExample #4 if (data_type_code == float_code) { printf("Please input the float.\n"); scanf("%f", &float_input_value); if (float_input_value < 0) { float_output_value = -float_input_value; } /* if (float_input_value < 0) */ else { float_output_value = +float_input_value; } /* if (float_input_value < 0)...else */ printf("The absolute value of "); printf("%f is %f.\n", float_input_value, float_output_value); } /* if (data_type_code == float_code) */ } /* main */ if Lesson 1 CS1313 Spring 2014
A More Complicated if Example Runs #1 % gcc -o absvalbytype absvalbytype.c % absvalbytype I’m going to calculate the absolute value of a number that you input. Would you like to input an int or a float? (Enter 1 for an int or 2 for a float.) 0 ERROR: I don’t recognize the data type code 0. if Lesson 1 CS1313 Spring 2014
A More Complicated if Example Runs #2 % absvalbytype I’m going to calculate the absolute value of a number that you input. Would you like to input an int or a float? (Enter 1 for an int or 2 for a float.) 1 Please input the int. 5 The absolute value of 5 is 5. % absvalbytype I’m going to calculate the absolute value of a number that you input. Would you like to input an int or a float? (Enter 1 for an int or 2 for a float.) 1 Please input the int. -5 The absolute value of -5 is 5. if Lesson 1 CS1313 Spring 2014
A More Complicated if Example Runs #3 % absvalbytype I’m going to calculate the absolute value of a number that you input. Would you like to input an int or a float? (Enter 1 for an int or 2 for a float.) 2 Please input the float. 5.5 The absolute value of 5.500000 is 5.500000. % absvalbytype I’m going to calculate the absolute value of a number that you input. Would you like to input an int or a float? (Enter 1 for an int or 2 for a float.) 2 Please input the float. -5.5 The absolute value of -5.500000 is 5.500000. if Lesson 1 CS1313 Spring 2014
Compound Statement a.k.a. Block #1 A compound statementis a sequence of statements, with a well-defined beginning and a well-defined end, to be executed, in order, under certain circumstances. Anifblock is a compound statement. We’ll see others later. Although anifblock is actually a sequence of statements, we can think of it as a single “super” statement in some contexts. Compound statements are also known as blocks. Thus, we speak of anifblock. if Lesson 1 CS1313 Spring 2014
Compound Statement a.k.a. Block #2 In C, a compound statement, also known as a block, is delimited by curly braces. That is, a compound statement (block): • begins with a block open { • ends with a block close } if Lesson 1 CS1313 Spring 2014