CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special...
-
Upload
lydia-simmons -
Category
Documents
-
view
218 -
download
0
Transcript of CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special...
![Page 1: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/1.jpg)
LECTURE 10:IF-ELSE IF-ELSE STATEMENTS
CSC 107 – Programming For Science
![Page 2: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/2.jpg)
Announcements
Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the
Tutoring Center Can answer questions you have about
material Also very good at helping with weekly
assignments Directly across from my office, so I can also
help My office hours are minimum time I am
around During the day, often (usually) around at
other times Please ask questions and get any help you
need No RSVPs; could also take candy I have in
my office
![Page 3: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/3.jpg)
Today’s Goals
Be able to use if – else if – else statements Know how to order choices to work properly Explain why if & else if conditions not
related Correctly use braces ({ }) and make code
pretty Explain when & why if – else if – else used
![Page 4: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/4.jpg)
if (…) statement
1st evaluates boolean expression in parenthesis
Executes next statement if expression is true When expression is false, skips over the
statementint num = -32;
if (num < 0) {
cout << "Sorry Dave, I can't do that."<<endl;
}
cout << "What is the next item?" << endl;
cin >> val;
if ((val < num) || (val <= 0)) {
cout << "That was a poor choice“ << endl;
}
![Page 5: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/5.jpg)
if (…) statement
1st evaluates boolean expression in parenthesis
Executes next statement if expression is true When expression is false, skips over the
statement This leads to one very common bug
; is statement (a boring statement, but…)
if (dc > 20);cout << "Huh?"
![Page 6: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/6.jpg)
else statement
Often want to perform one of two actions Execute some statements when condition is
true But when it is false, do some other work Continue as normal afterward, no matter
the path In these cases, can use if-else
statement else must come immediately after if's ;
or } Like if, else should always have {}
Cannot have else without if to start process In C++, there is no problem having if & no else
falsetrue
![Page 7: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/7.jpg)
if - else Examples
if (value > 0) { cout << "Its positive" << endl;} else { value = abs(value);}
if ((year % 4 == 0) && ((year % 100) != 0)) { daysInYear = 366; cout << "Not quite right, but close enough"; cout << endl; leapYear = true;} else { leapYear = false; daysInYear = 365;
}
![Page 8: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/8.jpg)
Many Choices
May want to choose from many possibilities
![Page 9: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/9.jpg)
Many Choices
May want to choose from many possibilities Could nest if – else statements inside one
anotherif (a > 3) { cout << "a greater than 3" << endl;} else { if (b > 10) { cout << "b greater than 10" << endl; } else { if (c == -23) { cout << "c is equal to -23" << endl; } else { cout << "This really sucks" << endl; cout << "Sorry its stupid" << endl; } }}
![Page 10: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/10.jpg)
Many Choices
May want to choose from many possibilities Could nest if – else statements inside one
another Or easier & nicer if – else if – else
statementif (a > 3) { cout << "a greater than 3" << endl;} else if (b > 10) { cout << "b greater than 10" << endl;} else if (c == -23) { cout << "c is equal to -23" << endl;} else { cout << "This really sucks" << endl; cout << "Sorry its stupid" << endl;}
![Page 11: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/11.jpg)
if – else if – else Usage
Must begin with if statement at the start This is required; what would we be saying else to?
Only required part of this entire process Can then have zero or more else ifs
Tests can be anything; do not have to be related
Until one is true, will be examined one-by-one
Execute 1st clause where true expression is found
Only at the very end can have else clause If nothing else matches then else is
executed
![Page 12: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/12.jpg)
if – else if – else Example
if (wealth > 1000000) { cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) { cout << "Wow, you're comfortable" << endl;} else { cout << "Government bailout!" << endl;} else if (wealth > -100000) { cout << "File for bankruptcy" << endl;} else if (wealth > 10000) { cout << "Could be worse" << endl;} else if (wealth > 0) { cout << "Good luck" << endl;}
![Page 13: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/13.jpg)
if – else if – else Example
if (wealth > 1000000) { cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) { cout << "Wow, you're comfortable" << endl;} else { cout << "Government bailout!" << endl;} else if (wealth > -100000) { cout << "File for bankruptcy" << endl;} else if (wealth > 10000) { cout << "Could be worse" << endl;} else if (wealth > 0) { cout << "Good luck" << endl;}
![Page 14: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/14.jpg)
if – else if – else Example
if (wealth > 1000000) { cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) { cout << "Wow, you're comfortable" << endl;} else if (wealth > -100000) { cout << "File for bankruptcy" << endl;} else if (wealth > 10000) { cout << "Could be worse" << endl;} else if (wealth > 0) { cout << "Good luck" << endl;} else { cout << "Government bailout!" << endl;}
![Page 15: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/15.jpg)
if – else if – else Example
if (wealth > 1000000) { cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) { cout << "Wow, you're comfortable" << endl;} else if (wealth > -100000) { cout << "File for bankruptcy" << endl;} else if (wealth > 10000) { cout << "Could be worse" << endl;} else if (wealth > 0) { cout << "Good luck" << endl;} else { cout << "Government bailout!" << endl;}
![Page 16: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/16.jpg)
if – else if – else Example
if (wealth > 1000000) { cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) { cout << "Wow, you're comfortable" << endl;} else if (wealth > 10000) { cout << "Could be worse" << endl;} else if (wealth > 0) { cout << "Good luck" << endl;} else if (wealth > -100000) { cout << "File for bankruptcy" << endl;} else { cout << "Government bailout!" << endl;}
![Page 17: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/17.jpg)
Spacing in a Program
C++ ignores spaces in a program This also means where newlines placed
ignored #define & text in quotes major exceptions
to rule This can lead to interesting code
choices
![Page 18: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/18.jpg)
Coding Style
Use consistent size to indent (I use 2 spaces) Not required, but makes it much easier to
read code Indent code within braces ({}) to show
structure If line could or does contain { add to
indentation Decrease indentation each } should be on a
line if, else if, or else line has opening
brace Always use braces to prevent having to fix
bugs Save space: closing brace on else/else if
line Closing brace on own line at end of the
statement
![Page 19: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/19.jpg)
Coding Style Example
#include <iostream>using namespace std;
int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl;
} else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; }
}
cout << "And now code continues" << endl; return 0;}
![Page 20: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/20.jpg)
Coding Style Example
#include <iostream>using namespace std;
int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl;
} else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; }
}
cout << "And now code continues" << endl; return 0;}
![Page 21: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/21.jpg)
Coding Style Example
#include <iostream>using namespace std;
int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl;
} else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; }
}
cout << "And now code continues" << endl; return 0;}
![Page 22: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/22.jpg)
Coding Style Example
#include <iostream>using namespace std;
int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl;
} else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; }
}
cout << "And now code continues" << endl; return 0;}
![Page 23: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/23.jpg)
Coding Style Example
#include <iostream>using namespace std;
int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl;
} else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; }
}
cout << "And now code continues" << endl; return 0;}
![Page 24: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/24.jpg)
Coding Style Example
#include <iostream>using namespace std;
int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl;
} else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; }
}
cout << "And now code continues" << endl; return 0;}
![Page 25: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/25.jpg)
Your Turn
Get in groups of 3 & work on following activity
![Page 26: CSC 107 – Programming For Science. Announcements Tutors available MTWR in WTC206/WTC208 Special lab (with Macs) & not in the Tutoring Center Can.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649e6b5503460f94b69c49/html5/thumbnails/26.jpg)
For Next Lecture
Read sections 7.4, 7.6 – 7.7 for Friday What if we want to run multiple clauses? Is something easier for testing lots of
equality? How can we break lots of things?
Week #3 weekly assignment due yesterday But could still submit today by using virtual
extension
Week #4 weekly assignment also on Angel If problem takes more than 10 minutes,
TALK TO ME!