Download presentation
Presentation is loading. Please wait.
1
Fundamental Programming
More Basics Fundamental Programming
2
What’s Next we started out with a minimal level of C++ syntax to get you started Next four sessions: Back to Basics – more basic C++ Testing – what is testing, and how do you do it? More on Selection – everything you always wanted to know about if…else… More on Repetition – different looping constructs Fundamental Programming
3
#include <iostream.h>
a couple of comments about this… # lines are not compiled, they simply direct the actions of the compiler – compiler directives , we’ll meet more later… all standards libraries are enclosed in angle brackets , libraries you develop are enclosed in double-quotes - later you’ll learn how to do this Fundamental Programming
4
void main (void) the second void above means that main has no input of output variables; actually, it is optional – you will sometimes see: void main() also, you will sometimes see int before main – a main function that returns an integer int main() {... return ErrorNumber; } this is used to return an error number to the operating system – 0 means no error Fundamental Programming
5
Data Objects Study Guide uses the term data object to refer collectively to variables and constants variable : holds a value that will vary constant : holds a value that is constant – cannot be changed in the program const int BOILING_POINT_OF_WATER = 100; const float ABSOLUTE_ZERO = ; declared like variables, with const before data type Fundamental Programming
6
Naming Rules start with a letter: A-to-Z, a-to-z
can contain upper or lower case letters, numerals (0-to-9), or underscore only first 32 characters are significant some compilers will allow you to use more compiler may not be able to distinguish between different names with same first 32 characters so, good idea to limit to 32 characters avoid reserved words like int, float, etc Fundamental Programming
7
Style Guide many organisations use a style guide
the goal is consistency in program code – to make it easier to maintain we use a simple Style Guide in this course: all names (functions, variable and constants) start with upper case letter variables use mixed case – capitalise first letter of each word: StudentMark constants are all upper case – use underscore to separate each word: ABSOLUTE_ZERO for details see: Study Guide (p. 7-19) Fundamental Programming
8
Local Data Objects local data objects: local to the function where they are declared; not visible outside the function objects declared in a function are local - are not visible outside function #include <iostream.h> void main (void) { const float ABSOLUTE_ZERO = ; int Temperature = 0; : } Fundamental Programming
9
Global Data Objects global data objects: declared outside functions - before main; visible everywhere #include <iostream.h> const float ABSOLUTE_ZERO = ; void main (void) { : } objects declared before main are global - visible everywhere global variables are not recommended in this course – but may be used for now! Fundamental Programming
10
Data Types the choice of data type affects how much memory is used to store a value, and what you can store… short int uses 2 bytes of memory (16 bits) and can hold a maximum value of 32,767 long int uses 4 bytes of memory and can hold a maximum value of 2,147,483,648 float uses 4 bytes and can hold a maximum value of 3.4 * 1038 double uses 8 bytes and holds a maximum value of 1.7 * 10308 Fundamental Programming
11
Data Types the choice of real type also affects the precision of numbers you can store – the number of significant digits float provides 7 significant digits – is stored as 1,234,567,890 is stored as 1,234,568,000 double provides 15 significant digits see: Study Guide (p. 7-12); textbook (p41) Fundamental Programming
12
Data Type Demotion when you assign a real value to an integer variable, we say that it is demoted – try: int IntVariable; IntVariable = 3.7; cout << IntVariable; the above code will output the value 3 the value 3.7 is demoted to 3 when it is assigned to an integer variable - the .7 is truncated (chopped off) Fundamental Programming
13
Data Type Promotion when you assign an integer value to a real variable, we say that it is promoted nothing to worry about here - no data is lost… int IntVariable = 3; float FloatVariable; FloatVariable = IntVariable; no information lost! Fundamental Programming
14
Expressions watch out for this one – try this:
int IntVariable1 = 3, IntVariable2 = 4; cout << IntVariable1 / IntVariable2; this code will output 0 – both operands are integers, so an integer result is given you can fix this by converting one of the operands to a real type: cout << float(IntVariable1) / InVariable2; or cout << 1.0 * IntVariable1 / InVariable2; Fundamental Programming
15
Expressions one more thing about expressions – try:
cout << 2 + (Variable = 3); you might expected this to cause a compilation error – it doesn’t, it outputs 5 in C++, an assignment is considered to evaluate to the value being assigned this is why the compiler does not object to: if (Variable = 3) : take care to avoid this type of error Fundamental Programming
16
Activity what will the following program output?
#include <iostream.h> void main(void) { int Variable; if (Variable = 2) cout << “True”; } else cout << “False”; Fundamental Programming
17
Activity Break Fundamental Programming
18
Activity Feedback the program will output: “True”
#include <iostream.h> void main(void) { int Variable; if (Variable = 2) cout << “True”; } else cout << “False”; as the expression does not evaluate to 0 (it evaluates to 2), it’s True Fundamental Programming
19
ASCII Coding System ASCII coding system is used to encode text:
) is stored as 41 1 is stored as 49 A is stored as 65 a is stored as 97 some values in ASCII coding system were reserved for controlling output devices 7 sound a beep - Bell 10 move to next line – Line Feed 13 move to start of current line – Carriage Return for details see: Study Guide (p. 7-13) Fundamental Programming
20
Special Characters the backslash character is used to send special characters to cout: \a - Bell \r – Carriage Return \n – Line Feed \t – Horizontal Tab example: cout << “\t\tInvalid Input!\a\n\n“; if you want to output the string “\t”: cout << “\\t“; for details see: Study Guide (p. 7-25) Fundamental Programming
21
More On cin one feature of cin is that it can read multiple input values off a single input line : Sum = 0; while (InputValue != -1) { Sum = Sum + InputValue; cin >> InputValue; } cout << “Sum = “ << Sum; when extracting a value from cin, characters are read from the keyboard until it reaches a white space character – space or new-line 1 2 3 4 -1 Sum = 10 Sum = 10 Fundamental Programming
22
More On cin sometimes you may want to throw away all characters to the end of the input line : char Char = ' '; while (Char != 'X') { cout << "Enter X to exit ==> "; cin >> Char; } Enter X to exit ==> abcdef Enter X to exit ==> Enter X to exit ==> Fundamental Programming
23
More On cin to ignore all characters to the end of the input line, do this: : char Char = ' '; while (Char != 'X') { cout << "Enter X to exit ==> "; cin >> Char; cin.ignore(80,’\n’); } ignore up to 80 characters, or to end-of-line Enter X to exit ==> abcdef Enter X to exit ==> Fundamental Programming
24
More On cin or this… : char Char = ' ‘, OtherChar; while (Char != 'X') { cout << "Enter X to exit ==> "; cin >> Char; OtherChar = ‘ ‘; while (OtherChar != '\n') cin.get(OtherChar); } get characters from keyboard until we reach end-of-line Enter X to exit ==> abcdef Enter X to exit ==> Fundamental Programming
25
More On cin as mentioned previously, we do not want to waste too much time on cin in this course, your programs are not required to handle input data of the wrong type – if an integer is required, you can assume the user will enter an integer for more details see: Study Guide (p ) Fundamental Programming
26
More On cout using cout, you can output more than one expression in a single statement cout << “Var1 = “ << Var1 << endl; often we want to format output, like: the iomanip library has some functions that are useful for formatting output… Centigrade Fahrenheit ========== ========== Fundamental Programming
27
set number of significant digits for all following output values
#include <iostream.h> #include <iomanip.h> void main(void) { cout << << endl; cout << setprecision(5); cout << setw(8); } set number of significant digits for all following output values set column width for next output value 1.2346 Fundamental Programming
28
Activity what output will this program produce?
#include <iostream.h> #include <iomanip.h> void main(void) { cout << "Centigrade Fahrenheit\n"; cout << "========== ==========\n"; cout << setw(10) << 100 << 212 << endl; cout << setprecision(5); cout << setw(10) << << << endl; cout << setprecision(3); cout << setw(10) << << << endl; cout << setw(10) << << << endl; } Fundamental Programming
29
Activity Break Fundamental Programming
30
#include <iostream.h>
#include <iomanip.h> void main(void) { cout << "Centigrade Fahrenheit\n"; cout << "========== ==========\n"; cout << setw(10) << 100 << 212 << endl; cout << setprecision(5); cout << setw(10) << << << endl; cout << setprecision(3); cout << setw(10) << << << endl; cout << setw(10) << << << endl; } Activity Feedback Centigrade Fahrenheit ========== ========== 100212 1.23e e+06 scientific notation used here because only 3 significant digits are output Fundamental Programming
31
Activity Feedback if we, set number of significant digits to 9
#include <iostream.h> #include <iomanip.h> void main(void) { cout << "Centigrade Fahrenheit\n"; cout << "========== ==========\n"; cout << setprecision(9); cout << setw(10) << 100 << setw(11) << 212 << endl; cout << setw(10) << << setw(11) << << endl; cout << setw(10) << << setw(11) << << endl; cout << setw(10) << << setw(11) << } Activity Feedback if we, set number of significant digits to 9 Centigrade Fahrenheit ========== ========== and set width of second column to 11, we get Fundamental Programming
32
More On cout setprecision only applies to floating point numbers – it has no effect on integer output cout << setprecision(2); cout << 1000; scientific notation is used when the number of digits preceding decimal place exceeds the number of significant digits to output cout << 100.0; 1000 1.e+02 Fundamental Programming
33
More On Assignment Pascal has only one assignment operator:
<variable> := <expression>; C++ has a number of assignment operators – the two statements below are equivalent: NbrMarks = NbrMarks + 1; NbrMarks += 1; as are the two below: Remainder = Remainder - CandidateFactor; Remainder -= CandidateFactor; for more see: Textbook (p. 169) Fundamental Programming
34
Comments two types of comments – single-line, multi-line
// This is a single-line comment, each line of // the comment must start with // /* This is a multi-line comment – the comment runs for any number of lines until we reach */ recommendation: use single-line comments –then, multi-line comments can be used to comment-out lines of code : cout << “Variable1 = “ << Variable1; // display value of other variables cout << “Variable2 = “ << Variable2; Fundamental Programming
35
Comments two types of comments – single-line, multi-line
// This is a single-line comment, each line of // the comment must start with // /* This is a multi-line comment – the comment runs for any number of lines until we reach */ recommendation: use single-line comments – then multi-line comments can be used to comment-out lines of code : /* cout << “Variable1 = “ << Variable1; // display value of other variables cout << “Variable2 = “ << Variable2; */ Fundamental Programming
36
Activity would this not work if we had: :
cout << “Variable1 = “ << Variable1; /* display value of other variables */ cout << “Variable2 = “ << Variable2; what would be the effect of adding /* here and adding */ here Fundamental Programming
37
Activity Break Fundamental Programming
38
Activity Feedback why would this not work if we had:
/* cout << “Variable1 = “ << Variable1; /* display value of other variables */ cout << “Variable2 = “ << Variable2; */ compiler error here Fundamental Programming
39
Multi-line Statements
compilers treat an end-of-line in C++ as white space – so: cout << “Variable1 = “ << Variable1; MyVeryLongVariableName = <my very long expression>; is same as: cout << “Variable1 = “ << Variable1; MyVeryLongVariableName = <my very long expression>; Fundamental Programming
40
That’s the end of “Back to Basics” ! Fundamental Programming
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.