Presentation is loading. Please wait.

Presentation is loading. Please wait.

TIPS: How to Be Successful

Similar presentations


Presentation on theme: "TIPS: How to Be Successful"— Presentation transcript:

1 TIPS: How to Be Successful
Strategies (and how not to lose points for silly things …)

2 Formatting Indent 1 more level at each new block
Put an empty line between all functions and main Put these braces (curly brackets) on their own line: { } Exception: do –while do }while(….);

3 Basic Formatting – body indents by 1
#include <iostream> using namespace std; int main() { int myint; myint = 3; cout << myint << endl; return(0); }

4 Basic Formatting: Each new control indents 1
#include <iostream> using namespace std; int main() { for(int i=5; i <30; i++) cout << "i= "<< i << endl; } return(0);

5 Comments A MINIMUM of one line per function Example: /* */
Include: describe inputs, return values, and what the function does, does not do, and errors it handles Example: /* int2Greeting takes military time (0 to <2400) and returns a string saying Good Morning, Afternoon or Evening. It returns an error message for times between 2200 and 0400 (10PM and 4AM). A value out of range (i.e. <0 or over 2400) also returns an error in the string. */

6 Comments Should occur before any large chunk of code Example: /* */
Summarize the actions Example: /* This next section computes the trajectory to Mars in 3 steps 1) … 2) … 3)… */

7 Structuring 'if' conditions
if((time >= 0 ) && (time < 1200)) if((time >=1200) && (time < 1600)) if((time >=1600) && (time < 2400)) NOTE the symmetry of the statements. >= all on the left side < all on the right side time (or any continuous set of values) should be listed in order. Don't jump around or you might miss something & is less readable.

8 Naming Variables More important than you think
Choose a NOUN not a verb, or indication of a verb! Good: multiplier balance name month total OK: firstUserInt numToMultiply Poor: multiply add plus Two Joe

9 Naming Variables to match use & type
int sum = a*b; float countOfPeople = 5; int numberTickets; char destination; char city; double totalCost; int ticketPrice; int ticketCost; string firstName; There are often several good choices for variable names. For example, ticketPrice or ticketCost destination or city Sometimes there are no great choices but try your best: typeOfDay

10 Hints on How to choose a loop
for: Counting or range is involved, e.g. count from 1 to 10. A value calculated a number of times. OFTEN: use a temporary value to count from the min value to the max value. If you do not have: an initial value, a test, AND an increment Do not use the 'for loop’. do-while: Do at least once e.g. getting input from a user (until it is right). Keep going (forever?) until the user wants to end. (Or the condition is met.) while: May or may not run through a loop. Maybe many times, maybe none. At some point, you may want to loop forever. This is my choice while (true).

11 If you create an infinite loop
And especially if you plan to escape the loop using: a break; or return (0) STOP! Do Not Do it! YOU CHOSE THE WRONG LOOP STRUCTURE OR The logic is wrong. Do not proceed.

12 Make use of partial printing
You have an old value to print and you update it soon. Print by parts cout << "num = " <<num << " "; num *=4; cout << "result = " << num<< endl;

13 Rules of Thumb (typical good practice)
When you read in a value from the user, DO NOT CHANGE IT. (you may need it later on) If you want the value for a calculation assign it to a new variable. Example: int monthsToCalculateInterest; cout << "How many months do you want me to calculate? -> "; cin >> monthsToCalculateInterest; for (int currentMonth = 1;currentMonth < monthsToCalculateInterest;currentMonth++) { .. do stuff.. }

14 boolean Has value true or false Name it logically. Yes/No True/False
No quotes Name it logically. Yes/No True/False shouldStopLoop hasAllFeatures allParametersAreGood needMoreInfo isFinished bool needMoreInformation = true; while (needMoreInformation) { // ask for more information // at some point needMoreInformation = false; }

15 boolean –use it to simplify your code
If you have a situation that has only two states: Examples: bool IsDay;// (vs. is not day) bool Is Weekend;// (vs. weekday) 1) Read in 'raw' info cout << "time of flight? (0-2300)"; cin >> time; cout << "Week[e]nd or Week[d]ay? char weekChar; cin >> weekChar; 2) Convert to the Boolean if (time >=500) && (time<1700) isDay=true; else isDay = false; if (weekChar == 'e') isWeekend = true; isWeekend = false;

16 boolean –use it to simplify your code
if (isWeekend) if(isDay) ticketPrice = 180; else // isnight & weekend ticketPrice = 120; else // is not weekend, i.e. weekday ticketPrice = 150; ticketPrice = 100;

17 Naming Functions Typically, the functions are taking an action
Find a verb or verb phrase that captures what you are doing doAtask(… ); addPrimeFactorsOf(int x) Good: Get2Integers(int &a, int &b) Not very good (could mean anything): Integer(int &a, int &b)


Download ppt "TIPS: How to Be Successful"

Similar presentations


Ads by Google