0% found this document useful (0 votes)
31 views

03 Control Structures

The document discusses control structures, branches, scopes, and while loops in C++ programming. It covers topics like defining natural numbers and variables in C++, incrementing variables, plotting variable values to the terminal, and using comparison, logical, and assignment operators for boolean expressions and conditional statements. Examples are provided for calculating the maximum of two numbers, evaluating trigonometric functions, and using strict evaluation to avoid unnecessary computations.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

03 Control Structures

The document discusses control structures, branches, scopes, and while loops in C++ programming. It covers topics like defining natural numbers and variables in C++, incrementing variables, plotting variable values to the terminal, and using comparison, logical, and assignment operators for boolean expressions and conditional statements. Examples are provided for calculating the maximum of two numbers, evaluating trigonometric functions, and using strict evaluation to avoid unnecessary computations.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Control Structures

Branches

Scopes

While Loops

Prime Numbers

3. Control Structures, Branches, and Scopes March 9, 2011

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;

How do you increment a natural number (variable)?

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 do you increment a natural number (variable)? a ++; a=a +1;

What is the difference? a=b a==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 do you increment a natural number (variable)? a ++; a=a +1;

What is the difference? a=b a==b

How can we plot the value of a variable to the terminal?

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 do you increment a natural number (variable)? a ++; a=a +1;

What is the difference? a=b a==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

Recap: Initialisation Pitfalls IIIAssignment Operators


int a = 3 bool b = (a==3); bool c = (a=3); int d = 0 bool e = (d==0); bool f = (d=0);

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

Missing Slide: Initialisation Pitfalls IVFreaky Collaborators


int a = 3; int b = ++a; (that is want we want) int c = 3; int d = c++; (that is something different) int e = (d+=a)++; (also very funny) int f = ((d=4)+a)--;

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.1. Control Structures


We want to compute the maximum of two oating point values.
# 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 ; max = b ; std : : cout < < max i s < < max < < std : : endl ; return 0; }

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

a true true false false

b true false true false

a | b true true true 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

Machine Code Stream


# 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 ; b o o l i s A G r e a t e r = a>b ; max = a ; max = b ; std : : cout < < max i s < < max < < std : : endl ; return 0; }

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; }

This is a new expression of the form lvalue = expression ? expression :

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; }

Again, let x be a oat. Compute x =


sin(x ) . x

Test x {20.0, 3.1415, 1.0, 0.25, 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; }

Same setting as before, but modied formula. Test x {1.0, 0.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

Non-strict Operator Semantics


a true true false false b true false true false a & b true false false false a true true false b true false ? a && b true false false

a true true false false

b true false true false

a | b true true true false

a true false false

b ? true false

a || b true 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

Operator Semantics Revisited


a true true true false false false b true false true false a & b true false false false a true true true false false false b true false true false a && b true false false false false

a true true true false false false

b true false true false

a | b true true true false

a true true true false false false

b true false true false

a || b true true true true false

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

3.2. Branches An Alternative Formulation of the max Function


double a = . . . ; double b = . . . ; double max = a<b ? b : a ; ...

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

double max; if (a < b) max = b; else max = c;

21: 22; 23; 24; 25; 26; 27; 28: 29:

? 4 5

max a b

113: 114: 115: 116: 117: 118: 119:

(a<b)? yes PC := 119 max := a PC := 120 max := 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

Nested if-else Statements


Nested statements. i f ( a<b ) { ... i f ( a==b ) { ... } } else { ... } Concatenated if statements i f ( a==b ) { ... } e l s e i f ( a<b ) { ... } else { ... }

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

more sophisticated called enum later on.

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

Switch StatementsAn Example


i n t vehicleClass ; double t o l l ; switch ( vehicleClass ) { case 1 : std : : cout < < car ; t o l l = 0.50; break ; case 2 : std : : cout < < bus ; t o l l = 1.50; break ; default : std : : cout < < not motorized ; t o l l = 0.0; break ; }

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.

This hides variablesthey are not overwritten.


This whole thing relies solely on the syntax, i.e. it has nothing to do with the actual

execution. Otherwise,
i f ( a==20) { i n t b = 30; } std : : cout < < b= < < b < < std : : endl ;

might work in some cases.


3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 36

Control Structures

Branches

Scopes

While Loops

Prime Numbers

3.4. While Loops


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 30 of 36

Control Structures

Branches

Scopes

While Loops

Prime Numbers

While Loops in Action


double a = 4 0 . 0 ; while ( a > 2.0) { std : : cout < < a= < < a< < std : : endl ; a /= 2 . 0 ; }

a=40 a=20 a=10 a=5 a=2.5 a=1.25

(?)

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 ; }

a=40 a=20 a=10 a=5 a=2.5 a=1.25

(?)

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

Syntax of While Loops


while ( expression ) one s t a t em e n t ; while ( expression ) { m u l t i p l e s t a t em e n t s ; }

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

Properties of While Loops


int i ; w h i l e ( i >40) { int a = i /20; ... }

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

Properties of While Loops


int i ; w h i l e ( i >40) { int a = i /20; ... }

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) { ... }

However, many consider this to be a bad smell.


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

3.5. Prime Numbers


Run through all the integers from 0 to

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

Prime NumbersA Code Template


i n t currentNumber = 2 ; w h i l e ( currentNumber <=100) { bool r e s u l t = t r u e ; i n t currentStudy = 2; w h i l e ( c u r r e n t S t u d y < currentNumber ) { i f ( currentNumber % c u r r e n t S t u d y == . . . ) { ... } ... } i f ( result ) { std : : cout < < ... } else { std : : cout < < ... } currentNumber ++; }

3. Control Structures, Branches, and Scopes Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 36 of 36

You might also like