CMIS 102 compiling, syntax, identifiers, variables, output We start learning some of the copious detail that all programming languages have. Entering (typing), editing, compiling, and running a program in Dev-C++: --make a New Source File, or open an existing .cpp file --type/edit --Compile F9 If any Errors, fix the first then Compile again... --Run F10 Enjoy the interaction. Loop: "figure out error, try to fix it". It's because programming is hard. It's not easy to create exactly correctly detailed instructions of a procedure. Every C++ program has a function called main. It's where the program starts. No main, no start! It can also have other functions but we won't be doing that in this course. Syntax is "grammar", the rules that specify the allowable forms that a program and it's parts may have. The compiler enforces the syntax rules 100%. You can't successfully compile a program that has syntax errors. Every program you write has to be syntactically perfect. There are lots of syntax rules and it seems difficult to learn them at first but syntax is the easy part of programming, just like it's the easy part of human language. It's doing something with the language that is hard: creating a program that does what it's supposed to correctly (or writing a novel, a research paper, a song, a recipe, a contract in human language). Identifiers are the names of variables that you create. The rules for a valid identifier are easy, and are insisted upon by the compiler so you can't disobey them. You should be vaguely familiar with the list of reserved words so that you understand why the compiler says it's an error to try use int, or while, or public etc. as an identifier. It's important to choose names that are meaningful and that indicate purpose, so that human readers of the program can understand it more easily. The computer is oblivious of any meaning of a name; it knows nothing about the world or human language, it has no common sense, it's a mindless machine; it has no idea what your intention or purpose is, it only knows what you've told it to do. The computer will carry out ("execute") your program. One of the things identifiers are used for is to give names to variables. The type of data is a very important concept in computing. We'll only be using the simple types (the others you learn in 141 and 242). "int" is the basic integer number type. Many of the reserved words in C are abbreviations (saves typing, or card punching in the really old days). int is short for integer. short, long and unsigned are variations on int of no great importance for now. An int is 4 bytes in size, so the range is roughly -2 billion to +2 billion (exactly 2,147,483,647). Range is important because values outside the range don't exist in the machine. Adding 1 to 2,147,483,647 does not equal what it should mathematically, but instead results in what is called a garbage value. A program that does this overflow is in error, but will continue on its merry way without caring. And then the reactor core melts down and everybody dies. Hey, just kidding, you'll probably just get an amazing tax refund. ints are for integers. Numbers with decimal point ("real" numbers in math) are called floating-point numbers in computing because of how they're stored in the machine. C++ has the "float" type (remember, saves typing). So cool numbers like 3.141592 and 2.718281828 are floats. And so is 5.0. 5 is the int five. There are these two kinds of numbers in C because the machine, specifically the CPU which does all the computation, has separate circuitry for integers and floating point values. Also because integers and real numbers are used for different purposes in the world, the former for counting things, the latter for measuring. Everyone knows computers are used for calculating, therefore they have numbers, therefore a programmaing language needs a way to store and manipulate numbers, hence int and float. But computers are also used for textual manipulation. Letters and punctuation characters need to be represented. C has the "char" type for this purpose. To specify a char value in a program, the character is surrounded by a pair of apostrophes (known as single quotes to the computing cognoscenti). So 'a' and '$' are chars. 'A' is a different char from 'a'. '5' is the character five digit, it is not the int 5 nor the float 5.0. Now we've got identifiers and data types. The time has come to join them together by declaring variables of specific types. A variable can be thought of as a little box with a name and a type. It's actually a location in memory. The box can contain ONE value of the type. We'll see shortly how to get a value into the variable, but when the variable is declared (created, or comes into existence) the value it has is whatever the value of those bits of memory are, which will be random garbage. A declaration is a statement. Syntax rule 849372*: All statements in C must end with a semicolon. Therefore, a declaration must end with a ;. * I'm making this number up. int myVar; This variable comes into existence at this point in the execution of the program. It has no meaningful value (i.e. don't use the value it has yet). A variable's value can vary, that's why it's called a variable, get it? A const variable, on the other hand confusingly, has a value that can't vary once it's been given a value at its declaration. They should be used for values that don't change in the program and for which a word is more meaningful than a "magic number". A program is more understandable if instead of a mysterious 0.12 being used several places in it, the word INTEREST_RATE is used. It's just a stylistic convention that the consts be in all caps. A convention that if you don't follow you will flunk the course. Kidding again. The authors style of initial words in caps for multiword variable names is a good one. If you have a variable you can assign a value to it. The = does not mean "equal". That was math, this is computer science. = is an action: it takes whatever is on the right side of it and makes that the value of the variable on its left side. myVar = 23; Whatever value myVar has before is gone, obliterated, lost, its new value after the execution of this statement is 23. C can do all the basic arithmetic of grade school: addition, subtraction, multiplication, division, and inverse hyperbolic trigonometric functions (you missed that day?). The mod operator is the unusual one. If you recall your grade school math when you first learned division: 7 divided by 3 is 2 with a remainder of 1. 7 % 3 is 1. It's actually more useful than division and you'll learn that deep truth as your CMIS career progresses. For now just learn how it works. x = x * 3; If x's value before the statement is 5, then the right side of the = is evaluated, it's 15 and that value is assigned to the variable x (sort of just coincidental that it's used on the right side). x's value after the statement is 15. ++ and -- are handy shorthand operators to increment and decrement a numeric variable. Use them as statements by themselves: x++; cout is the statement to do output. Each value, either constant or variable or expression is preceded by the << output operator. To get the effect of Enter or carriage return, output an endl. If x's value is 3 then cout << "hello " << 5 * x << endl; will output: hello 15 and the cursor will be at the beginning of the next line down. Note that the space after the o is part of the string "hello ". Without it the 15 would abut hello like so: hello15 C++ has two ways to indicate comments. /* blah blah blah */ is the old C way to do it. //blabityblabblab-to-the-end-of-line is the preferred way to do it in C++. The #include is a preprocessor directive. The preprocessor is the first step in compilation. This particular directive allows the program to do I/O using cin and cout. Every program you write will have that. consts are typically declared before main. Then in main, all variables are declared, followed by the executable statements. Statements are executed in the sequence in which they appear in the program (this will be modified when we learn selection and iteration).