Download presentation
Presentation is loading. Please wait.
Published byMatthew Noah Lindsey Modified over 9 years ago
1
CSC 107 – Programming For Science
2
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
3
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 Code could/does contain { add to indentation Decrease indentation each } that found in the code if, else if, or else line has opening brace Always use braces to prevent having to fix bugs Limit space, closing brace on else/else if line Closing brace on own line otherwise (like at end)
4
Coding Style Example #include using namespace std; int main(void) { int dcScore = 23, nycScore = 26; if (dcScore > nycScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < nycScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }
5
Coding Style Example #include using namespace std; int main(void) { int dcScore = 23, nycScore = 26; if (dcScore > nycScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < nycScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }
6
Coding Style Example #include using namespace std; int main(void) { int dcScore = 23, nycScore = 26; if (dcScore > nycScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < nycScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }
7
Coding Style Example #include using namespace std; int main(void) { int dcScore = 23, nycScore = 26; if (dcScore > nycScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < nycScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }
8
Coding Style Example #include using namespace std; int main(void) { int dcScore = 23, nycScore = 26; if (dcScore > nycScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < nycScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }
9
Coding Style Example #include using namespace std; int main(void) { int dcScore = 23, nycScore = 26; if (dcScore > nycScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < nycScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }
10
if (…) statement Parenthesis first, evaluate this expression Execute next statement, when expression true When expression is false, skips over statement int nyc = 32, dc = 1; bool gang = (nyc > 12) && (dc == 1); if (nyc >= dc) { cout << “You lie" << endl; } if (!gang) { cout << "That ain't right." << endl; }
11
I Want More! Add opening brace ( { ) after closing parenthesis Can now write all statements to execute Add closing brace ( } ) to show where block ends If expression false, execution restarts at that point if (sqrt(x) == 3.0) { someVar = 45 + 65 * 12; cout << "root of x = 3" << endl; cout << "So, x = 9" << endl; }
12
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 comes 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 with if & no else yesno
13
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; }
14
Many Choices May want to choose from many possibilities
15
Many Choices May want to choose from many possibilities Could nest if – else statements inside one another if (a > 3) { cout 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; } } }
16
Many Choices May want to choose from many possibilities Could nest if – else statements inside one another Or easier & nicer if – else if – else statement if (a > 3) { cout 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; }
17
if – else if – else Usage Must begin with if statement at the start This is required; what would we be saying else to? Only Only required part of this entire process Can then have zero or more else if s Tests can be anything; do not have to be related Until one is true, will be examined one-by-one Execute 1 st clause where true expression is found Only at the very end can have else clause If nothing else matches then else is executed
18
if – else if – else Example if (wealth > 1000000) { cout 100000) { cout -100000) { cout 10000) { cout 0) { cout << "Good luck" << endl; }
19
if – else if – else Example if (wealth > 1000000) { cout 100000) { cout -100000) { cout 10000) { cout 0) { cout << "Good luck" << endl; }
20
if – else if – else Example if (wealth > 1000000) { cout 100000) { cout -100000) { cout 10000) { cout 0) { cout << "Good luck" << endl; } else { cout << "Government bailout!" << endl; }
21
if – else if – else Example if (wealth > 1000000) { cout 100000) { cout -100000) { cout 10000) { cout 0) { cout << "Good luck" << endl; } else { cout << "Government bailout!" << endl; }
22
if – else if – else Example if (wealth > 1000000) { cout 100000) { cout 10000) { cout 0) { cout -100000) { cout << "File for bankruptcy" << endl; } else { cout << "Government bailout!" << endl; }
23
Your Turn Get in groups of 3 & work on following activity
24
For Next Lecture Read sections 7.4, 7.6 – 7.7 for Wednesday What if we want to run multiple clauses? Is something easier for testing lots of equality? How can we break lots of things? Week #4 weekly assignment available now Problems available on Angel If problem takes more than 10 minutes, TALK TO ME!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.