Control Structures in C++
Lyle N. Long
Aerospace Engineering
Penn State University
Copyright, Oct. 2003
1
References
C++ and Object-Oriented Numeric Computing,
by D. Yang
"Core C++," by Shtern
Selection Structures
IF
single
IF / ELSE
double
SWITCH
Multiple
GOTO
Repetition Structures
WHILE
DO / WHILE
FOR
IF
double x;
if ( x > 2.0 )
cout << x is greater than 2;
Possibilities:
>
<
>=
<=
==
!=
5
Nested IF
double x;
if ( x > 2.0 )
if ( x < 3.0 )
cout << x is greater than 2.0 and less than 3.0;
Nested IF
I think you are apt
to have fewer
potential errors if
you do this. (ie use
brackets)
double x;
if ( x > 2.0 ) {
if ( x < 3.0 ) {
cout << x is greater than 2.0 and less than 3.0;
}
}
7
IF
double x;
if ( x > 2.0 && x < 3.0 )
cout << x is greater than 2.0 and less than 3.0;
Possibilities:
&&
||
(and)
(or)
IF
double x;
if ( x > 2.0 && x < 3.0 ) {
cout << x is greater than 2.0 and less than 3.0;
}
Possibilities:
&&
||
(and)
(or)
Again, I think you
are apt to have
fewer potential
errors if you do this.
(ie use brackets)
IF
bool doprint;
doprint = true;
if ( doprint ) cout << doprint is true !;
10
IF
bool doprint;
doprint = true;
if ( doprint ) {
cout << doprint is true !;
cout << you can put several ;
cout << lines inside {} << endl;
}
11
IF / ELSE
bool doprint;
doprint = false;
if ( doprint )
cout << doprint is true !;
else
cout << doprint is false !;
12
IF / ELSE
bool doprint;
doprint = false;
if ( doprint ) {
cout << doprint is true !;
cout << you can use {} ;
}
else {
cout << doprint is false !;
cout << you can use {} ;
}
13
IF / ELSE
double x;
if ( x >= 90.0 )
cout << you get an A !;
else
if ( x >= 80.0 )
cout << you get a B !;
else
if ( x >= 60.0 )
cout << you get a C !;
else
if ( x >= 40.0 )
cout << you get a D !;
else
cout << you get an F !;
14
IF / ELSE
double x;
if ( x >= 90.0 )
cout << you get an A !;
else if ( x >= 80.0 )
cout << you get an B !;
else if ( x >= 60.0 )
cout << you get a C !;
else if ( x >= 40.0 )
cout << you get a D !;
else
cout << you get an F !;
15
This might
be a better
way to write
nested if/else
true and false
true and false are both keywords in C++ and
Java (but not in C)
Usually true=1 and false=0, but it is better
to actually use the keywords, instead of 0 or
1.
You can write an infinite loop via:
while ( true ) { do something};
This would work too, but dont do this:
while ( 1 ) { do something};
16
WHILE
double x;
while ( x < 20.0 )
x = x * 2.0;
17
WHILE
double x;
while ( x < 20.0 ) {
x = x * 2.0;
cout << x is less than 20.0 << endl;
}
18
WHILE
double x;
int i;
i = 1;
while ( i < 200 ) {
++i;
x = x * 2.0;
}
19
See any problems here?
WHILE
double x;
int i;
i = 1;
while ( i < 200 ) {
++i;
x = x * 2.0;
}
20
See any problems here?
x is not initialized!
FOR
index
conditional
incrementer
for ( int i =0 ; i <= 20; i++ )
cout << i = << i << endl;
21
FOR
index
conditional
incrementer
for ( int i =0; i <= 20; i++ ) {
cout << i = << i ) ;
cout << you can use multiple lines << endl;
}
22
BREAK
for ( int i=0; i <= 20 ; i++ ) {
if ( i >= 10 ) break;
cout << i is less than 10;
}
23
SWITCH
for ( int i=0; i <= 20 ; i++ ) {
switch ( i % 3 ) {
case 1:
cout << case 1 ;
break;
case 2:
cout << case 2;
break;
default:
cout << default ;
break;
}
}
24
what does
this do ????
goto
#include <iostream>
#include <cmath>
using namespace std;
int main() {
tryAgain: // this is a statement label
cout << "Enter a non-negative number";
double dX;
cin >> dX;
You should usually
avoid the goto
statement. Usually
it would be better
to use exception
handling instead.
if (dX < 0.0)
goto tryAgain; // this is the goto statement
cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl;
}
25
C++ Keywords
Storage:
char
unsigned
extern
short int
struct union
volatile const
The blue ones we have discussed already
here! We will discuss the red ones next,
most of which deal with OOP, dynamic
memory, and exception handling.
You need to know all these keywords.
long
float
class
enum
typedef void
double bool
auto
static
sizeof
signed
register
else
do
case
Control:
if
break
while
goto
switch continue
default return
false
try
inline
asm
catch
operator
for
Other:
true
protected
virtual
26
friend
throw
template
public private
new
delete
this
CONCLUSIONS
Selection Approaches:
if , if / else , goto, and switch
Repetition Approaches:
while , do while , and for
27