Control Structures in C++: Lyle N. Long Aerospace Engineering Penn State University
Control Structures in C++: Lyle N. Long Aerospace Engineering Penn State University
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)
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
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
WHILE
double x;
int i;
i = 1;
while ( i < 200 ) {
++i;
x = x * 2.0;
}
20
x is not initialized!
FOR
index
conditional
incrementer
21
FOR
index
conditional
incrementer
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;
C++ Keywords
Storage:
char
unsigned
extern
short int
struct union
volatile const
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