0% found this document useful (0 votes)
4 views109 pages

Lecture Conditionals

Uploaded by

atif83837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views109 pages

Lecture Conditionals

Uploaded by

atif83837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

Introduction

Computer Science I
Conditionals

Logical
Operators
Conditionals
Pitfalls

Exercises

Dr. Chris Bourke


[email protected]
Outline

Introduction
1. Introduction
Conditionals

Logical
Operators
2. Conditionals
Pitfalls

Exercises 3. Logical Operators

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

We need a way to compare the value stored in variables


Introduction
Conditionals
Boolean
Statements
Numeric
Comparisons
Complex Logic
Statements

Conditionals

Logical
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Numeric
Comparisons
Complex Logic
Statements

Conditionals

Logical
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons
Complex Logic
Statements

Conditionals

Logical
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons Comparisons:
Complex Logic
Statements

Conditionals

Logical
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons Comparisons:
Complex Logic
Statements Are two values equal or not equal?
Conditionals

Logical
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons Comparisons:
Complex Logic
Statements Are two values equal or not equal?
Conditionals Is one value greater than or equal to/lesser than or equal to another?
Logical
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons Comparisons:
Complex Logic
Statements Are two values equal or not equal?
Conditionals Is one value greater than or equal to/lesser than or equal to another?
Logical Is one value strictly greater/lesser than another?
Operators

Pitfalls

Exercises
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons Comparisons:
Complex Logic
Statements Are two values equal or not equal?
Conditionals Is one value greater than or equal to/lesser than or equal to another?
Logical Is one value strictly greater/lesser than another?
Operators

Pitfalls Standard mathematical notations


Exercises
= 6= ≥ ≤ > <
Numeric Comparisons

We need a way to compare the value stored in variables


Introduction Compare the relative value of two variables
Conditionals
Boolean
Statements
Compare the value stored in one variable with a fixed value (literal)
Numeric
Comparisons Comparisons:
Complex Logic
Statements Are two values equal or not equal?
Conditionals Is one value greater than or equal to/lesser than or equal to another?
Logical Is one value strictly greater/lesser than another?
Operators

Pitfalls Standard mathematical notations


Exercises
= 6= ≥ ≤ > <

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

Exercises Uses the keyword else


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

Exercises Uses the keyword else


Behavior: if <condition> evaluates to true code block A is executed
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

Exercises Uses the keyword else


Behavior: if <condition> evaluates to true code block A is executed
If <condition> evaluates to false, code block B is executed
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

Exercises Uses the keyword else


Behavior: if <condition> evaluates to true code block A is executed
If <condition> evaluates to false, code block B is executed
The two code blocks are mutually exclusive
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

Exercises Uses the keyword else


Behavior: if <condition> evaluates to true code block A is executed
If <condition> evaluates to false, code block B is executed
The two code blocks are mutually exclusive
A generalization of the if statement
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
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

Conditionals C has no “boolean variables”


Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples

Pitfalls

Exercises
Flag Variables

Introduction

Conditionals C has no “boolean variables”


Logical Any numerical value can be treated as a boolean value
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples

Pitfalls

Exercises
Flag Variables

Introduction

Conditionals C has no “boolean variables”


Logical Any numerical value can be treated as a boolean value
Operators
Negation
Flag Variables
0 is false
Logical And
Logical Or
Examples

Pitfalls

Exercises
Flag Variables

Introduction

Conditionals C has no “boolean variables”


Logical Any numerical value can be treated as a boolean value
Operators
Negation
Flag Variables
0 is false
Logical And
Logical Or Any non-zero value is true
Examples

Pitfalls

Exercises
Flag Variables

Introduction

Conditionals C has no “boolean variables”


Logical Any numerical value can be treated as a boolean value
Operators
Negation
Flag Variables
0 is false
Logical And
Logical Or Any non-zero value is true
Examples

Pitfalls 3, 3.5, 3.14, -10 are all true


Exercises
Flag Variables

Introduction

Conditionals C has no “boolean variables”


Logical Any numerical value can be treated as a boolean value
Operators
Negation
Flag Variables
0 is false
Logical And
Logical Or Any non-zero value is true
Examples

Pitfalls 3, 3.5, 3.14, -10 are all true


Exercises Convention: use 1 as true
Flag Variables

Introduction

Conditionals C has no “boolean variables”


Logical Any numerical value can be treated as a boolean value
Operators
Negation
Flag Variables
0 is false
Logical And
Logical Or Any non-zero value is true
Examples

Pitfalls 3, 3.5, 3.14, -10 are all true


Exercises Convention: use 1 as true
Best practice: only use int variables as booleans
Flag Variables
Example

1 //flag variable to indicate if someone is a


2 //student (true) or not (false)
Introduction
3 int isStudent;
4
Conditionals
5 //set the variable to true:
Logical
Operators
6 isStudent = 1;
Negation 7
Flag Variables
Logical And 8 //they are not a student:
Logical Or 9 isStudent = 0;
Examples
10
Pitfalls
11 if(isStudent) {
Exercises
12 printf("You get a student discount!\n");
13 }
14
15 //using a negation
16 if(!isStudent) {
17 printf("You pay full price!\n");
18 }
Logical And

Logical And operator: &&


Introduction

Conditionals

Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples

Pitfalls

Exercises
Logical And

Logical And operator: &&


Introduction Evaluates to true only if both operands evaluate to true
Conditionals

Logical
Operators
Negation
Flag Variables
Logical And
Logical Or
Examples

Pitfalls

Exercises
Logical And

Logical And operator: &&


Introduction Evaluates to true only if both operands evaluate to true
Conditionals

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

1 if(a > 10 && a < 20) {


2 //...
Introduction 3 }
Conditionals 4
Logical 5 if(a == b && a < 10) {
Operators
Negation
6 //...
Flag Variables
Logical And
7 }
Logical Or 8
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

Consider the following code:

Introduction 1 if(0 <= a <= 10) {


Conditionals 2 printf("Value is within range!\n");
Logical 3 }
Operators

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

Consider the following code:

Introduction 1 if(0 <= a <= 10) {


Conditionals 2 printf("Value is within range!\n");
Logical 3 }
Operators

Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting

Exercises What happens when a = 20 ?


Pitfall
Incorrect Complex Logic

Consider the following code:

Introduction 1 if(0 <= a <= 10) {


Conditionals 2 printf("Value is within range!\n");
Logical 3 }
Operators

Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting

Exercises What happens when a = 20 ?


First comparison: 0 <= 20
Pitfall
Incorrect Complex Logic

Consider the following code:

Introduction 1 if(0 <= a <= 10) {


Conditionals 2 printf("Value is within range!\n");
Logical 3 }
Operators

Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting

Exercises What happens when a = 20 ?


First comparison: 0 <= 20
Evaluates to true ( 1 )
Pitfall
Incorrect Complex Logic

Consider the following code:

Introduction 1 if(0 <= a <= 10) {


Conditionals 2 printf("Value is within range!\n");
Logical 3 }
Operators

Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting

Exercises What happens when a = 20 ?


First comparison: 0 <= 20
Evaluates to true ( 1 )
Second comparison: 1 <= 10 (true)
Pitfall
Incorrect Complex Logic

Consider the following code:

Introduction 1 if(0 <= a <= 10) {


Conditionals 2 printf("Value is within range!\n");
Logical 3 }
Operators

Pitfalls
Non-Numerical
Comparisons
Precedence
Rules The above code will compile, will execute, but will not work for certain values
Short Circuiting

Exercises What happens when a = 20 ?


First comparison: 0 <= 20
Evaluates to true ( 1 )
Second comparison: 1 <= 10 (true)
Incorrect result
Pitfall
Incorrect Complex Logic

Introduction

Conditionals

Logical Solution: break up your conditions using a &&


Operators

Pitfalls 1 if(0 <= a && a <= 10) {


Non-Numerical
Comparisons 2 printf("Value is within range!\n");
Precedence
Rules
Short Circuiting
3 }
Exercises
Pitfall
Confusing Comparisons & Assignments

Consider the following code:


Introduction
1 int a = 5;
Conditionals 2
Logical 3 if(a = 10) {
Operators
4 printf("a is ten\n");
Pitfalls
Non-Numerical 5 }
Comparisons
Precedence
Rules
Short Circuiting

Exercises
The above code will compile, run, but will give incorrect results
Pitfall
Confusing Comparisons & Assignments

Consider the following code:


Introduction
1 int a = 5;
Conditionals 2
Logical 3 if(a = 10) {
Operators
4 printf("a is ten\n");
Pitfalls
Non-Numerical 5 }
Comparisons
Precedence
Rules
Short Circuiting

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

Consider the following code:


Introduction
1 int a = 5;
Conditionals 2
Logical 3 if(a = 10) {
Operators
4 printf("a is ten\n");
Pitfalls
Non-Numerical 5 }
Comparisons
Precedence
Rules
Short Circuiting

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

Consider the following code:


Introduction
1 int a = 5;
Conditionals 2
Logical 3 if(a = 10) {
Operators
4 printf("a is ten\n");
Pitfalls
Non-Numerical 5 }
Comparisons
Precedence
Rules
Short Circuiting

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

Consider the following code:


Introduction

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

Semicolon (in general) only go after executable statements


Pitfall
Improper Semicolons

Consider the following code:


Introduction

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

Semicolon (in general) only go after executable statements


Above code will compile, will run, but will not give the correct results
Pitfall
Improper Semicolons

Consider the following code:


Introduction

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

Semicolon (in general) only go after executable statements


Above code will compile, will run, but will not give the correct results
Conditional statement is bound to an empty statement
Non-Numerical Comparisons

You can compare single char values with character literals:


Introduction 1 char initial = 'C';
Conditionals 2

Logical 3 if(initial == 'c' || initial == 'C') {


Operators
4 //...
Pitfalls
Non-Numerical
5 }
Comparisons
Precedence
Rules
Short Circuiting

Exercises
Non-Numerical Comparisons

You can compare single char values with character literals:


Introduction 1 char initial = 'C';
Conditionals 2

Logical 3 if(initial == 'c' || initial == 'C') {


Operators
4 //...
Pitfalls
Non-Numerical
5 }
Comparisons
Precedence
Rules
Short Circuiting
You cannot use equality and inequality operators on strings (sequences of
Exercises characters)

1 if(name == "Chris") {
2 printf("Greetings, Professor.\n");
3 }
Non-Numerical Comparisons

You can compare single char values with character literals:


Introduction 1 char initial = 'C';
Conditionals 2

Logical 3 if(initial == 'c' || initial == 'C') {


Operators
4 //...
Pitfalls
Non-Numerical
5 }
Comparisons
Precedence
Rules
Short Circuiting
You cannot use equality and inequality operators on strings (sequences of
Exercises characters)

1 if(name == "Chris") {
2 printf("Greetings, Professor.\n");
3 }

The above will never give correct results


Precedence Rules

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

Logical Consider a logical and: a || b


Operators

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

Logical Consider a logical and: a || b


Operators

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

Logical Consider a logical and: a || b


Operators

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

Conditionals Short circuiting is common to the vast majority of programming language


Logical
Operators

Pitfalls
Non-Numerical
Comparisons
Precedence
Rules
Short Circuiting

Exercises
Short-Circuiting

Introduction

Conditionals Short circuiting is common to the vast majority of programming language


Logical
Operators
Historic reasons
Pitfalls
Non-Numerical
Comparisons
Precedence
Rules
Short Circuiting

Exercises
Short-Circuiting

Introduction

Conditionals Short circuiting is common to the vast majority of programming language


Logical
Operators
Historic reasons
Pitfalls Common idiom in many programming languages:
Non-Numerical
Comparisons
Precedence
Rules
Short Circuiting
1 if(a != NULL && a[0] == 10) {
Exercises
2 //...
3 }
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.

You might also like