control structures session 03 mata kuliah: m0874 – programming ii tahun: 2010

29

Upload: avis-singleton

Post on 29-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Control StructuresSession 03

Mata kuliah : M0874 – Programming IITahun : 2010

Bina Nusantara University

3

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

4

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

5

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

6

If Selection Structure

If student’s grade is greater than or equal to 60Print “passed”

Pseudocode if statement:if ( studentGrade >= 60 )

Console.WriteLine ( “Passed” );

Bina Nusantara University

7

Flow chart If Selection Structure

Bina Nusantara University

8

If/else Selection Structure

If student’s grade is greater than or equal to 60Print “Passed”

elsePrint “Failed”

Pseudocode if/else structure:if ( studentGrade >= 60 )

Console.WriteLine( “Passed” );else

Console.WriteLine( “Failed” );

Bina Nusantara University

9

Flow Chart If/else Selection Structure

Bina Nusantara University

10

Nested if/else

if student’s grade is greater than or equal to 90Print “A”

elseif 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

11

Nested if/else

Pseudocode may be written as:if ( studentGrade >= 90 )

Console.WriteLine ( “A” );else

if ( studentGrade >= 80 )Console.WriteLine ( “B” );

elseif ( studentGrade >= 60 )

Console.WriteLine( “C” );else

Console.WriteLine( “D” );

Bina Nusantara University

12

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

13

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 listExample:

int product = 2;while ( product <= 1000 )

product = 2 * product

Bina Nusantara University

14

While Repetition Structure

product <= 1000 product = 2 * producttrue

false

Bina Nusantara University

15

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

Bina Nusantara University

16

Increment and Decrement Operators

Bina Nusantara University

17

Increment and Decrement Operators

Bina Nusantara University

18

Increment and Decrement Operators

•Output:

For Repetition Structure

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 varBina Nusantara

University19

for ( expression1; expression2; expression3 )statement

Bina Nusantara University

20

For Repetition Structure

Bina Nusantara University

21

For Repetition Structure

Bina Nusantara University

22

Switch Multiple-Selection Structure

Bina Nusantara University

23

Do/While Repetition Structure

•Do/while repetition structure is similar to the while structure.•In the while structure, the test of the loop-continuation 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

24

Do/While Repetition Structuredo do

statement OR {while ( condition ); statement

} while ( condition );

25

Do/While Repetition Structure

Bina Nusantara University

26

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

27

Statements break and continue

Bina Nusantara University

28

Statements break and continue