Introduction To Programming: 3 Statements
Introduction To Programming: 3 Statements
Summer Term
3 Statements
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 48
3 Statements ...
Contents
➥ Expressions
➥ Operators
➥ Input and output
➥ if statement
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 49
3 Statements ...
Statements
➥ Variables are the program’s data
➥ Statements specify what to do with the data
➥ Different kinds of statements:
➥ each expression is a statement
➥ always terminated with a ;
➥ e.g. assignment: x = a + b;
➥ compound statement: { list-of-statements }
➥ empty statement: (white space and/or ; )
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 50
3.1 Expressions
Expressions
➥ Anything that returns a value is an expression in C++
➥ An operator is a symbol that causes the program to perform an
operation (e.g., + for the summation)
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 51
3.1 Expressions ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 52
3.1 Expressions ...
Mathematical operators
➥ On floats: + - * /
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 53
3.1 Expressions ...
➥ Also defined: -= *= /= %=
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 54
3.1 Expressions ...
➥ Examples:
➥ x = 5; a = x++; afterwards, a is 5, and x is 6
➥ x = 5; a = ++x; afterwards, a is 6, and x is 6
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 55
3.1 Expressions ...
Precedence
➥ Defines the order in which operators are evaluated
Examples:
➥ x = 5 + 3 - 8 * 9; ≡ x = (5 + 3) - (8 * 9);
➥ x = y = z = 0; ≡ x = (y = (z = 0));
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 56
3.1 Expressions ...
Precedence ...
☞ http://en.cppreference.com/w/cpp/language/operator precedence
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 57
3.1 Expressions ...
Bit operations
➥ Reminder: in a computer, all data (like, e.g., numbers) is stored in
a binary form
➥ e.g., 291 = 0 ... 0 0 0 0 1 0 0 1 0 0 0 1 1
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 58
3.1 Expressions ...
Relational operators
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 60
3.2 Input and Output
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 63
3.3 The if Statement
The if statement
➥ Normally, statements are executed in the order they appear in the
program (top to bottom).
➥ Sometimes, statements shall only be executed if some condition
holds:
if ( expression ) // If condition is true,
statement; // execute the statement
➥ Example:
if ( new_value < minimum )
minimum = new_value;
➥ Note that statement; can be replaced by a compound statement
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 64
3.3 The if Statement ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 65
3.3 The if Statement ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 66
3.3 The if Statement ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 67
3.3 The if Statement ...
Nested if statements
➥ What does this program print to cout?
int main() {
bool a = true, b = false;
if ( a )
if ( b )
cout << "a and b\n";
else
cout << "not a\n";
return 0;
}
➥ Always use { and } for if and else clauses!
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 68
3.3 The if Statement ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 69
3.3 The if Statement ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 70
Summary
Statements
➥ Variables are the program’s data
➥ Statements specify what to do with the data
➥ Different kinds of statements:
➥ each expression is a statement
➥ always terminated with a ;
➥ e.g. assignment: x = a + b;
➥ compound statement: { list-of-statements }
➥ empty statement: {} or just a ;
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 71
Summary ...
Expressions
➥ Anything that returns a value is an expression in C++
➥ An operator is a symbol that causes the program to perform an
operation (e.g., + for the summation)
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 72
Summary ...
Reminder: if Statement
int main() {
bool a = true, b = false;
if ( a ) {
if ( b ) {
cout << "a and b\n";
}
}
else {
cout << "not a\n";
}
return 0;
}
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 73
Summary ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 74