Download presentation
Presentation is loading. Please wait.
Published byAvis Singleton Modified over 9 years ago
2
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010
3
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
4
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.
5
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
6
Bina Nusantara University 6 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” );
7
Bina Nusantara University 7 Flow chart If Selection Structure
8
Bina Nusantara University 8 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” );
9
Bina Nusantara University 9 Flow Chart If/else Selection Structure
10
Bina Nusantara University 10 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”
11
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” ); else if ( studentGrade >= 60 ) Console.WriteLine( “C” ); else Console.WriteLine( “D” );
12
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” );
13
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 list Example: int product = 2; while ( product <= 1000 ) product = 2 * product
14
Bina Nusantara University 14 While Repetition Structure
15
Bina Nusantara University 15 Assignment Operators Assume: int c = 3, d = 5, e = 4, f = 6, g = 12 Assignment operatorSample expressionExplanationAssigns +=c += 7c = c + 710 to c -=d -= 4d = d - 41 to d *=e *= 5e = e * 520 to e /=f /= 3f = f / 52 to f %=g %= 9g = g % 93 to g
16
Bina Nusantara University 16 Increment and Decrement Operators
17
Bina Nusantara University 17 Increment and Decrement Operators
18
Bina Nusantara University 18 Increment and Decrement Operators Output:
19
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 var Bina Nusantara University 19 for ( expression1; expression2; expression3 ) statement
20
Bina Nusantara University 20 For Repetition Structure
21
Bina Nusantara University 21 For Repetition Structure
22
Bina Nusantara University 22 Switch Multiple-Selection Structure
23
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.
24
Bina Nusantara University 24 Do/While Repetition Structure do statement OR{ while ( condition );statement } while ( condition );
25
25 Do/While Repetition Structure
26
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.
27
Bina Nusantara University 27 Statements break and continue
28
Bina Nusantara University 28 Statements break and continue
29
Bina Nusantara University 29 References http://books.google.co.id/books?id=8R451rZJ5o0C &printsec=frontcover&dq=c%23&cd=2#v=onepage& q=&f=falsehttp://books.google.co.id/books?id=8R451rZJ5o0C &printsec=frontcover&dq=c%23&cd=2#v=onepage& q=&f=false
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.