Skip to content

Commit 773d786

Browse files
authored
Merge pull request #157 from UCL/mm_updates2025
Remove terminal from index
2 parents 598a8c5 + 7b9a53f commit 773d786

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

01projects/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ In this section we also provide notes on how to use the **command line terminal*
1313
- [Introduction to C++ and "Hello World" Program](./sec01IntroToCpp.md)
1414
- [Further C++ Syntax](./sec02CppSyntax.md)
1515
- [C++ Programs with Multiple Files](./sec03MultipleFiles.md)
16-
- [Using the terminal](./sec04Terminal.md)
1716
- [Version control with Git](./sec05Git.html)
1817

1918
## Useful References

01projects/sec02CppSyntax.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,39 @@ double y = 7;
7777

7878
Here the compiler will interpret the literal `7` as an `int` and then convert it to `double` to be assigned to the variable `y`.
7979

80+
## Comments
81+
82+
Comments are extremely useful for explanatory notes in your code. You can begin a comment with `//` for a comment on a single line.
83+
84+
```cpp
85+
// This is a comment.
86+
87+
double r = 5.0; // Comments can also be on the same line as some code.
88+
```
89+
90+
Longer comments begin with `/*` and can stretch over multiple lines, until we reach `*/`. This can be used for long comments or to temporarily comment out functionality.
91+
92+
```cpp
93+
/* This is a longer comment
94+
over multiple lines. The code in this comment will not execute.
95+
96+
int x = 5*5;
97+
int y = x + 17;
98+
99+
*/
100+
101+
int z = 2*12; // this code does execute.
102+
```
103+
104+
Be careful when commenting out code that you don't comment out definitions that you are relying on elsewhere!
105+
80106
## Defining and Calling Functions
81107

82108
We've already seen function signatures and the `main` function, let's look at how to define a simple function which will get used in our main.
83109

84110
```cpp
85111
#include <iostream>
86112

87-
//
88113
int square(int x)
89114
{
90115
return x*x;
@@ -121,7 +146,7 @@ This would of course be a waste of time with a function like this, but is occasi
121146

122147
A true/false value is called a Boolean value, or `bool`. Conditional statements test the value of a Boolean value or expression and execute the following code block if it is `true`. (Remember that a code block is contained within curly braces `{}`, and can be as large as you like.)
123148

124-
```cpp=
149+
```cpp
125150
// if statement with a Boolean variable
126151
if(condition)
127152
{
@@ -144,7 +169,7 @@ In the examples above, nothing will happen if the statement inside the brackets
144169

145170
If you want something to happen when the statement is false, you can also use `else` and/or `else if` statements.
146171

147-
```cpp=
172+
```cpp
148173
if(x < 10)
149174
{
150175
std::cout << "x is small" << std::endl;
@@ -161,7 +186,7 @@ else
161186

162187
## Loops (`for` and `while`)
163188

164-
```cpp=
189+
```cpp
165190
for(unsigned int i = 0; i < 100; ++i)
166191
{
167192
// loop code goes here
@@ -177,7 +202,7 @@ for(unsigned int i = 0; i < 100; ++i)
177202
- `++i` increments the value of `i` by 1.
178203

179204
If we have a `vector` or similar container, we can loop over its elements without writing our own loop conditions:
180-
```cpp=
205+
```cpp
181206
#include <vector>
182207

183208
int main()
@@ -198,7 +223,7 @@ int main()
198223

199224
`while` loops have simpler syntax than `for` loops; they depend only on a condition, and the code block executes over and over until the condition is met. This is useful for situations where the number of iterations is not clear from the outset, for example running an iterative method until some convergence criterion is met.
200225

201-
```cpp=
226+
```cpp
202227
while( (x_new - x_old) > 0.1) // convergence criterion
203228
{
204229
x_old = x_new;

01projects/sec03MultipleFiles.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: C++ Programs with Multiple Files
77
Like other programming languages, it is possible (and good practice!) to break up C++ programs into multiple files. C and C++ however have a slightly unusual approach to this compared to some other languages. Let's consider that we have two C++ files, our `main.cpp` which contains our `main` function (the entry point for execution of our program), and another which defines some function that we want to use in `main`.
88

99
**main.cpp:**
10-
```cpp=
10+
```cpp
1111
#include <iostream>
1212

1313
int main()
@@ -22,7 +22,7 @@ int main()
2222
```
2323

2424
**function.cpp:**
25-
```cpp=
25+
```cpp
2626
int f(int a, int b)
2727
{
2828
return (a+2) * (b-3);
@@ -50,7 +50,7 @@ Let's use this simple example program to explore how the compiler deals with our
5050
We'll start with a single file and work towards a multiple file version. Consider the following two versions of the same program:
5151
5252
**Version 1**
53-
```cpp=
53+
```cpp
5454
#include <iostream>
5555
5656
int f(int a, int b)
@@ -70,7 +70,7 @@ int main()
7070
```
7171

7272
**Version 2**
73-
```cpp=
73+
```cpp
7474
#include <iostream>
7575

7676
int main()
@@ -93,7 +93,7 @@ Only the first of these two programs will compile!
9393
- C++ does **not** need to know everything about `f` ahead of time though; it just need to know _what_ it is and what its type is. This is the job of **forward declaration**: something that tells us that there will be a function with this signature defined somewhere in the program is without telling us exactly what it does. We can also have declarations for things other than functions in C++, as we shall see later on in the course.
9494
9595
**With a function declaration:**
96-
```cpp=
96+
```cpp
9797
#include <iostream>
9898
9999
// Function declaration for f
@@ -129,7 +129,7 @@ This might seem like a rather pointless thing to do in a program as trivial as t
129129
Now that we know that we can write function declarations, we can move the function definition to a different file, and compile both files separately.
130130

131131
**main.cpp**:
132-
```cpp=
132+
```cpp
133133
#include <iostream>
134134

135135
int f(int a, int b);
@@ -145,7 +145,7 @@ int main()
145145
```
146146
147147
**function.cpp**:
148-
```cpp=
148+
```cpp
149149
int f(int a, int b)
150150
{
151151
return (a + 2) * (b - 3);
@@ -175,25 +175,25 @@ g++ -o test_f main.cpp function.cpp
175175

176176
## Header Files
177177

178-
Forward declarations for functions are helpful, but they can still clutter up our code if we are making use of large numbers of functions! Instead, we put these declarations in **header files**, which usually end in `.h` or `.hpp`. We use `#include` to add header files to a `.cpp` file: this allows the file to get the declaration from the header file. The definitions are not kept in the header file, they are in a separate `.cpp` file.
178+
Forward declarations for functions are helpful, but they can still clutter up our code if we are making use of large numbers of functions. We would also need to rewrite these forward declarations for _every_ source file that needs to use them! Instead, we put these declarations in **header files**, which usually end in `.h` or `.hpp`. We use `#include` to add header files to a `.cpp` file: this allows the file to get all the declaration from the header file. The definitions are not kept in the header file, they are in a separate `.cpp` file so that they can be compiled separately.
179179

180180
In this case the files look as follows:
181181

182182
**function.h**:
183-
```cpp=
183+
```cpp
184184
int f(int a, int b); // function declaration
185185
```
186186
187187
**function.cpp**:
188-
```cpp=
188+
```cpp
189189
int f(int a, int b)
190190
{
191191
return (a + 2) * (b - 3);
192192
}
193193
```
194194

195195
**main.cpp**:
196-
```cpp=
196+
```cpp
197197
#include <iostream>
198198
#include "function.h" // include our header file with the declaration
199199

0 commit comments

Comments
 (0)