c++ / g4mice course session 2 basic c++ types. control and looping functions in c function/method...

22
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Upload: audrey-reeves

Post on 20-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

C++ / G4MICE Course

Session 2

Basic C++ types.Control and Looping

Functions in CFunction/method signatures and scope

Page 2: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Types• Quick reminder from the previous session:

– int, bool, char, double, float, string, ...• Some types can also be long or short and

some can be unsigned:– unsigned long int x;

• Classes are new types that we can get from elsewhere (e.g. Standard Template Library or G4MICE) or which we can make ourselves.

• string is a class that the STL provides for us.

2

Page 3: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Control and Looping

• We will look at the following:– If– Else– While– For

• C/C++ also provides:– Switch– Case

• Which we will not cover in this course.3

Page 4: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Operators• Mathematical:

+ (addition)

- (subtraction)

* ( multiplication)

/ (division)

% (modulus)

• Logical:& (bitwise AND)

| ( bitwise OR)

! (NOT)

4

Page 5: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Logical Operators• == equal to, this is different from =, which

is the “assignment operator”!)

• != not equal to

• < less than

• > greater than

• <= less than or equal to

• >= greater than or equal to

• && logical AND

• || logical OR

5

Page 6: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Statement Blocks

• In all of the control and looping commands you may wish to have more than one command executed.

• To do this, we put the commands inside a pair of { }

• Any block of code can be collected in this manner (we will come back to this with scope later on).

6

Page 7: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

if Statement

• if( condition ) expression;

• if( condition ) { expression1; expression2; ... expressionN; }

• Note that the if() does not have a semicolon after it, that would be a NULL command:– If( condition );

• This would do nothing.

7

Page 8: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

else

• else can be added as many times as required:

• if( condition 1 ) expression 1;

• else if( condition 2) expression 2;

• ...

• else expression N;

• As before, { } can be used as required.

8

Page 9: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

while

• while will loop over the code so long as the condition is true:

• while( condition ) expression;

• while( condition ) { expression1; expression2; ... expressionN; }

9

Page 10: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

for

• A loop that allows you to set some initial values, specify the condition that must be true for looping to continue and specify code to be executed at the end of each loop:– for( x = 0; x < 10; x = x + 1 ) { ... }

• This will start with x = 0 and execute the code in the {} and then repeat with x = 1, 2, 3, ... up to 9.

10

Page 11: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

++ -- += -=

• Several operators in addition to those we’ve already seen:++x; increment x

--y; decrement y;

x += 5; add 5 to x;

z -= 10; subtract 10 from z;

• These can be useful in loops, e.g.:– for( x = 0; x < 10; ++x )

11

Page 12: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Control and Looping Example

• Download loop.cc, compile it and run it.

• Read through the code and let me know if anything is unclear.

12

Page 13: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Functions• Functions in C are declared by

specifying:– The return type (can be void if nothing is

returned)– The function name– Whether it is const (we will get to this later)– Arguments to the function

• Examples:– void dump();– double absoluteValue( double x );

13

Page 14: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Absolute Value

• The file absolute.cc shows an example of a function that returns the absolute value of the parameter that is passed to it.

• Compile and run this and ensure that you understand how it works.

14

Page 15: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Parameter Passing

• Parameters to functions are only ever passed “by value”.

• That is, a copy of the value of the variable is sent to the function.

• There is a way to pass by reference, we will cover that in the next session.

• For now, you can’t change the value of a variable that is passed to a function.

15

Page 16: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Signatures

• The complete set of information about a function that uniquely identifies it is known as its signature.

• The signature consists of the function name, return type, constness and arguments.

• You can have more than one function with the same name, so long as they have different signatures.

16

Page 17: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

signature.cc

• The file signature.cc shows an example of different functions with the same name, but different signatures.

• Compile and run this and ensure that you understand how it works.

17

Page 18: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

scope

• Functions, variables and types may not be visible from all code.

• We have seen an example of the std namespace, within which variables such as cout and types such as string are defined.

• This is why we refer to them with std:: preceding the variable or type name.

18

Page 19: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

variables inside { }• If a variable is declared inside a { } block,

it is only visible to the code inside that block.

• This means that you can temporarily declare a variable that will only exist while the code in that block is being executed.

• It also means that you can run the risk of having two different variables of the same name at the same time!

19

Page 20: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

scope example

• The file scope.cc shows a range of examples of variables being visible to different fractions of the code.

• Compile and run this and ensure that you understand how it works.

• Note that it will NOT compile at first, you will need to understand why...

20

Page 21: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Final Exercise

• If you have time, add a function to the now working scope example.

• Experiment to understand whether the variables defined inside main() are visible inside the new function and vice versa.

21

Page 22: C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

After Lunch

• Classes

• Pointers and References

• Makefiles

• Standard Template Library

• Unit testing

• Documentation

• Homework assignment for tomorrow!

22