Fundamental Programming: Fundamental Programming Introduction to C++
Fundamental Programming: Aims Of Course develop an understanding of what programs do and how they do it develop logical and procedural thinking develop familiarity with the C++ language develop skill in program code development provide an introduction to programming languages – some fundamental concepts develop analysis and design skills provide an introduction to structured design encourage good s/w engineering practices
Fundamental Programming: Status last week we started work on: developing an understanding of what programs do and how they do it developing logical and procedural thinking this week we start work on: developing familiarity with C++ language developing skill in software development (in lab) providing an introduction to some fundamental programming concepts
Fundamental Programming: Hello World it’s traditional to start with the “Hello World!” program that is, a very small program which simply outputs the message “Hello World!” a pseudocode design for this program would only have one line: write “Hello World!“
Fundamental Programming: Hello World Program in C++, the Hello World program is: #include using namespace std; void main (void) { cout << "Hello World!"; } lets’ take a look at this program we’ll leave some details to later there are 6 lines in this program…
Fundamental Programming: Hello World – Line 1 most programs rely on other software the first line of the program tells the compiler to “include” code, from the “iostream” library, before it is compiled #include if this code were not included, the compiler would report an error on Line 5: cout << "Hello World!"; it would say: “cout is undefined”
Fundamental Programming: Hello World – Line 1 All elements of standard C++ library are declared within namespace std. So in order to access its functionality we declare with this expression that we will be using these entities.
Fundamental Programming: Hello World – Line 3 a structured program is one that has been broken up into chunks - “functions” in C++ this programs only has one function – the “main” function – Line 3 declares the start of this function void main (void) every C++ program has a “main“ function we’ll talk about the “void” bits later…
Fundamental Programming: Hello World – Lines 3 & 5 Lines 4 and 6 go with Line 3 – to help the compiler find the end of a function, curly brackets (or “braces”) are used void main (void) { } it’s common practice to align the brace that marks the end of a block of code with the brace that marks the start of the block
Fundamental Programming: Hello World – Line 4 Lines 5 is the only “executable” line of code here – it is executed by the computer… { cout << "Hello World!"; } this line sends “Hello World!” to the display notice how this line is indented we use indentation to make code easier to read it’s not required by the C++ compiler
Fundamental Programming: Hello World – Line 4 cout << "Hello World!"; you can think of: cout as the display << as write “c” in cout as “character” - “character output” so, cout << "Hello World!"; says: write the string of characters Hello World! to the display
Fundamental Programming: Hello World – Line 4 cout << "Hello World!"; in C++, we use braces to mark the start and end of a block of code here we use double quotes to mark the start and end of a string of characters also notice: you must use double quotes around Hello World! the line ends with a semi-colon (you get a compilation error if it is missing) compilers are very fussy about punctuation
Fundamental Programming: Sample Program Revisited write “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “ read StudentMark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage in C++, this looks like…
Fundamental Programming: Sample Program In C++ #include using namespace std; void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; }
Fundamental Programming: Declaring Variables in C++, you must declare variables before you use them also, you must declare the type of value each variable will hold the first variable declared is NbrMarks – it can hold any integer value int NbrMarks = 0; an integer is any +ve or –ve whole number, or zero: …-3, -2, -1, 0, 1, 2, 3, …
Fundamental Programming: Naming Variables start with a letter - A to Z can contain upper or lower case letters, or numerals - 0 to 9 good idea to limit names to 32 characters must avoid C++ reserved words like int, float, void, etc more later…
Fundamental Programming: Initialising Variables our declaration of NbrMarks also gives it an initial value - 0 int NbrMarks = 0; if you do not give a variable an initial value, it will hold whatever data was left in memory by the previous program incorrect initialisation of variables is a very common logic error made by programmers please read the previous point again!
Fundamental Programming: More Declarations our program also declares two floating point variables – StudentMark and Percentage float StudentMark = 0, Percentage = 0; we use variables of type float to hold values that may not be an integer examples: an student’s mark: 15.5 (marks) a distance: (meters) a temperature: -2.3 (degrees C)
Fundamental Programming: Why “Floating Point” some programs need to store very big numbers – eg. 1,234,500,000,000,000,000; others need to store very small numbers these two numbers can be represented as: * and * float variables are stored in two parts: number part : in above cases power part : 18 or –12 in above cases decimal point “floats around”, depending on power part – more details later
Fundamental Programming: More On Declarations we could declare these variables like this: float StudentMark = 0; float Percentage = 0; instead, a comma was used to separate two variables of the same type float StudentMark = 0, Percentage = 0; breaking the declaration over two lines is optional in C++ - it’s “common practice” float StudentMark = 0, Percentage = 0; notice how a comma is used here to separate two use of punctuation -, and ; “floating point” because you can store big numbers with several digits on left of decimal point, or small numbers with several digits on right of decimal point – more later
Fundamental Programming: Data Types selecting the correct data type for a variable is an important issue if StudentMark were declared as an integer variable, the program would not work correctly if the user entered a mark of 22.5 we’ll look at data types in more detail later back to our program…
Fundamental Programming: Basic Datatypes No data typevoid Integerint Floating pointfloat Double Precisiondouble Chareaterchar Booleanbool
Fundamental Programming: Modified Type bool char unsigned char signed char int unsigned int signed int short int unsigned short int signed short int long int signed long int unsigned long int float double long double wchar_t
Fundamental Programming: Sample Program In C++ #include using namespace std; void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; cout "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage; }
Fundamental Programming: Output Statements our program has 4 output statements: cout "; cout << " Student’s percentage: "; cout << Percentage; in the last statement, the value held in variable Percentage is output to the display the following statements are very different: cout << Percentage; cout << “Percentage”;
Fundamental Programming: Input Statements our program has 2 input statements: cin >> NbrMarks; cin >> StudentMark; you can think of: cin as the keyboard >> as read “c” in cin as “character” - “character input” so, cin >> NbrMarks; says: read character input from the keyboard and assign the value entered to NbrMarks
Fundamental Programming: Assignment Statements our program has 1 assignment statement: Percentage = 100 * StudentMark / NbrMarks; note: values are also assigned to variables by input statements cin >> NbrMarks; what about if-then-else and while ? C++ includes an if-else statement - no “then” a simple example is…
Fundamental Programming: if-else Statements #include using namespace std; void main (void) { float StudentMark = 0; cout "; cin >> StudentMark; if (StudentMark >= 50) { cout << "Pass!"; } else { cout << "Fail!"; }
Fundamental Programming: if-else Statements notice that comparison between StudentMark and 50 is enclosed in parentheses if (StudentMark >= 50) also, braces mark start and end of the if- branch (shown below), and the else-branch { cout << "Pass!"; } indentation makes code easier to read – it’s not required by the compiler
Fundamental Programming: if-else-if Statements if-else-if statements can be coded as: if (StudentMark >= 75) { cout << “Distinction!"; } else if (StudentMark >= 50) { cout << “Pass!"; } else { cout << "Fail!"; }
Fundamental Programming: Activity – Code in C++ write “Select conversion - (1) C to F, (2) F to C ==> “ read ConversionType write “Input temperature ==> “ read Temperature write “Converts to ” if ConversionType = 1 then write 32 + (Temperature * 1.8) write “ degrees Fahrenheit” else write (Temperature – 32) / 1.8 write “ degrees Centigrade”
Fundamental Programming: A Start #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; }
Fundamental Programming: Activity Break
Fundamental Programming: A Solution #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + (Temperature * 1.8); cout << " degrees Fahrenheit"; } else { cout << (Temperature - 32) / 1.8; cout << " degrees Centigrade"; } note: C++ uses == instead of = for testing equality
Fundamental Programming: while Statements last, but not least, the while statement C++ includes a while statement a simple example follows… some activities for the following example: see if you can work out what it does what do you think the following line will do? cout << endl;
Fundamental Programming: while Statements #include void main (void) { int NbrTimesTold = 0, NbrTimesToTell = 0; cout "; cin >> NbrTimesToTell; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!"; cout << endl; NbrTimesTold = NbrTimesTold + 1; }
Fundamental Programming: Activity Break
Fundamental Programming: Activity Feedback the program displays “No new taxes!” the number of times requested by the user… cout << endl; moves to start of next line How many times must I tell you? ==> 3 No new taxes!
Fundamental Programming: while Statements things to notice: comparison enclosed in parentheses while (NbrTimesTold < NbrTimesToTell) { cout << endl; cout << " No new taxes!"; NbrTimesTold = NbrTimesTold + 1; } use of braces and indentation
Fundamental Programming: One More Detail About C++ all compilers are fussy about punctuation C++ compilers are also fussy about case the following code has 3 compilation errors… #Include void Main (void) { Cout << "Hello World!"; }
Fundamental Programming: C++ is Case-Sensitive #Include void Main (void) { Cout << "Hello World!"; } this program has three errors: it should be #include, not #Include it should be main, not Main it should be cout, not Cout C++ compilers are case-sensitive
Fundamental Programming: C++ Syntax Summary input : cin >> ; output : cout ; assignment : = ; a selection statement: if ( ) { } else { } a repetition statement: while ( ) { }
Fundamental Programming: Summary the iostream library must be included to do input and output – cin and cout every C++ program has a main function variables are declared before they are used the choice of data type is important variables must be initialised correctly Syntax Summary shows C++ implementation of Study Guide’s five pseudocode statements more…
Fundamental Programming: Summary C++ compilers are fussy about punctuation: angle-brackets around library names braces around blocks of code commas between variables in a declaration double quotes around strings semi-colons at end of lines parentheses around comparisons C++ compilers are also case sensitive indentation makes code easier to read