Fundamental Programming: 310201 1 310201 Fundamental Programming Introduction to C++

44
Fundamental Programming: 310201 1 310201 Fundamental Programming Introduction to C++

Transcript of Fundamental Programming: 310201 1 310201 Fundamental Programming Introduction to C++

Fundamental Programming: 310201 1

310201Fundamental Programming

Introduction to C++

Fundamental Programming: 310201 2

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: 310201 3

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: 310201 4

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: 310201 5

Hello World Program in C++, the Hello World program is:

#include <iostream>

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: 310201 6

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 <iostream>

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: 310201 7

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: 310201 8

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: 310201 9

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) {

< code for main function > }

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: 310201 10

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: 310201 11

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: 310201 12

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: 310201 13

Sample Program Revisitedwrite “Number of marks in exam ==> “ read NbrMarks write “Student’s mark ==> “read StudentMarkset Percentage to 100 * StudentMark / NbrMarkswrite “ Student’s percentage: “write Percentage

in C++, this looks like…

Fundamental Programming: 310201 14

Sample Program In C++#include <iostream>using namespace std;void main (void){ int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout << "Number of marks in exam ==> "; cin >> NbrMarks; cout << "Student’s mark ==> "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage;

}

Fundamental Programming: 310201 15

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: 310201 16

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: 310201 17

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: 310201 18

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: 123.456 (meters)

a temperature: -2.3 (degrees C)

Fundamental Programming: 310201 19

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 - 0.000000000012345

these two numbers can be represented as: 1.2345 * 1018 and 1.2345 * 10-12

float variables are stored in two parts: number part : 1.2345 in above cases power part : 18 or –12 in above cases

decimal point “floats around”, depending on power part – more details later

Fundamental Programming: 310201 20

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: 310201 21

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: 310201 22

Basic Datatypes

• No data type void• Integer int• Floating point float• Double Precision double• Chareater char• Boolean bool

Fundamental Programming: 310201 23

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: 310201 24

Sample Program In C++#include <iostream>using namespace std;void main (void){ int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout << "Number of marks in exam ==> "; cin >> NbrMarks; cout << "Student’s mark ==> "; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << " Student’s percentage: "; cout << Percentage;

}

Fundamental Programming: 310201 25

Output Statements our program has 4 output statements:

cout << "Number of marks in exam ==> "; cout << "Student’s mark ==> "; 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: 310201 26

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: 310201 27

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: 310201 28

if-else Statements#include <iostream>using namespace std;void main (void){ float StudentMark = 0; cout << "Student’s mark ==> "; cin >> StudentMark; if (StudentMark >= 50) {

cout << "Pass!"; } else {

cout << "Fail!"; }

}

Fundamental Programming: 310201 29

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: 310201 30

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: 310201 31

Activity – Code in C++write “Select conversion - (1) C to F, (2) F to C ==> “read ConversionTypewrite “Input temperature ==> “read Temperaturewrite “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: 310201 32

A Start#include <iostream.h>void main (void){ int ConversionType = 0; float Temperature = 0; cout << "Select conversion - (1) C to F, (2) F to C ==> "; cin >> ConversionType;

}

Fundamental Programming: 310201 33

Activity Break

Fundamental Programming: 310201 34

A Solution#include <iostream.h>void main (void){ int ConversionType = 0; float Temperature = 0; cout << "Select conversion - (1) C to F, (2) F to C ==> "; cin >> ConversionType; cout << "Input temperature ==> "; 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: 310201 35

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: 310201 36

while Statements

#include <iostream.h>void main (void){ int NbrTimesTold = 0,

NbrTimesToTell = 0; cout << "How many times must I tell you? ==> "; cin >> NbrTimesToTell;

while (NbrTimesTold < NbrTimesToTell) {

cout << “ No new taxes!"; cout << endl; NbrTimesTold = NbrTimesTold + 1;

}}

Fundamental Programming: 310201 37

Activity Break

Fundamental Programming: 310201 38

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! No new taxes! No new taxes!

Fundamental Programming: 310201 39

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: 310201 40

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 <iostream.h> void Main (void) {

Cout << "Hello World!"; }

Fundamental Programming: 310201 41

C++ is Case-Sensitive

#Include <iostream.h> 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: 310201 42

C++ Syntax Summary input : cin >> <variable>; output : cout << <data>; assignment : <variable> = <data>;

a selection statement: if ( <test> ){ <if-block statements> }

else{ <else-block statements> }

a repetition statement: while ( <test> ){ <while-block statements> }

Fundamental Programming: 310201 43

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: 310201 44

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