Copyright © 2007-2017 – Curt Hill The Second C++ Program Variables, Types, I/O Animation! Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Second Program Next we will consider a more interesting program on the next screen The comments have been left out for space Powerpoint has also limited line length Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill The second program #include <iostream.h> int main (void){ float num,square; cout << "Welcome to our C++ demo “ << " program!\nEnter a number:"; cin >> num; cout << "\nYou entered " << num << "\n"; square = num*num; cout << "The square of " <<num << “ is " << square << "\n"; return(0); } Copyright © 2007-2017 – Curt Hill
The output of the program Welcome to our C++ demo program! Enter a number: 5 You entered 5.000000 The square of 5.000000 is 25.000000 Copyright © 2007-2017 – Curt Hill
This program shows the following features: Variables Input Calculations Each of these is a fairly substantial topic This may take a while Copyright © 2007-2017 – Curt Hill
PreProcessor commands We will later learn other preprocessor commands Always start with a # # is usually in col 1, but must be first non-blank Older compilers required it in 1, but not so anymore Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill The preprocessor: Scans the source file before the main part of compiler Before variable processing or statement processing It modifies the text doing substitutions One thing to know and remember: The preprcessor does not "know" C++ Its command syntax is simple, but not C-esque Originally a separate program written independently of C Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill The include Includes are used to tell the compiler what predefined functions (subroutines) are to be used In this case the objects cin and cout In iostream there are many routines(functions), objects, etc. if we want to use any of them we must have the include Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Include again One include allows us to use any or all of items in it The CPP extension says that this is a C source file The H extension says that this is a C header file A header file describes the variables and functions present without giving all the code for them The <> around iostream.h says to look for it in the standard place Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Variable declaration The form is: type var1, var2, ... varn; The type is float A float is a floating point number Similar to scientific notation with exponent and mantissa Declaring two variables num and square Both of which are of type float Copyright © 2007-2017 – Curt Hill
Other common types are: int which is an integer or whole number char which is any character including letters digits punctuation control characters blanks There are other types as well Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Declarations Variable declarations and code are mixed together Must declare the variable before we use it In ordinary C the declarations had to come at the beginning of the function In C++ they may be anywhere (even within statements) Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Declarations again There is nothing that says: Variable declaration starts or stops or executable commands start or stop other than the braces The cin and cout are actually declared in the include file, not here Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill cout is not new This is both the announcement of the beginning of the program and a prompt A prompt is the request for input A good prompt should tell us what we want and maybe why Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill cin The next statement is a cin (pronounced C In) which is an input statement Both cout and cin are objects and the use of << and >> constitute function calls They both receive parameters or arguments which guide the subroutine as to what to do Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Legal input Since num is a float the input can allow a large variety of formats: Allow leading white space (blanks, tabs, CR or LF) and ignore it Allow a leading sign (+ or -) and remember Look for a string of digits (0-9) This may be followed by an optional decimal point and digits This may be followed by an optional exponent and signed number Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Example inputs Since the form can be quite flexible, all of the following are legal: (Notice that some dashes belong to Powerpoint) 4 -5 +3 3.2 -694.6897 +3.4E-7 etc Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill cin and cout One large difference between cin and cout cout never modifies the things it uses cin always should Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Another cout The next cout shows that it can output variables as well cout << "\nYou entered " << num << "\n"; The first is a quoted string When cout encounters something like “string” it merely displays it as-is When it runs into a variable it takes the contents of that variable and then formats it for output Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Escape sequences Some languages like Pascal and VB have different commands for writing a complete line Not the C family Instead an escape sequence is used An escape sequence is a backslash (\) followed by a single character These are characters that cannot be typed in easily Copyright © 2007-2017 – Curt Hill
Common escape sequences \n is new line A new line in MS is Carriage return and line feed A new line in UNIX/Linux/Solaris is line feed \t is the tab Many others as well Copyright © 2007-2017 – Curt Hill
An assignment statement The statement: square=num*num; An assignment is where a variable is changed to the value obtained by some expression In this case the variable changed is square The expression is num*num which means Take the value of num and multiply it by the value of num Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Assignment again The assignment is our main way of changing values of variables Unfortunately there is plenty more to know about the assignment Most will wait for subsequent presentations Copyright © 2007-2017 – Curt Hill
Outline of C++ programs For now every C++ program will have the following form: Preprocessor directives Main function header { Beginning of the body of the main function Declarations Attaching names to types Executable statements These are delimited by ; The declarations and executables may be intermixed, though it is usually good practice to cluster the declarations } Closing brace of body Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Reserved words Certain words are called reserved words or key words: void float int double char We will see others as well Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Reserved words again These can only be used in certain contexts Variables cannot use them as variable names The standard identifiers are not key words cin and cout for example An identifier is any name made up by the users to indicate an item Variable, constant, class, method etc Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Identifiers Every identifier in a C program has several properties Three of which we discuss here Name Type Value Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Name Starts with a letter etc. Letters (both upper and lower), digits and underscores Programmer assigns the name, but it cannot be a reserved word Name is case sensitive No blanks or special characters The name is given in declaration before any other use The name is unchanged throughout the program Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Name Hints Choose a good name Meaningful Not very similar to any other name Having variables i and I is very poor practice – too easy to confuse with 1 Variables that are the same but a single character (especially an _) are also poor practice No length restriction but usually must be unique within first 31 characters Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Type Kind of thing it is Defines the allowable values Defines the legal operations Defines the size of item The type like the name is unchanged throughout the program There is another presentation on Types Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Value A variable can take one of many values However, it can only have one at any one time That value may change through the course of the program Values are changed by using cin or assignment Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Statements Statements may be executable or non-executable A function header or a variable declaration is not executable An assignment or use of cout is executable Usually the non-executables modify how the executables act in some way Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Expressions C++ (like C) is an expression language What is an expression? It is an item that returns a value for further use Expressions may contain: Constants Variables Operators Functions Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Constants Numeric -12 (integer) 2.4 (float) -2.3e-5 (float) Non-numeric 's' (char) "Hi there" (string) The values that these produce are mostly obvious They also have a type, which may need to be converted to another type to be used Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Variables Suppose that in the declarations I have: i,j; x,y; Then the presence of any of these is also an expression: x (takes the current value of x (a double) and returns it) i (takes the current value of i (an int) and returns it) Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Operators An operator is a symbol that indicates a machine action to be taken The operator can only be interpreted properly with an understanding of the involved types The meaning of the operator is dependent upon the types involve as we will see later Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Examples: i + 5 Take the value of i and add 5 to it The two expressions are of the same type, int It produces an int type This is a binary operator, which means it has two operands The variable i is not changed Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill More examples: 5.0*x/y Multiply the values of 5 and x, then divide the result by y All values are double (long float) as is the returned value Both multiply and divide are binary operators Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Unary example -j Take the value of j and reverse the sign This is a unary operator Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Functions Only functions we have seen so far are: main We will see many more Copyright © 2007-2017 – Curt Hill
C is an expression language Its executable statements are merely expressions terminated by semicolons Although we can say x*y+3; is a complete statement we do not because it causes work to be done but no change occurs Copyright © 2007-2017 – Curt Hill
Executable statements Most of our statements are function calls or assignments Usage of cout and cin are function calls We will consider more of these later Copyright © 2007-2017 – Curt Hill
Copyright © 2007-2017 – Curt Hill Conclusion This is a lot of explanation for a simple program Yet we now need to study in more depth some of the concepts introduced Types The assignment statement Many others Copyright © 2007-2017 – Curt Hill