Lecture Conditionals
Lecture Conditionals
Computer Science I
Conditionals
Logical
Operators
Conditionals
Pitfalls
Exercises
Introduction
1. Introduction
Conditionals
Logical
Operators
2. Conditionals
Pitfalls
4. Pitfalls
5. Exercises
Introduction
Conditionals
Boolean
Statements
Numeric
Comparisons
Complex Logic
Statements
Conditionals
Part I: Introduction
Logical
Control Flow & Logical Operators
Operators
Pitfalls
Exercises
Sequential Control Flow
Introduction
Conditionals
Boolean
Statements
Numeric Statement 1
Comparisons
Complex Logic
Statements
Conditionals
Logical
Statement 2
Operators
Pitfalls
Exercises
Statement 3
If Statement Flow
Introduction
Conditionals
Boolean
Statements true
Numeric hconditioni Code Block
Comparisons
Complex Logic
Statements
Conditionals
Logical
Operators
Pitfalls false
Exercises
Remaining
Program
If-Else Statement Flow
Introduction
Conditionals true false
Boolean hconditioni
Statements
Numeric
Comparisons
Complex Logic
Statements
Conditionals
Logical
Operators Code Code
Block A Block B
Pitfalls
Exercises
Remaining
Program
If-Else-If Statement Flow
Introduction true
Conditionals Code
if(hcondition 1i) hcondition 1i
Block A
Boolean
Statements
Numeric
Comparisons false
Complex Logic
Statements
true
Code
Conditionals else if(hcondition 2i) hcondition 2i
Block B
Logical
Operators
false
Pitfalls
Code
Exercises else
Block C
Remaining
Program
true
Code
if(hcondition 1i) hcondition 1i
Block A
false
true
Code
else if(hcondition 2i) hcondition 2i
Block B
false
true
Code
else if(hcondition 3i) hcondition 3i
Block C
false
.. ..
. .
false
true
Code
else if(hcondition ni) hcondition ni
Block N
false
Code
else
Block M
Remaining
Program
Boolean Statements
Introduction
Conditionals
Boolean
Statements
Numeric A boolean condition or expression is a logical expression that evaluates to
Comparisons
Complex Logic
Statements
either true or false
Conditionals
Logical
Operators
Pitfalls
Exercises
Boolean Statements
Introduction
Conditionals
Boolean
Statements
Numeric A boolean condition or expression is a logical expression that evaluates to
Comparisons
Complex Logic
Statements
either true or false
Conditionals May involve numerical comparisons a ≥ 0
Logical
Operators
Pitfalls
Exercises
Boolean Statements
Introduction
Conditionals
Boolean
Statements
Numeric A boolean condition or expression is a logical expression that evaluates to
Comparisons
Complex Logic
Statements
either true or false
Conditionals May involve numerical comparisons a ≥ 0
Logical
Operators A condition can be simple or complex
Pitfalls
Exercises
Boolean Statements
Introduction
Conditionals
Boolean
Statements
Numeric A boolean condition or expression is a logical expression that evaluates to
Comparisons
Complex Logic
Statements
either true or false
Conditionals May involve numerical comparisons a ≥ 0
Logical
Operators A condition can be simple or complex
Pitfalls May connect one or more expressions using a logical and or a logical or
Exercises
Numeric Comparisons
Conditionals
Logical
Operators
Pitfalls
Exercises
Numeric Comparisons
Conditionals
Logical
Operators
Pitfalls
Exercises
Numeric Comparisons
Conditionals
Logical
Operators
Pitfalls
Exercises
Numeric Comparisons
Conditionals
Logical
Operators
Pitfalls
Exercises
Numeric Comparisons
Logical
Operators
Pitfalls
Exercises
Numeric Comparisons
Pitfalls
Exercises
Numeric Comparisons
Pitfalls
Exercises
Numeric Comparisons
Code versions:
== != >= <= > <
Logical And
Introduction
Conditionals
Boolean
Statements A B A and B
Numeric
Comparisons
Complex Logic
false false false
Statements
false true false
Conditionals
true false false
Logical
Operators true true true
Pitfalls
Exercises
Logical And
Introduction
Conditionals
Boolean
Statements A B A and B
Numeric
Comparisons
Complex Logic
false false false
Statements
false true false
Conditionals
true false false
Logical
Operators true true true
Pitfalls
Exercises
Code version: &&
Logical Or
Introduction
Conditionals
Boolean
Statements
A B A or B
Numeric
Comparisons
Complex Logic
false false false
Statements
false true true
Conditionals
true false true
Logical
Operators true true true
Pitfalls
Exercises
Logical Or
Introduction
Conditionals
Boolean
Statements
A B A or B
Numeric
Comparisons
Complex Logic
false false false
Statements
false true true
Conditionals
true false true
Logical
Operators true true true
Pitfalls
Exercises
Code version: ||
Logical Negation
Introduction
Conditionals
Boolean
Statements
Numeric
Comparisons
A not A
Complex Logic
Statements false true
Conditionals
true false
Logical
Operators
Pitfalls
Exercises
Logical Negation
Introduction
Conditionals
Boolean
Statements
Numeric
Comparisons
A not A
Complex Logic
Statements false true
Conditionals
true false
Logical
Operators
Pitfalls
Code version: !
Exercises
Introduction
Conditionals
Numerical
Comparisons
Examples
Logical
Operators
Part II: Conditionals
Pitfalls
If, If-Else, If-Else-If & Numeric Comparisons
Exercises
If Statement
1 if(<condition>) {
Introduction
2 //conditional body: code inside this code block
Conditionals
Numerical
3 //will only execute if the <condition> evaluates
Comparisons
Examples
4 //to true, otherwise it will not execute at all
Logical
5 }
Operators
Pitfalls
Exercises
Uses the keyword if
If Statement
1 if(<condition>) {
Introduction
2 //conditional body: code inside this code block
Conditionals
Numerical
3 //will only execute if the <condition> evaluates
Comparisons
Examples
4 //to true, otherwise it will not execute at all
Logical
5 }
Operators
Pitfalls
Exercises
Uses the keyword if
The condition is enclosed in parentheses
If Statement
1 if(<condition>) {
Introduction
2 //conditional body: code inside this code block
Conditionals
Numerical
3 //will only execute if the <condition> evaluates
Comparisons
Examples
4 //to true, otherwise it will not execute at all
Logical
5 }
Operators
Pitfalls
Exercises
Uses the keyword if
The condition is enclosed in parentheses
The code block begins and ends with curly brackets
If Statement
1 if(<condition>) {
Introduction
2 //conditional body: code inside this code block
Conditionals
Numerical
3 //will only execute if the <condition> evaluates
Comparisons
Examples
4 //to true, otherwise it will not execute at all
Logical
5 }
Operators
Pitfalls
Exercises
Uses the keyword if
The condition is enclosed in parentheses
The code block begins and ends with curly brackets
Behavior
If-Else Statement
1 if(<condition>) {
Introduction
2 //code block A
Conditionals
3 } else {
Numerical 4 //code block B
Comparisons
Examples 5 }
Logical
Operators
Pitfalls
1 if(<condition>) {
Introduction
2 //code block A
Conditionals
3 } else {
Numerical 4 //code block B
Comparisons
Examples 5 }
Logical
Operators
Pitfalls
1 if(<condition>) {
Introduction
2 //code block A
Conditionals
3 } else {
Numerical 4 //code block B
Comparisons
Examples 5 }
Logical
Operators
Pitfalls
1 if(<condition>) {
Introduction
2 //code block A
Conditionals
3 } else {
Numerical 4 //code block B
Comparisons
Examples 5 }
Logical
Operators
Pitfalls
1 if(<condition>) {
Introduction
2 //code block A
Conditionals
3 } else {
Numerical 4 //code block B
Comparisons
Examples 5 }
Logical
Operators
Pitfalls
1 if(<condition1>) {
2 //code block A
Introduction
3 } else if(<condition2>) {
Conditionals
Numerical
4 //code block B
Comparisons
Examples
5 } else {
Logical 6 //code block C
Operators 7 }
Pitfalls
Multiple conditions: may define as many as you want
Exercises
If-Else-If Statement
1 if(<condition1>) {
2 //code block A
Introduction
3 } else if(<condition2>) {
Conditionals
Numerical
4 //code block B
Comparisons
Examples
5 } else {
Logical 6 //code block C
Operators 7 }
Pitfalls
Multiple conditions: may define as many as you want
Exercises
The first condition that evaluates to true is the one (and only one) that is
executed
If-Else-If Statement
1 if(<condition1>) {
2 //code block A
Introduction
3 } else if(<condition2>) {
Conditionals
Numerical
4 //code block B
Comparisons
Examples
5 } else {
Logical 6 //code block C
Operators 7 }
Pitfalls
Multiple conditions: may define as many as you want
Exercises
The first condition that evaluates to true is the one (and only one) that is
executed
Each code block is mutually exclusive
If-Else-If Statement
1 if(<condition1>) {
2 //code block A
Introduction
3 } else if(<condition2>) {
Conditionals
Numerical
4 //code block B
Comparisons
Examples
5 } else {
Logical 6 //code block C
Operators 7 }
Pitfalls
Multiple conditions: may define as many as you want
Exercises
The first condition that evaluates to true is the one (and only one) that is
executed
Each code block is mutually exclusive
The most specific conditions come first, more general last
If-Else-If Statement
1 if(<condition1>) {
2 //code block A
Introduction
3 } else if(<condition2>) {
Conditionals
Numerical
4 //code block B
Comparisons
Examples
5 } else {
Logical 6 //code block C
Operators 7 }
Pitfalls
Multiple conditions: may define as many as you want
Exercises
The first condition that evaluates to true is the one (and only one) that is
executed
Each code block is mutually exclusive
The most specific conditions come first, more general last
You may omit the final else block if there is no “final case” to consider
Numerical Comparisons
Introduction
Conditionals
Numerical Comparison operators:
Comparisons
Examples < , > , <= , >=
Logical
Operators
Pitfalls
Exercises
Numerical Comparisons
Introduction
Conditionals
Numerical Comparison operators:
Comparisons
Examples < , > , <= , >=
Logical
Operators Equality operator: ==
Pitfalls
Exercises
Numerical Comparisons
Introduction
Conditionals
Numerical Comparison operators:
Comparisons
Examples < , > , <= , >=
Logical
Operators Equality operator: ==
Pitfalls Inequality operators !=
Exercises
Numerical Comparisons
Introduction
Conditionals
Numerical Comparison operators:
Comparisons
Examples < , > , <= , >=
Logical
Operators Equality operator: ==
Pitfalls Inequality operators !=
Exercises
May be used in combinations of literals (hardcoded numerical values),
variables or expressions
Numerical Comparisons
1 int a, b, c;
2
3 //comparing a variable to a literal
4 if(a == 0) {
Introduction
5 printf("a is zero!\n");
Conditionals 6 }
Numerical
7
Comparisons 8 //comparing two variable values:
Examples 9 if(a == b) {
10 printf("the two values are equal\n");
Logical 11 }
Operators 12
13 //you can, but shouldn't do the following
Pitfalls
14 if(10 == a) {
Exercises 15 //...
16 }
17
18 if(b * b - 4 * a * c < 0) {
19 printf("looks bad...\n");
20 }
21
22 //you can but shouldn't:
23 if(10 < 20) {
24 printf("duh, that's always true\n");
25 }
Conditional Examples
1 int huskerScore;
2 int opponentScore;
3
Introduction 4 //a simple if statement:
5 if(huskerScore > opponentScore) {
Conditionals
6 printf("Huskers Win!\n");
Numerical
Comparisons 7 }
Examples 8
9 //an if-else statement:
Logical 10 if(huskerScore > opponentScore) {
Operators 11 printf("Huskers Win!\n");
12 } else {
Pitfalls 13 printf("Huskers do not win.\n");
14 }
Exercises
15
16 //an if-else-if statement:
17 if(huskerScore > opponentScore) {
18 printf("Huskers Win!\n");
19 } else if(huskersScore < opponentScore) {
20 printf("Huskers Lose!\n");
21 } else {
22 printf("Tie, let's go to overtime!\n");
23 }
Coding Style
Introduction
1 if(huskerScore > opponentScore) { Use of spaces
Conditionals
Numerical 2 printf("Huskers Win!\n");
Comparisons
Examples 3 } else if(huskersScore < opponentScore) {
Logical 4 printf("Huskers Lose!\n");
Operators
5 } else {
Pitfalls 6 printf("Tie, let's go to overtime!\n");
Exercises 7 }
Coding Style
Introduction
1 if(huskerScore > opponentScore) { Use of spaces
Conditionals
Numerical 2 printf("Huskers Win!\n"); Opening curly brackets on the
Comparisons
Examples 3 } else if(huskersScore < opponentScore) { same line as keywords
Logical 4 printf("Huskers Lose!\n");
Operators
5 } else {
Pitfalls 6 printf("Tie, let's go to overtime!\n");
Exercises 7 }
Coding Style
Introduction
1 if(huskerScore > opponentScore) { Use of spaces
Conditionals
Numerical 2 printf("Huskers Win!\n"); Opening curly brackets on the
Comparisons
Examples 3 } else if(huskersScore < opponentScore) { same line as keywords
Logical 4 printf("Huskers Lose!\n");
Operators Closing curly brackets on the
5 } else {
Pitfalls
same indentation level
6 printf("Tie, let's go to overtime!\n");
Exercises 7 }
Coding Style
Introduction
1 if(huskerScore > opponentScore) { Use of spaces
Conditionals
Numerical 2 printf("Huskers Win!\n"); Opening curly brackets on the
Comparisons
Examples 3 } else if(huskersScore < opponentScore) { same line as keywords
Logical 4 printf("Huskers Lose!\n");
Operators Closing curly brackets on the
5 } else {
Pitfalls
same indentation level
6 printf("Tie, let's go to overtime!\n");
Exercises 7 } All blocks are indented at the
same level
Coding Style
Introduction
1 if(huskerScore > opponentScore) { Use of spaces
Conditionals
Numerical 2 printf("Huskers Win!\n"); Opening curly brackets on the
Comparisons
Examples 3 } else if(huskersScore < opponentScore) { same line as keywords
Logical 4 printf("Huskers Lose!\n");
Operators Closing curly brackets on the
5 } else {
Pitfalls
same indentation level
6 printf("Tie, let's go to overtime!\n");
Exercises 7 } All blocks are indented at the
same level
Consistency is the most
important thing
Introduction
Conditionals
Logical
Operators
Negation
Flag Variables
Logical And
Part III: Logical Operators
Logical Or
Examples
Negation, Logical And, Logical Or
Pitfalls
Exercises
Negation Operator
Introduction
Conditionals
Any logical statement can be negated using !
Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples
Pitfalls
Exercises
Negation Operator
Introduction
Conditionals
Any logical statement can be negated using !
Logical
Operators Negation of (a == b) can be !(a == b)
Negation
Flag Variables
Logical And
Logical Or
Examples
Pitfalls
Exercises
Negation Operator
Introduction
Conditionals
Any logical statement can be negated using !
Logical
Operators Negation of (a == b) can be !(a == b)
Negation
Flag Variables
Logical And Negation of (a <= b) can be !(a <= b)
Logical Or
Examples
Pitfalls
Exercises
Negation Operator
Introduction
Conditionals
Any logical statement can be negated using !
Logical
Operators Negation of (a == b) can be !(a == b)
Negation
Flag Variables
Logical And Negation of (a <= b) can be !(a <= b)
Logical Or
Examples Better to use: (a != b) and (a > b)
Pitfalls
Exercises
Negation Operator
Introduction
Conditionals
Any logical statement can be negated using !
Logical
Operators Negation of (a == b) can be !(a == b)
Negation
Flag Variables
Logical And Negation of (a <= b) can be !(a <= b)
Logical Or
Examples Better to use: (a != b) and (a > b)
Pitfalls
Exercises
Usually a negation is used on a “flag” variable: a variable that simply holds a
truth value (true or false)
Flag Variables
Introduction
Pitfalls
Exercises
Flag Variables
Introduction
Pitfalls
Exercises
Flag Variables
Introduction
Pitfalls
Exercises
Flag Variables
Introduction
Pitfalls
Exercises
Flag Variables
Introduction
Introduction
Introduction
Conditionals
Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples
Pitfalls
Exercises
Logical And
Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples
Pitfalls
Exercises
Logical And
Logical
Operators
Negation
1 if(subTotal >= 50.0 && isPreferredMember) {
Flag Variables
Logical And
2 discount = .20;
Logical Or 3 shipping = 0;
Examples
Pitfalls
4 } else if(subTotal >= 50.0 && !isPreferredMember) {
5 discount = 0.0;
Exercises
6 shipping = 0;
7 } else {
8 discount = 0.0;
9 shipping = 10.50;
10 }
Logical Or
Introduction
Conditionals
Logical Or operator: ||
Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples
Pitfalls
Exercises
Logical Or
Introduction
Conditionals
Logical Or operator: ||
Logical
Operators
Negation
Evaluates to true only if at least one of its operands evaluate to true
Flag Variables
Logical And
Logical Or
Examples
Pitfalls
Exercises
Logical Or
Introduction
Conditionals
Logical Or operator: ||
Logical
Operators
Negation
Evaluates to true only if at least one of its operands evaluate to true
Flag Variables
Logical And
Logical Or
Examples
1 if(isStudent || isPreferredMember) {
Pitfalls
2 discount = .20;
Exercises
3 }
Examples
Pitfalls
9 if(a > 10 || a < 20) {
10 //...
Exercises
11 }
12
13 if(a == b || a < 10) {
14 //...
15 }
Introduction
Conditionals
Logical
Operators
Pitfalls
Non-Numerical
Part IV: Pitfalls
Comparisons
Precedence
Common Errors & Misconceptions
Rules
Short Circuiting
Exercises
Pitfall
Incorrect Complex Logic
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting
Exercises
Pitfall
Incorrect Complex Logic
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting
Introduction
Conditionals
Exercises
The above code will compile, run, but will give incorrect results
Pitfall
Confusing Comparisons & Assignments
Exercises
The above code will compile, run, but will give incorrect results
a = 10 results in an assignment of the value 10 to the variable a
Pitfall
Confusing Comparisons & Assignments
Exercises
The above code will compile, run, but will give incorrect results
a = 10 results in an assignment of the value 10 to the variable a
A value of 10 evaluates to true
Pitfall
Confusing Comparisons & Assignments
Exercises
The above code will compile, run, but will give incorrect results
a = 10 results in an assignment of the value 10 to the variable a
A value of 10 evaluates to true
The if body gets executed regardless of the original value of a
Pitfall
Improper Semicolons
Conditionals 1 int a = 5;
2
Logical
Operators 3 if(a == 10); {
Pitfalls 4 printf("a is ten!\n");
Non-Numerical
Comparisons 5 }
Precedence
Rules
Short Circuiting
Exercises
Conditionals 1 int a = 5;
2
Logical
Operators 3 if(a == 10); {
Pitfalls 4 printf("a is ten!\n");
Non-Numerical
Comparisons 5 }
Precedence
Rules
Short Circuiting
Exercises
Conditionals 1 int a = 5;
2
Logical
Operators 3 if(a == 10); {
Pitfalls 4 printf("a is ten!\n");
Non-Numerical
Comparisons 5 }
Precedence
Rules
Short Circuiting
Exercises
Exercises
Non-Numerical Comparisons
1 if(name == "Chris") {
2 printf("Greetings, Professor.\n");
3 }
Non-Numerical Comparisons
1 if(name == "Chris") {
2 printf("Greetings, Professor.\n");
3 }
Introduction
Conditionals
The logical and && is evaluated before the logical or ||
Logical
Operators
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules
Short Circuiting
Exercises
Precedence Rules
Introduction
Conditionals
The logical and && is evaluated before the logical or ||
Logical
Operators
The following are not equivalent:
Pitfalls
Non-Numerical
a && (b || c)
Comparisons
Precedence a && b || c
Rules
Short Circuiting
Exercises
Precedence Rules
Introduction
Conditionals
The logical and && is evaluated before the logical or ||
Logical
Operators
The following are not equivalent:
Pitfalls
Non-Numerical
a && (b || c)
Comparisons
Precedence a && b || c
Rules
Short Circuiting
Use parentheses when necessary
Exercises
Precedence Rules
Introduction
Conditionals
The logical and && is evaluated before the logical or ||
Logical
Operators
The following are not equivalent:
Pitfalls
Non-Numerical
a && (b || c)
Comparisons
Precedence a && b || c
Rules
Short Circuiting
Use parentheses when necessary
Exercises
Best practice: use them even when not necessary to express intent
Short-Circuiting
Introduction
Conditionals
Logical
Consider a logical and: a && b
Operators
Pitfalls
Non-Numerical
If a evaluates to false, it does not matter what b evaluates to
Comparisons
Precedence
Rules
Short Circuiting
Exercises
Short-Circuiting
Introduction
Conditionals
Logical
Consider a logical and: a && b
Operators
Pitfalls
Non-Numerical
If a evaluates to false, it does not matter what b evaluates to
Comparisons
Precedence
Rules
Since a is false, the entire expression is false
Short Circuiting
Exercises
Short-Circuiting
Introduction
Conditionals
Logical
Consider a logical and: a && b
Operators
Pitfalls
Non-Numerical
If a evaluates to false, it does not matter what b evaluates to
Comparisons
Precedence
Rules
Since a is false, the entire expression is false
Short Circuiting
Consequently: b is not evaluated/executed
Exercises
Short-Circuiting
Introduction
Conditionals
Pitfalls
Non-Numerical
If a evaluates to true, it does not matter what b evaluates to
Comparisons
Precedence
Rules
Short Circuiting
Exercises
Short-Circuiting
Introduction
Conditionals
Pitfalls
Non-Numerical
If a evaluates to true, it does not matter what b evaluates to
Comparisons
Precedence
Rules
Since a is true, the entire expression is true
Short Circuiting
Exercises
Short-Circuiting
Introduction
Conditionals
Pitfalls
Non-Numerical
If a evaluates to true, it does not matter what b evaluates to
Comparisons
Precedence
Rules
Since a is true, the entire expression is true
Short Circuiting
Consequently: b is not evaluated/executed
Exercises
Short-Circuiting
Introduction
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules
Short Circuiting
Exercises
Short-Circuiting
Introduction
Exercises
Short-Circuiting
Introduction
Conditionals
Logical
Operators
Pitfalls
Exercises
Part V: Exercises
Exercise
Introduction
Conditionals
Logical
Operators
Pitfalls
Write a code snippet that determines the maximum of three integer values.
Exercises
Exercise
Introduction Write a program that reads a decibel level from the user and gives them a
Conditionals description of the sound level based on the following categories.
Logical
Operators
0 - 60 Quiet
Pitfalls
Exercises
61 - 70 Conversational
71 - 90 Loud
91 - 110 Very Loud
111 - 129 Dangerous
130 - 194 Very Dangerous
Exercise
Introduction
Conditionals
Logical
Operators
Pitfalls
Exercises
Figure: Examples of Equilateral, Isosceles, and Scalene triangles
3 sides are a valid triangle only if the sum of the length of any two sides is strictly
greater than the length of the third side.
Write a program to determine if 3 inputs form a valid triangle and if so, what type.