Lecture 9: Conditionals
Monday, February 27, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 4.1 - 4.3 (Note - We will cover logical operators later)
Homework Due: None
Homework Assigned: Homework 04 -- assigned last class
Classwork Assigned: None
Topics Covered:
- Homework 4 questions
- Example questions for quiz
- Finish (well - start really) last lecture
- Since we didn't go over last lecture last class, we may delay this lecture one day - or may start it but only get half way - we'll see
- Conditionals
- Used to change the flow of program code
- Used to allow the program to respond to different input
- Two main constructs in C
- IF - ELSE
- SWITCH
- Conditional tests - operators
- Multiple tests can be used to determine if something is "true" or not
- Your code can use these tests to then change what it will do
- Eg. for homework 4, after we calculate the grade, we could print "PASS" or "FAIL" depending on the class grade:
/* Print PASS if average is good enough to pass */ void print_pass(double avg) { if (avg >= 70) { printf("PASS\n"); } }
- Relational operators
- Can compare variables, constants, and expressions.
- Used to test if things are equal, less than, greater than, etc.
- < true if left is less than right
- > true if left is greater than right
- <= true if left is less than or equal to right
- >= true if left is greater than or equal to right
- == true if left is equal to right (note TWO ='s)
- != true if left is not equal to right
- The result of a relational operator is an integer with the value 0 (false) or 1 (true)
- if will evaluate its condition and run the conditional code if the value is non-zero
- Variable being compared to another variable
if (a == b) { printf("a is equal to b\n"); }
- Constant being compared to a variable
if (100 != var) { printf("var is not equal to 100\n"); }
- Constant being compared to an expression (assume my_function() returns some int or double)
if (70.0 <= my_function() * 20.0) { printf("Calculated value is greater than 70\n"); }
- IF - ELSE details
- if and else are reserved words in C
- Takes the form if (condition) { condition_true_code; } else { condition_false_code; }
- The condition_true_code can be one statement, or a series of statements
- The condition_true_code will execute ONLY if condition was evaluated to true
- The condition_false_code can be one statement, or a series of statements
- The condition_false_code will execute ONLY if condition was evaluated to false
- The else { condition_false_code; } is optional and can be left off if the program doesn't need it
- Note that when there is only one statement to execute after the if, the {}'s are optional
if (x == 1) printf("This code will only print when x is 1\n"); printf("This code will always print\n");
- This can cause hard to diagnose bugs, so it is recommended to always use {}'s with if
- The {}'s also make it easier to see what block of code is associated with the condition
if (x == 1) { printf("This code will only print when x is 1\n"); } printf("This code will always print\n");
- An example of using if with else
if (avg >= 70) { printf("PASS\n"); } else { printf("FAIL\n"); }
- Note that spaces and newlines don't matter, the following is the same to the compiler as the last example
if (avg >= 70) { printf("PASS\n"); } else { printf("FAIL\n"); }
- And with optional {}'s, the compiler will generate the same code for the following as well
if (avg >= 70) printf("PASS\n"); else printf("FAIL\n");
- And this is the same too
if (avg >= 70) printf("PASS\n"); else printf("FAIL\n");
- One final pathological example that is the same to the compiler, but not to us having to read it!
if( avg >= 70) printf( "PASS\n" );else printf( "FAIL\n") ;
- A correct (but normally not what you want) example:
if (var) { printf("This code will print whenever var is non-zero\n"); }
- This example checks to condition inside the ()'s following if, which simply evaluates var
- The printf will run when var is true (non-zero), and won't run when var is false (zero)
- Compiler will not warn you about this, so be careful
- A sample program showing combining user input, with if, else, and conditionals to compare two values
- An improved sample program showing combining user input, with if, else, and conditionals to compare two values
- Note that the second program shows a more complex chaining of if-else-if that will be covered later