C++ for beginners lecture 1
Download
Report
Transcript C++ for beginners lecture 1
C++ for beginners
Lecture 1
© 2008 Richèl Bilderbeek
1
Question
• What are variables?
• Where do you use variables for?
• What is the C++ syntax for using variables?
2
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
3
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)
4
Question
• An int stores whole numbers. Which other
data types do you guess to exist?
• What is the purpose of using data types?
5
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
6
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;
7
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;
}
8
Answer
• The variable 'x' is declared twice!
#include <iostream>
int main()
int must be removed!
{
int x = 12;
std::cout << x << std::endl;
int x = 24;
std::cout << x << std::endl;
}
9
Math
#include <iostream>
int main()
{
int a = 1;
int b = 2;
int som = a + b;
std::cout << som << std::endl;
}
10
Mathematical operators
+
*
/
%
plus
minus
multiplication
division
modulus ('rest after division',
e.g. 7 % 4 equals 3)
11
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?
}
12
Adding
i = i + 2; // i is its old value plus 2
i += 2;
// i is increased by 2
i = i + 1; // i is
i += 1;
// i is
i++;
// i is
++i;
// i is
its old value plus 1
increased by 1
incremented
incremented
13
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;
}
14
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.
15
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)
16
Question
• If you want to do something depending a
certain condition, what would you need to
specify?
17
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
18
If statement
int x = /* something */;
if (x % 2 == 0)
{
std::cout << "x is even" << std::endl;
}
else
{
std::cout << "x is odd" << std::endl;
}
19
Logische operatoren
if (truth == true) { /* if equal */ }
if (x < 96) { /* if smaller */ }
==
!=
<
>
<=
>=
is
is
is
is
is
is
&&
||
and
or
equal to
not equal to
smaller
bigger
smaller of equal
bigger or equal
if (x == 1 && y == 2) { /* if both are true */ }
20
Question
• How do you write a program that calculates
the formula below?
X = 1 + 2 + 3 + 4 + …. + 98 + 99 + 100
21
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!
22
For-loop
for (
int i = 1; //Initialisation
i!=100;
//Perform is this is true
++i)
//Step
{
//Something
}
23
For-loop
#include <iostream>
int main()
{
int x = 0;
for (int i = 1; i!=100; ++i)
{
x+=i;
}
std::cout << x << std::endl;
}
24
Special for-loops
int main()
{
//Empy for-loop
for (int i = 0; i!=10; ++i) ;
//Infinite loop
for ( ; ;) { }
//Infinite empty loop
for ( ; ;) ;
}
25
Question
• How should do the layout if his/her code?
– Indentation?
– Tabs?
26
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
27
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
}
28