03 Control Structures
03 Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Outline
Recapitulation & Missing Slides Simple Control Structures Strict Evaluation Branches Scopes An Example: Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Recapitulation
This is both, a control opportunity for the audience and a feedback mechanism for the lecture. It is denitely not some kind of exam or a competition.
How do you dene a natural number in C++?
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Recapitulation
This is both, a control opportunity for the audience and a feedback mechanism for the lecture. It is denitely not some kind of exam or a competition.
How do you dene a natural number in C++? int a; int b;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Recapitulation
This is both, a control opportunity for the audience and a feedback mechanism for the lecture. It is denitely not some kind of exam or a competition.
How do you dene a natural number in C++? int a; int b;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Recapitulation
This is both, a control opportunity for the audience and a feedback mechanism for the lecture. It is denitely not some kind of exam or a competition.
How do you dene a natural number in C++? int a; int b;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Recapitulation
This is both, a control opportunity for the audience and a feedback mechanism for the lecture. It is denitely not some kind of exam or a competition.
How do you dene a natural number in C++? int a; int b;
How can we plot the value of a variable to the terminal? std : : cout < < a;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Recapitulation
Default Values int a; / / some computation a = / r e s u l t / ;
What is a declaration? Do default values induce performance penalties? Do default values facilitate bugs?
Using g++
g++ is both compiler and linker. Well use multiple les later (actually we already do). You can shorten the things you have to type in by g++ -o applicationame file1.cpp file2.cpp file3.cpp but that really is just a short cut.
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 5 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
C/C++ offer many strange constructs. This is good luck for posers, freaks, and people giving lectures. Others should avoid them.
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Operators
Operator Comparison (equals) Comparison (smaller) Comparison (greater) And Or Not Symbol == < < & | ! Symbol (non strict) Works for . . . doubles (machine precision) and booleans doubles (machine precision) doubles (machine precision) booleans booleans booleans
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Operator Semantics
a true true false false b true false true false a & b true false false false
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Examples
# i n c l u d e <iostream > i n t main ( ) { double a = 0 . 5 5 ; double b = 2 3 . 2 ; std : : cout < < a is < < a< < std : : endl ; std : : cout < < b is < < b< < std : : endl ; double max ; bool bool bool bool bool bool bool ... } b0 b1 b2 b3 b4 b5 b6 = = = = = = = a==b ; ! ( a==b ) ; b0 | b1 ; b0 | ( a<b ) ; a<=b ; ( a<=b ) & ( a>=b ) ; ! ( ( a<=b ) & ( a>=b ) ) ;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
How can we tell the computer to increase the program counter without executing a line?
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Solution
# i n c l u d e <iostream > i n t main ( ) { double a = 0 . 5 5 ; double b = 2 3 . 2 ; std : : cout < < a is < < a< < std : : endl ; std : : cout < < b is < < b< < std : : endl ; double max ; max = a>b ? a : b ; std : : cout < < max i s < < max < < std : : endl ; return 0; }
expression.
It is interpreted from left to right. We could also write something like max = a>b ? a : b*2.
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 12 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
First Example
# i n c l u d e <iostream > # i n c l u d e <math . h> o r <math> o r <cmath> o r math . h i n t main ( ) { double x = ; double r e s u l t = s t d : : s i n ( x ) / x ; std : : cout < < result is < < result < < std : : endl ; return 0; }
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 13 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Strict Evaluation
# i n c l u d e <iostream > # i n c l u d e <math . h> o r <math> o r math . h i n t main ( ) { double a , h ; double r e s u l t = ( x ==0) | ( 1 / x = = 1 . 0 ) ? 1 . 0 : s t d : : s i n ( x ) / x ; std : : cout < < result is < < result < < std : : endl ; return 0; }
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Strict Evaluation
# i n c l u d e <iostream > # i n c l u d e <math . h> o r <math> o r math . h i n t main ( ) { double a , h ; double r e s u l t = ( x ==0) | ( 1 / x = = 1 . 0 ) ? 1 . 0 : s t d : : s i n ( x ) / x ; std : : cout < < result is < < result < < std : : endl ; return 0; }
Same setting as before, but modied formula. Test x {1.0, 0.0}. sometimes, boolean expression should not be evaluated completely. often, this would be expensive anyway.
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Operators
Operator Comparison (equals) Comparison (smaller) Comparison (greater) And Or Not Symbol == < < & | ! Symbol (non strict) Works for . . . doubles (machine precision) and booleans doubles (machine precision) doubles (machine precision) booleans booleans booleans
&& ||
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
b ? true false
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 16 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Alternative Formulation:
double a = . . . ; double b = . . . ; double max ; i f ( a<b ) max = b ; else max = a ; ...
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
The if-Statement
Memory
? 4 5
max a b
PC
...
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 19 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
if-else Statements
i f ( boolean e x p r e s s i o n ) Yes s ta t e m e n t else No s t a t e me n t i f ( boolean e x p r e s s i o n ) { Yes st a t e m en t s } else { No s t at e m e n ts }
First variant: Only one statement executed. Second variant: Several statements can be executed. Else statement always is optional.
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 20 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
if-else Pitfalls
i f (a!=0) result = 1.0/a; else { result = 0.0; std : : cout < < max s e t t o < < d e f a u l t value < < std : : endl ; }
Pitfall I: Never use the = operator! Pitfall II: Always use the brackets { } .
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Switch Statements I
An alternative to concatenated if statements for integers is the switch statement.
int a; ... switch ( a ) case 0 : result break ; case 1 : result break ; case 2 : result break ; }
{ = 10;
= 23;
= 25;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 23 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Switch Statements II
s w i t c h ( expr ) { case x : ...; break ; case x : ...; break ; default : ...; break ; }
Always provide a default branch. Always use a break command. Always use == instead of =. Never use a switch statement for integers (bad style). There will be something
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
What happens for vehicleClass=2? What happens for vehicleClass=4? What happens if we remove rst break and vehicleClass=1?
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3.3. Scopes
i n t a=; ... a = 20; ... i f ( something happens ) { a = 30; } ...
A variable is an abstraction of a memory address. It consequently has to be declared before we use it.
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
A First Experiment
int a; a = 20; std : : cout < < a= < < a < < std : : endl ; i f ( a==20) { i n t b = 10; std : : cout < < b= < < b < < std : : endl ; } else { b = 0; std : : cout < < b= < < b < < std : : endl ; } std : : cout < < b= < < b < < std : : endl ;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
A Second Experiment
int a; a = 20; std : : cout < < a= < < a < < std : : endl ; i f ( a==20) { / Replace t h e f o l l o w i n g l i n e with i n t a = 30; / a = 30; std : : cout < < a= < < a < < std : : endl ; } std : : cout < < a= < < a < < std : : endl ;
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Scopes
A variable has to be declared within the same brackets { } . Otherwise, compiler searches within the enclosing brackets (not within the
neighbours).
The brackets dene a scope. The outer brackets dene an outer scope. Some compilers allow the programmer to redene variables within subscopes.
execution. Otherwise,
i f ( a==20) { i n t b = 30; } std : : cout < < b= < < b < < std : : endl ;
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 30 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
(?)
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 31 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
A While in Action
double a = 4 0 . 0 ; while ( a > 2.0) { std : : cout < < a= < < a< < std : : endl ; a /= 2 . 0 ; }
(?)
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Code performs (multiple) iterations. Each iterations executes the complete loop body. Expression is the guard of the block (scope). It is evaluated before the loop body is
executed.
If expression is false the rst time, loop reduces to no operation (nop).
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Variables contained in the expression have to be declared outside. Inner variable declarations are not visible in the guard expression. Inner variables might hide (not overwrite) outer variables (see scoping rules)
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 34 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
Variables contained in the expression have to be declared outside. Inner variable declarations are not visible in the guard expression. Inner variables might hide (not overwrite) outer variables (see scoping rules) Again, avoid intermixing = and ==. As while guard expects an expression, we could however modify variables. w h i l e ( ( i ++) > 40) { ... }
Control Structures
Branches
Scopes
While Loops
Prime Numbers
100.
Check for each number whether it is a
prime number.
Write the result of the check to the
terminal.
Check is very simple:
Take a second variable running from 0 to the current integer studied. Evaluate the modulo (operator %).
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 35 of 36
Control Structures
Branches
Scopes
While Loops
Prime Numbers
3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 36 of 36