Document 15063150

advertisement
Mata kuliah : M0874 – Programming II
Tahun
: 2010
Control Structures
Session 03
Outline Materi
•Pseudocode
•Control Structure
•If Selection Structure
•If/else Selection Structure
•While Repetition Structure
•Assignment Operators
•Increment and Decrement Operators
•For Repetition Structure
•Switch Multiple-Selection Structure
•Do/While Repetition Structure
•Statements break and continue
Bina Nusantara University
3
Pseudocode
•An artificial an informal language that helps
programmers develop algorithms.
•Similar to everyday english; it is convenient and
user-friendly, and it is not an actual computer
programming language.
•Not executed on computers.
•Helps the programmer “think out” a program before
attempting to write it in a programming language.
Bina Nusantara University
4
Control Structure
•Three types of selection structures:
•If selection structure
•If/else selection structure
•Switch selection structure
•Four repetition structures:
•While
•Do/while
•For
•Foreach
Bina Nusantara University
5
If Selection Structure
If student’s grade is greater than or equal to 60
Print “passed”
Pseudocode if statement:
if ( studentGrade >= 60 )
Console.WriteLine ( “Passed” );
Bina Nusantara University
6
Flow chart If Selection Structure
Bina Nusantara University
7
If/else Selection Structure
If student’s grade is greater than or equal to 60
Print “Passed”
else
Print “Failed”
Pseudocode if/else structure:
if ( studentGrade >= 60 )
Console.WriteLine( “Passed” );
else
Console.WriteLine( “Failed” );
Bina Nusantara University
8
Flow Chart If/else Selection Structure
Bina Nusantara University
9
Nested if/else
if student’s grade is greater than or equal to 90
Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 60
Print “C”
else
Print “D”
Bina Nusantara University
10
Nested if/else
Pseudocode may be written as:
if ( studentGrade >= 90 )
Console.WriteLine ( “A” );
else
if ( studentGrade >= 80 )
Console.WriteLine ( “B” );
else
if ( studentGrade >= 60 )
Console.WriteLine( “C” );
else
Console.WriteLine( “D” );
Bina Nusantara University
11
Nested if/else
Most C# programmers prefer to write the preceding
if structure as:
if ( studentGrade >= 90 )
Console.WriteLine ( “A” );
else if ( studentGrade >= 80 )
Console.WriteLine ( “B” );
else if ( studentGrade >= 60 )
Console.WriteLine( “C” );
else
Console.WriteLine( “D” );
Bina Nusantara University
12
While Repetition Structure
•Specify that an action is to be repeated while a
condition remains true.
•The pseudocode statement
While there are more items on my shopping list
Purchase next item and cross it off my list
Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product
Bina Nusantara University
13
While Repetition Structure
true
product <= 1000
product = 2 * product
false
Bina Nusantara University
14
Assignment Operators
Assume:
int c = 3, d = 5, e = 4, f = 6, g = 12
Assignment operator
Sample expression
Explanation
Assigns
+=
c += 7
c=c+7
10 to c
-=
d -= 4
d=d-4
1 to d
*=
e *= 5
e=e*5
20 to e
/=
f /= 3
f=f/5
2 to f
%=
g %= 9
g=g%9
3 to g
15
Bina Nusantara University
Increment and Decrement Operators
Bina Nusantara University
16
Increment and Decrement Operators
17
Bina Nusantara University
Increment and Decrement Operators
•Output:
Bina Nusantara University
18
For Repetition Structure
for ( expression1; expression2; expression3 )
statement
for keyword
counter variable name
final value of control variable
for ( int counter = 1; counter <= 5; counter++ )
Initial value of control variable loop-continuation condition increment of
control var
Bina Nusantara University
19
For Repetition Structure
Bina Nusantara University
20
For Repetition Structure
Bina Nusantara University
21
Switch Multiple-Selection Structure
Bina Nusantara University
22
Do/While Repetition Structure
•Do/while repetition structure is similar to the while
structure.
•In the while structure, the test of the loopcontinuation condition occurs at the beginning of the
loop, before the body of the loop executes.
•The do/while structure tests the loop-continuation
condition after the loop body executes; therefore,
the loop body always executes at least once.
Bina Nusantara University
23
Do/While Repetition Structure
do
statement
while ( condition );
Bina Nusantara University
OR
do
{
statement
} while ( condition );
24
Do/While Repetition Structure
25
Statements break and continue
•The break and continue statements alter the flow of
control.
•The break statement, when executed in a while, for,
do/while or switch structure, cause immediate exit
from that structure.
•The continue statement, when executed in a while,
for or do/while structure, skips the remaining
statements in the body of that structure and
proceeds with the next iteration of the loop.
Bina Nusantara University
26
Statements break and continue
Bina Nusantara University
27
Statements break and continue
Bina Nusantara University
28
References
•http://books.google.co.id/books?id=8R451rZJ5o0C
&printsec=frontcover&dq=c%23&cd=2#v=onepage&
q=&f=false
Bina Nusantara University
29
Download