320 likes | 408 Vues
This lecture covers data types, C++ program structure, and formatting basics in coding. Learn about ints, floats, bools, chars, strings, and more. Discover the importance of consistency, whitespace, variable naming, and comments in writing good code. Examples provided to enhance understanding and practice. Be consistent and start good habits now for clearer and more readable code.
E N D
Code Formatting By: Taylor Helsper
Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions
Introduction • What is this lecture? • Part of a CSE 4000 Independent Study Course • “Practical Issues in Software Engineering” • What’s the point? • To provide practical information to students in Software Engineering topics
Data Types • Int • Short for Integer • It's just a whole number • These are Ints • 10,000 • -1 • 314 • These aren't Ints • 1.1 • "HELLO" • 'f'
Data Types • Float / Doubles • Numbers with decimals! • What's the difference? Doubles are twice as big. • These are Floats/Doubles • 1.1 • 0.0 • 3.14159265 • These Aren't • "HELLO" • 'i'
Data Types • Bool • Short for Boolean • True/False • Any number != 0 equals True • These are Bools • 0 • 1 • True • False • These Aren't • 'HELLO' • 'f'
Data Types • Char • A single letter • These are Chars • 'p' • 'i' • ‘e' • These aren't • "Hello"
Data Types • String • 0 or more characters • These are strings • "I <3 Pie" • "Intermediate is the most amazing class in the history of classes" • "The internet is a series of tubes." • These aren't • 1 • 1.1 • True
Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions
C++ Program Structure • Main Function Name of the function where C++ starts Number of arguments to the program int main(int argc, char** argv) { cout << “Hello World!”; } Strings of the arguments
C++ Program Structure • Arguments Example
C++ Function Structure • Functions Name of the function Function parameters string get_message(int a) { string s = “”; if ( a < 10 ) { s = “Less than 10”; } else { s =“>= 10”; } return s; }
C++ Function Examples • Good functions…
C++ Function Examples • … help code readability
Return Types • Functions are different in C++ than in Python • Return Types must be defined • Definitions look like this • The return the type must always be defined. int function_name( float arg1, bool arg2 ) { return 1; }
Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions
Formatting Basics • Consistency • Very important for readability • Makes debugging much easier • Easier for others to review your code
Formatting Basics • Whitespace • Each indent should represent a new level of logic • Allows code to be read much easier
Formatting Basics • Good Variable Names • Name variables according to what they represent • Long variable names are good if they are helpful
Formatting Basics • Whitespace and Naming Example void f(int n) { int s=0; int e=1; For (int b=0;b<n;b++){ cout << e; e = s + e; s = e; }} void fib(int num) { int start=0; int end=1; for (int n=0; n<num; n++) { cout << end; end = start + end; start = end; } }
Formatting Basics • Comments • Help with debugging • Allow others to more easily understand the code
Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions
Examples • Good Code • Bad Code
Examples codequiz.com
Example #1 Write a function that takes one number and returns the square of the number Ex. Input: 2; Returns 4
Example #2 Write a function that takes three numbers and returns the average Ex. avg(double d1, double d2, double d3)
Example #3 Write a function to return the area of a square. The function will take the height and width of the square as parameters.
Example #4 Write a function that takes a number grade and prints the associated letter grade. Ex. 95 prints “A” 80 prints “B”
Conclusion • Be consistent • Formatting helps you and others understand your code • Start good habits now