1.65k likes | 1.67k Views
Dive into the basics of C++ programming with this comprehensive review of your first class. Learn about commenting, output/input operations, syntax rules, variable declaration, data types, and more.
E N D
C++ Boot Camp A review of your first C++ class
The Skeleton #include <iostream.h> void main ( ) { // Go ahead and memorize this. // It has to be there! } // Note: this should go in a .cpp file
Documenting Your Programs • This is called “commenting”, and the computer ignores this when compiling. • There’s 2 ways to do it in C++! //This is for a single line comment. /* This is for a paragraph of comments and can go on and on. It doesn’t matter until you see the */
Printing to the Screen • Use cout to do this (pronounced “see-out”) • cout can print out just about anything • Variables of most data types • Multiple things at once • Special characters like ‘\n’ (newline), etc. • Print multiple things with additional <<‘s • Print out endl to drop to next line
Cout • Some examples: cout << “This is a sample string”; cout << my_int << endl; cout << “C++ scares me”; cout << “Hello” << “ world!” << endl;
Getting Input from the User • Use cin (pronounced “see-in”) • Read into variables (later) • Example: // Declare a variable int myInt; // Load it with information from the keyboard cin >> myInt; // Print it out cout << “myInt is “ << myInt << endl;
Cin • More examples: cin >> my_int; cin >> age >> name;
Chaining stream Operations • There is no limit to the number of things that you can print & read • Simply chain them together like this: cout << “First item” << “second item” << endl;
Special Escape Sequences • Added within strings • Performs “special character” \n newline \r carriage return (not common) \t tab \a alert (beep) \\ backslash \’ single quote \” double quote
Your First C++ Program #include <iostream.h> void main ( ) { cout << “Hello World” << endl; } // Note: yes, this should be put into a .cpp file
Rules about Syntax • Where do the ;’s go? • Semicolons come after single statements • Similar to a period at the end of a sentence • Where do the { and } go? • Creates a block of text for multiple single statements • { tells the computer to begin a section • } tells the computer to end a section • Can be nested (or inside of each other) • Let’s go back to our first c++ program
Your First C++ Program #include <iostream.h> void main ( ) { cout << “Hello World” << endl; } Begin the main End the main End a single statement
Compiling Your Program • You’ll use one of two things: • The command-line: g++ (on Unix) • Your IDE (like Visual C++) • When you compile, you’ll have one of two things happen: • It compiles • It doesn’t • If it doesn’t compile, you have syntax errors
Knowing When to Compile • As a rule, the less comfortable you are, the more you should compile • This isolates the errors • As a rule, compile: • After you type in the skeleton • After every 5 or 10 lines of new code • Don’t become dependent on the compiler!
Overview of Variables • Computer programs manipulate data • There is a need to store data • Variables: • Represent a named cell in the computer’s memory • Used to hold information • When you declare a variable: • You open up some of the computer’s memory • You’re giving the variable an identifier (name) • You only do this once per variable
Data Types • A data type describes the kind of information a variable can hold • Some data types are built into the language • Some data types we define ourselves (later) • Different Forms: • Primitive data types • Complex data types
Primitive Data Types • These are the simplest kinds of data types • Built-in • Whole numbers: • short (relatively small numbers) • int (larger numbers) • long (VERY large numbers) • Number of bytes these take up depends on compiler and OS!
Primitive Data Types • Decimal numbers: • float (less precise than a double) • double (more precise than a float) • Precision means how many numbers come after the decimal point • Others: • char (holds characters like ‘a’, ‘A’, ‘1’, ‘ ‘) • bool (holds only a true or false value)
How to Declare a Variable(of any type) • Format: <data type> <variable name>; • Variable name you can choose almost anything • Examples: byte age; float gpa; String name; char letterGrade; Computer's Memory age gpa ? ? name letterGrade ? ?
How Big is it: sizeof( ) • sizeof( ) will tell you how much memory something takes up in bytes • Usage: int myInt; cout << sizeof (myInt) << endl;
Reserved Words in C++ • These words are used by C++ for special reasons, so you can’t use them • List: • auto, bool, break, case, catch, char, class, const, continue, default, do, double, else, enum, extern, float, for, friend, goto, if, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, void, volatile, while
Legal Variable Names • Can’t be a reserved word • Can’t begin with a number • Can’t contain special symbols • Like #, @, !, ^, &, /, ), or SPACES • Exceptions: underscore _, and dollar sign $ • Examples: byte $theValue; // legal char test_value; // legal double double; // not legal int rum&coke; // not legal boolean true or false; // not legal for two reasons!
Literal Values • Values that are constant • Examples: • String literal – “Hello, World!” • Whole number literal – 17 • Decimal literal – 3.14159 • Character literal – ‘V’
Initializing Variables • Initializing a variable means giving a variable some kind of starting value • Assignment operator is = • Copies the information on the right into the variable on the left and must be compatible • Can be a literal OR another variable! • Examples: int age; age = 15; char letterGrade = ‘B’; char yourGrade = letterGrade;
Why Size Matters • Each data type takes up a different amount of the computers memory (in bytes) • Can’t put a larger data typed variable into a smaller data typed variable: short s = 5; long l = 5; long l = s; short s = l; long long long short short short
Operators • + adds two variables/literals together (including Strings) • - subtraction • / division • * multiplication • % modulus (remainder) • ( and ) follows standard math rules • Example: int result = ( (4 % 3) * 5) + 1; // result gets 6
Type Casting • This occurs when you want to put something larger into something smaller • Can possibly lose precision or value • Has format: <smaller var> = (<smaller data type>)<larger var>; • Example: long myLong = 17; short myShort = (short)myLong; // We are squeezing myLong into myShort!
A Flow Diagram Start Blame it on someone else Lay low and keep hidden Did you break anything? Yes Yes No Have your parents asked about it? Yes Are you lying? No Are you passing your classes? No No Ask for $$ Yes End
The Boolean • A bool is a something that resolves to true (1) or false (0) • You can get booleans several different ways • Simple • Complex • These booleans will be used (later) to make decisions to change your program’s behavior
Relational Operators(for primitive data types) • Relational Operators > greater than < less than == equals ! Not != not equal >= greater than or equal <= less than or equal • Notice that all of these return us a true or false value!
Relational Operator Examples • Literal Example 5 > 3 // true 6 != 6 // false (4 <= 2) // false ‘c’ != ‘b’ // true !0 // true (prints out 1) • Variable Example int num1 = 5; short num2 = 7; bool result = num2 > num1; //Note: result is now true
&& and || Operators • These operators check for multiple conditions • && (AND) needs both the left and the right to be true in order to return true • || (OR) needs either one (or both) to be true to return true • Can shortcut if necessary • Examples: ( (6 > 5) && ( ‘c’ == ‘b’) ) // false ( (6 > 5) && ( 7 < 9) ) // true ( (6 > 5) || ( ‘c’ == ‘b’) ) // true ( (6 > 6) || ( ‘c’ == ‘b’) ) // false
Now Let’s Do Something! • Using the if statement, we can decide whether or not to execute some code! • Has format: if (<boolean value>) { // all the code that’s in here will only execute // if and only if the boolean above is true }
“if” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if cout << “B”; } // end main
“if” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if cout << “B”; } // end main Output A B
“if” Example(Part II) #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if cout << “B”; } // end main
“if” Example(Part II) #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if cout << “B”; } // end main Output B
The “else” statement • if has a counterpart – the else statement • If the if clause didn’t execute, the else clause will! • Has format: if (<boolean value>) { // statements that execute if the boolean is true } else { // statements that execute if the boolean is false } • Notice only one set of statements executes, no matter what!
“else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main
“else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start > end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main Output B
“else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main
“else” Example #include <iostream> using namespace std; void main ( ) { int start = 5; int end = 19; if (start < end ) { cout << “A”; } // end if else { cout << “B”; } // end else } // end main Output A
else if’s • Selecting one from many • As soon as one is true, the rest are not considered • Has format: if (<boolean value>) { // statements that execute if the above boolean is true } elseif (<boolean value>){ // statements that execute if the above boolean is true } elseif (<boolean value>){ // statements that execute if the above boolean is true } else { // something that executes if nothing matched above }
Combining into “else if”s(Selecting one from many) #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start < middle ) { cout << “A”; } // end if else if (start < end) { cout << “B”; } // end else if } // end main
Combining into “else if”s(Selecting one from many) #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start < middle ) { cout << “A”; } // end if else if (start < end) { cout << “B”; } // end else if } // end main Output A
else catch-all Example #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start > middle ) { cout << “A”; } // end if else if (start > end) { cout << “B”; } // end else if else { cout << “C”; } // end else } // end main
else catch-all Example #include <iostream> using namespace std; void main ( ) { int start = 5; int middle = 8; int end = 19; if (start > middle ) { cout << “A”; } // end if else if (start > end) { cout << “B”; } // end else if else { cout << “C”; } // end else } // end main Output C
Switch statements • Simplify the else-if statements for primitive data types • Trying to find a match • Once a match is found, it will keep going until it sees the break keyword (telling it to stop) • Has format: switch (<primitive variable>) { case <value>: case <value>: }
Switch Example #include <iostream> using namespace std; void main ( ) { int myInt = 1; switch (myInt) { case 1: cout << “A”; case 2: cout << “B”; case 3: cout << “C”; } //end switch } //end main // Prints out A, B, and C. Why?