Download presentation
Presentation is loading. Please wait.
Published byAudrey Reeves Modified over 9 years ago
1
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope
2
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
3
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
4
Operators Mathematical: + (addition) - (subtraction) * ( multiplication) / (division) % (modulus) Logical: & (bitwise AND) | ( bitwise OR) ! (NOT) 4
5
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
6
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
7
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
8
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
9
while while will loop over the code so long as the condition is true: while( condition ) expression; while( condition ) { expression1; expression2;... expressionN; } 9
10
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
11
++ -- += -= 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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
After Lunch Classes Pointers and References Makefiles Standard Template Library Unit testing Documentation Homework assignment for tomorrow! 22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.