More Conditionals Nested Conditionals Switch Statements: ITP 165 - Fall 2015 Week 2, Lecture 2
More Conditionals Nested Conditionals Switch Statements: ITP 165 - Fall 2015 Week 2, Lecture 2
More Conditionals Nested Conditionals Switch Statements: ITP 165 - Fall 2015 Week 2, Lecture 2
Switch Statements
ITP 165 Fall 2015
Week 2, Lecture 2
Required
braces
if (x > 0)
{
std::cout << "Stuff";
}
else
else keyword
{
std::cout << "Other";
}
if part the
same
return 0;
// Scope for "main" ends
equals 5.";
equals 10.";
equals 20.";
If x = 7, it
satisfies these
conditions
Because of the
else if, only
the first condition
is executed
Nesting if statements
Since an if statement is a type of statement, we can nest them.
For example:
int x = 50;
if (x < 0)
{
if (x < -100)
{
std::cout << "X is really negative...";
}
else
{
std::cout << "X is a little negative...";
}
}
Spaghetti Code
This term is used sometimes if you have lots of confusing
conditionals and its really unclear which way the code flows
Switch statement
The previous slides code can be rewritten as a switch statement:
switch (select)
{
case 1:
// Option 1
break;
case 2:
// Option 2
break;
case 3:
// Option 3
break;
case 4:
// Option 4
break;
default:
// Invalid option
break;
}
Required
open/close
braces
switch (select)
{
case 1:
// Option 1
break;
default:
// Invalid option
break;
}
variable we are
switching on
(parenthesis
required)
value
corresponding
to this case
Required colon
break followed
by semi-colon
(these are
extremely
important!!!)
case 1:
// Option 1
break;
Any number of statements to
execute if this is the selected
case.
Default case
The default case is whats executed if the variable was not equal to
any of the other cases
Generally, its a good practice to have a default case
switch (select)
{
case 1:
// Option 1
break;
default:
// Invalid option
break;
}
In this example,
the default
case is selected
in the event that
select != 1
Switch Caveat #1
switch only works on whole numbers
This means that if you must select based on a double or string, you
cant use a switch
For example, this is not valid:
double test;
switch (test) // Error: Not a whole number
{
case 1.0:
break;
default:
break;
}
Switch Caveat #2
Switch only works in the instance where the condition is that the
value equals a specific number
If you need complex behavior like greater than 0 but less than
100 you need to use if/else if statements
Switch Caveat #3
Dont forget the break after every case
Otherwise, you will have weird, behavior
For example, this is wrong:
#include <iostream>
int main()
{
int select = 0;
std::cin >> select;
switch (select)
{
case 1:
std::cout << "Option 1" << std::endl;
case 2:
std::cout << "Option 2" << std::endl;
default:
std::cout << "Invalid option" << std::endl;
}
return 0;
}
?!?!?!?!
Note:
Wont always work the first time around. You may have to play a little with it
first to see what can be written outside.
Notes on Conditionals
You can compare text too
int main()
{
std::string name = "Raymond";
if (name == "Raymond")
{
std::cout << name << " is an awesome name bro.";
}
else
{
std::cout << name << " is a lame name bro.";
}
Notes on Conditionals
We get
Notes on Conditionals
Dont forget, C++ is case-sensitive
Notes on Conditionals
If you want to satisfy two conditions at once, you can combine the
conditions using a Boolean operator
For example:
0 X 10
This is like saying X is greater than or equal to 0 AND X is less
than or equal to 10
((X >= 0) && (X <= 10))
Notes on Conditionals
If we flip the range from the last slide, we get:
X0
X 10
Notes on Conditionals
Briefly mentioned order of operations for conditionals
Operator
Description
< or <=
> or >=
== or !=
&
Bitwise AND
Bitwise OR
&&
Logical AND
||
Logical OR
Notes on Conditionals
Dont always rely on the order of operations to sort out which
evaluation to do first
Always make your conditionals explicitly clear
Use parentheses!!!
Float Comparison
Remember when we said decimals are approximations?
Float Comparison
We get
Float Comparison
How do we deal with our minds being blown?
If decimals are approximations, there is some wiggle-room we can
play with
Referred to as a tolerance
Simply put:
Tolerance is a percentage of a value such that
Tolerance
Think of tolerance like a range
The actual value is between the expected value minus the
tolerance and the expected value plus the tolerance
Tolerance
Whats a good tolerance?
It depends
Generally, the smaller the better
But how small is too small?
Lab Practical #3