1 c++ for beginners lecture 1 © 2008 richèl bilderbeek

28
1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

Upload: godfrey-atkinson

Post on 18-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

1

C++ for beginnersLecture 1

© 2008 Richèl Bilderbeek

Page 2: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

2

Question

• What are variables?

• Where do you use variables for?

• What is the C++ syntax for using variables?

Page 3: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

3

Answer

• A variable is a value (number, text, etc.) a computer has to remember

• The purpose of variables is perform operations on them, for example mathematical operations

Page 4: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

4

C++ syntax for variables

int value = 12;

int = data type

int stores whole numbers

C++ is type sensitive

value = identifier

C++ is case sensitive

12 = initial value of 'value'

should be a whole number (as it is stored into an int)

Page 5: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

5

Question

• An int stores whole numbers. Which other data types do you guess to exist?

• What is the purpose of using data types?

Page 6: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

6

Answer

• Everything can be a data type, e.g. you can define a data type called 'Chicken'

• There are some standard C++ data types

• (one of) the purpose(s) of data types is that the compiler can prevent illegal conversions, e.g. from Chicken to Database

Page 7: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

7

Data types

int wholeNumber = 12;

double brokenNumber = 12.34;

bool truth = true;

std::string woord = “Bilderbikkel”;

enum Sex { man, vrouw };

Sex richel = man;

class Chicken { /* IETS */ };

Chicken henny;

Page 8: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

8

Question

• What is the problem in the following program?

#include <iostream>

int main()

{

int x = 12;

std::cout << x << std::endl;

int x = 24;

std::cout << x << std::endl;

}

Page 9: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

9

#include <iostream>

int main()

{

int x = 12;

std::cout << x << std::endl;

int x = 24;

std::cout << x << std::endl;

}

Answer

• The variable 'x' is declared twice!

int must be removed!

Page 10: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

10

Math

#include <iostream>

int main()

{

int a = 1;

int b = 2;

int som = a + b;

std::cout << som << std::endl;

}

Page 11: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

11

Mathematical operators

+ plus

- minus

* multiplication

/ division

% modulus ('rest after division',

e.g. 7 % 4 equals 3)

Page 12: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

12

Question

• How do you increase the value of a certain variable?

int main()

{

int x = 12;

int y = 23;

//How to increase the value of x by the value of y?

}

Page 13: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

13

Adding

i = i + 2; // i is its old value plus 2

i += 2; // i is increased by 2

i = i + 1; // i is its old value plus 1

i += 1; // i is increased by 1

i++; // i is incremented

++i; // i is incremented

Page 14: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

14

Question

• What happens in the following program?

#include <iostream>

int main()

{

int numerator = 22;

int denominator = 7;

//Approximation of pi

double pi = numerator / denominator;

std::cout << pi << std::endl;

}

Page 15: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

15

Answer

• The int '22' divided by the int '7' results in the int '3', which gets converted to the double '3.0'

• The get the value '3.14…', convert the ints to doubles.

• This is called casting.

Page 16: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

16

Casting

int teller = 22; int noemer = 7;

double pi = teller / noemer; //WRONG

double pi = static_cast<double>(numerator)

/ static_cast<double>(denominator); //GOOD

double pi = numerator

/ static_cast<double>(denominator); //WRONG

double pi = static_cast<double>(numerator)

/ denominator; //GOOD (if you know why)

Page 17: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

17

Question

• If you want to do something depending a certain condition, what would you need to specify?

Page 18: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

18

Answer

• The keyword if

• The condition

• The line(s) of code that must be executed when the condition is true

• Optionally: the line(s) of code that must be executed when the condition is false

Page 19: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

19

If statement

int x = /* something */;

if (x % 2 == 0)

{

std::cout << "x is even" << std::endl;

}

else

{

std::cout << "x is odd" << std::endl;

}

Page 20: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

20

Logische operatoren

if (truth == true) { /* if equal */ }

if (x < 96) { /* if smaller */ }

== is equal to

!= is not equal to

< is smaller

> is bigger

<= is smaller of equal

>= is bigger or equal

&& and

|| or

if (x == 1 && y == 2) { /* if both are true */ }

Page 21: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

21

Question

• How do you write a program that calculates the formula below?

X = 1 + 2 + 3 + 4 + …. + 98 + 99 + 100

Page 22: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

22

Answer

• Let the int x be zero

• Let the int i be one

• As long as i is not equal to 101– Add the value of i to x– Increment the value of i

• We must use a loop!

Page 23: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

23

For-loop

for (

int i = 1; //Initialisation

i!=100; //Perform is this is true

++i) //Step

{

//Something

}

Page 24: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

24

For-loop

#include <iostream>

int main()

{

int x = 0;

for (int i = 1; i!=100; ++i)

{

x+=i;

}

std::cout << x << std::endl;

}

Page 25: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

25

Special for-loops

int main()

{

//Empy for-loop

for (int i = 0; i!=10; ++i) ;

//Infinite loop

for ( ; ;) { }

//Infinite empty loop

for ( ; ;) ;

}

Page 26: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

26

Question

• How should do the layout if his/her code?– Indentation?– Tabs?

Page 27: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

27

Answer

• Depends on your company's standard!

• Indentation should be used in a consistent way

• Some teams ban tabs, some teams allow tabs under disciplined use

Page 28: 1 C++ for beginners Lecture 1 © 2008 Richèl Bilderbeek

28

Three standard layouts

for (int i = 0; i!=10; ++i) {

//Something

}

for (int i = 0; i!=10; ++i)

{

//Something

}

for (int i = 0; i!=10; ++i)

{

//Something

}