1 cis 230 25-jan-06. 2 overview selection statements –if statement –else –nested if-else...
Post on 19-Dec-2015
220 views
TRANSCRIPT
1
CIS 230
25-Jan-06
2
Overview
• Selection Statements– If Statement– Else– Nested If-Else– Switch
• Repetition Statements– While statement– For Statement– Infinite loops– Do-While Statement– Break– Continue
• Formatted Output
3
Selection Statements
• if-else– Decision making statement
• switch– Allows choice: one out of several options
4
If Statement
if ( test )
statement;
if ( test )
{
statements;
}
if true (i.e. != 0) do statementif false (i.e. == 0) skip it
5
If Statement
if ( x == 7 ) cout << “you win\n”;
if ( value != 0){ statements;}SAME AS:if (value){ statements;}
Remember to use == to compare.Using = will assign the value, and will ALWAYS be true.
6
Else
if ( test )
true statement;
else
false statement;
if ( test )
{
true statements;
}
else
{
false statements;
}
7
Else
cout << “Please enter a positive integer”;cin >> value;if (value > 0) cout << “Thank you!\n”;else{ cout << “The value entered was not positive\n”; cout << “I will use the value 10 \n”; value = 10;}
8
Nested If-Else
if (x > 0)
cout << "x is positive";
else
if (x < 0)
cout << "x is negative";
else cout << "x is 0";
9
Nested If-Else
cin >> command;if ( command == ‘u’ ) cout << “Move up” << endl;else if ( command == ‘d’ ) cout << “Move down” << endl;else if ( command == ‘l’ ) cout << “Move left” << endl;else if ( command == ‘r’ ) cout << “Move right” << endl;else cout << “Invalid command received << endl;
10
Nested If-Elsecin >> command;if ( command == ‘u’ ) cout << “Move up” << endl;else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received << endl;
11
Switch
switch (value){ case constant1: statement1; statement; break; case constant2: statement2; break; default: statement; break;}
• Cases may be in any order• “case” is terminated with a :
(colon)• “constants” can be integers or
characters• Cases may have several
statements• break jumps out of the
construct• default is optional
12
Switchcin >> choice;switch (choice){ case 0: cout << “Just a beginner huh?\n”; moves = 15; break; case 1: case 2: case 3: cout << “OK, let’s begin \n”; moves = 10; break; case 4: cout << “You only have 7 moves \n”; moves = 7; break; default: cout << “Incorrect selection \n”; moves = 0; break;}
Only way to have several choices do the same thing.
13
If-else-if vs Switchcin >> command;if ( command == ‘u’ ) cout << “Move up” << endl;else if ( command == ‘d’ ) cout << “Move down” << endl; else if ( command == ‘l’ ) cout << “Move left” << endl; else if ( command == ‘r’ ) cout << “Move right” << endl; else cout << “Invalid command received <<
endl;
cin >> command;switch ( command ){ case ‘u’: cout << Move up” << endl; break; case ‘d’: cout << “Move down” << endl; break; case ‘l’: cout << “Move left” << endl; break; case ‘r’: cout << “Move right” << endl; break; default: cout << “Invalid command received” << endl; break;}
14
Repetition Statements
• while loops– pretest loop
• for loops– pretest loop
• do-while loops– posttest loop
• pretest loop: – Test at the beginning
of the loop– Condition must be set
prior to loop entry.
• posttest loop:– Test at the end of the
loop– Always executes at
least once.
15
While statement
while ( expression )
statement;
while ( expression )
{
statements;
}Evaluates the expression
if true (i.e. != 0) executes statement(s)
Whiletest
Body
true false
16
While statement
while ( x < 0 )
x = x – y + z;
while ( a <= b)
{
a = a + 1;
b = b / 2;
total = total + b;
}
17
For Statement
for ( initialize; expression; alter )
statement;
for ( initialize; expression; alter )
{
statement(s);
}
Initialize
fortest
Body
Alter
false
true
18
For Statement
int i;
for ( I = 0; I <= 10; i++)
{
cout << i << endl;
}
int answer, a, c, b;
a = 0;
b = 100;
answer = 0;
for (c = a; c <= b; c++)
answer = answer + c;
19
While vs. For
int i = 0;
while ( i <= 10)
{
cout << i << endl;
i++;
}
for (int i = 0; i <= 10; i++)
cout << i << endl;
20
Beware the infinite loop!
for (c = a; c <= b; 1) answer = answer + c;
for ( c = a; 10; c++) answer = answer + c;
while (i < 10){ cout << i << endl;}
c does not change, infinite loop
Always true, infinite loop
i is never incremented; infinite loop
21
Do-While Statement
do
{
body;
}
while ( expression )
executes statement(s)evaluates the expression
if true (i.e. != 0)
do
body
whiletest
true
false
22
Do-While statement
do
{
cout << “Enter a number between 1 & 10 \n”;
cin >> value;
}
while ( value < 1 || value > 10 )
23
Break
• break– Can also be used to exit (jump out) of other loops as
wellfor (i = 1; a <= b; i++){ a += i; //a = a + i b -= i; //b = b – i if (b <=0) break; c += a + b //c = c + (a + b)}
Can end in two ways
24
Continue
• continue– Goes to the test of a loop (Does the test again)– Must be a loop not a switch
while ( a < b){ cin >> a; if (a < 0 || a > 9) continue; //process rest of loop}
25
Formatted Output
• #include <iomanip>
• setw( )
• fixed
• setprecision( )
• setiosflags( )
26
Formatted Output
cout << 6 << endl
<< 18 << endl
<< 124 << endl
<< "---\n"
<< (6+18+124) << endl;
Results:
6
18
124
---
148
27
setw()
• Used within cout
• Sets field width for next output
• Right justifies
28
setw()
//Added setw(3) to each line
cout << setw(3) << 6 << endl
<< setw(3) << 18 << endl
<< setw(3) << 124 << endl
<< "---\n"
<< (6+18+124) << endl;
Results:
6
18
124
---
148
29
setw
int a = 24;
int b = 573;
cout << '|' << setw(4) << a
<< '|' << setw(6) << b
<< '|' << setw(2) << b
<< '|' << endl;
Results:
| 24| 573|573|
30
fixed
• Used with cout
• Forces the display of a decimal point
31
setprecision()
• Used with cout
• Sets maximum number of decimal places– Will show less if fewer decimal values
• Floating point numbers only
32
setprecision()
float x = 19.1738;
float y = 6.4;
cout << '|' << setw(7) << setprecision(2)
<< y << '|' << endl;
cout << '|' << setw(7) << fixed
<< setprecision(2) << x << '|'
<< endl;
cout << '|' << setw(7) << fixed
<< setprecision(2) << y << '|'
<< endl;
Results:
| 6.4|
| 19.17|
| 6.40|
33
setiosflags()
• ios::fixed
• ios::scientific
• ios::showpoint
• ios::showpos
• ios::left
• ios::right