Post on 18-Dec-2015
1
C++ for beginnersLecture 1
© 2008 Richèl Bilderbeek
2
Question
• What are variables?
• Where do you use variables for?
• What is the C++ syntax for using variables?
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
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)
5
Question
• An int stores whole numbers. Which other data types do you guess to exist?
• What is the purpose of using data types?
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
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;
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;
}
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!
10
Math
#include <iostream>
int main()
{
int a = 1;
int b = 2;
int som = a + b;
std::cout << som << std::endl;
}
11
Mathematical operators
+ plus
- minus
* multiplication
/ division
% modulus ('rest after division',
e.g. 7 % 4 equals 3)
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?
}
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
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;
}
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.
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)
17
Question
• If you want to do something depending a certain condition, what would you need to specify?
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
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;
}
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 */ }
21
Question
• How do you write a program that calculates the formula below?
X = 1 + 2 + 3 + 4 + …. + 98 + 99 + 100
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!
23
For-loop
for (
int i = 1; //Initialisation
i!=100; //Perform is this is true
++i) //Step
{
//Something
}
24
For-loop
#include <iostream>
int main()
{
int x = 0;
for (int i = 1; i!=100; ++i)
{
x+=i;
}
std::cout << x << std::endl;
}
25
Special for-loops
int main()
{
//Empy for-loop
for (int i = 0; i!=10; ++i) ;
//Infinite loop
for ( ; ;) { }
//Infinite empty loop
for ( ; ;) ;
}
26
Question
• How should do the layout if his/her code?– Indentation?– Tabs?
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
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
}