|
1
|
|
|
2
|
- int age;
- This is a declaration. Makes the name and the type of variable known to
the program.
- int age = 22;
- This is a definition. It is also a declaration, so it defines the
variable type and name, in addition to initializing it to a value.
|
|
3
|
- = is an operator that can be used > 1 time in an expression:
- Value of = is the value that is assigned
- Associates right to left:
|
|
4
|
- variable whose content cannot be changed during program execution
- Used for representing constant values with descriptive names:
- const double TAX_RATE = 0.0675;
- const int NUM_STATES = 50;
- Often named in uppercase letters
- Unmodifiable for the life of the program.
|
|
5
|
- What’s the difference here?
- ‘c’
- “c”
- Is there a difference?
|
|
6
|
- Char is 8 bits
- May contain one of the
values from the ascii character codes
- String depends of the number of characters
- Size is determined when we define the variable
|
|
7
|
- int x = 22;
- // what's happening here
- if(3 < x < 7) {
- cout << (3 < x < 7) << endl;
- cout << "Executing if statements\n";
- }
- cout << "outside of if statements\n";
|
|
8
|
- if( temp > 68 && temp < 72 || temp > 100) {
- cout << “Temperature “
<< temp;
- }
- && has a higher precedence than the || operator
- equivalent to:
- if((temp > 68 && temp < 72) || temp > 100)
|
|
9
|
- ! – the not operator
- Returns the inverse of the condition or expression
- int temp = 72;
- if(!temp)
- bool registered = false;
- if(!registered)
|
|
10
|
- int iVal = 0;
- iVal = 3.541 + 3;
- Your compiler should complain by giving you a warning
- Loss of data due to the fact that an int uses 2 bytes and doubles use 8
bytes.
|
|
11
|
- int c = 5;
- cout << c << endl;
- cout << c++ << endl;
- cout << c << endl;
- c = 5;
- cout << c << endl;
- cout << ++c << endl;
- cout << c << endl;
|
|
12
|
- for(int p = 0; p < 10; p++) {
- if(p < 5) {
- continue;
- }
- if(p > 5)
- break;
- cout << "This is p " << p << endl;
- }
|
|
13
|
- Initialization statement – initializes a counter variable (counter-controlled
loop). Executed once when the loop begins
- Continuation condition – determines if the body of the loop will be
executed. As long as the condition remains true the body of the loop is
executed
- The counter is modified. The counter can be incremented or decrement
(this doesn’t have to be by 1)
|
|
14
|
- for(;;) {
- cout << “catch me if you can!!!\n”;
- }
- Initialization can be omitted if initialization occurs else where
- The condition is omitted, it’s the equivalent of typing true. If this is
the case then the loop must have a break or a return statement to
terminate the loop
- The expression for the counter can be omitted if the body handles
changing the value of the counter or the loop must be terminated with a
break or return statement
|
|
15
|
- for(int j = 0; j < 5; j++) {
- cout << "outer: j is " << j << endl;
- for(int i = 4; i > 0; i--) { cout << “\tinner: i is "
<< i << endl;
- }
- }
|
|
16
|
- Inner loop goes through all repetitions for each repetition of outer
loop
- Inner loop repetitions complete sooner than outer loop
- Total number of repetitions for inner loop is product of number of
repetitions of the two loops. In
previous example, inner loop repeats 20 times
|
|
17
|
- Step through the code on a piece of paper
- Type it into your editor and step through the code in the debugger
|
|
18
|
- #include directive
- Inserts the contents of the file specified < >
- Runs before your code gets to the compiler
- Searches in the directory of the file then the include path
- More after we cover header files
|
|
19
|
|