OOP Chapter 4 Control Structures Part 1
OOP Chapter 4 Control Structures Part 1
4.1 Introduction
4.2 Algorithms
• Procedure
– The actions a program will perform
– The order these actions will be performed
– Also called an algorithm
• Program control
– The task of ordering a computers procedure correctly
4.3 Pseudocode
• Pseudocode
– Artificial and informal language
– Helps programmers to plan an algorithm
– Similar to everyday English
– Not an actual programming language
– Converting simply replaces words with C# code
• Program of control
– Program performs one statement then goes to next line
• Sequential execution
– Different statement other than the next one executes
• Selection structure
– The if and if/else statements
– The goto statement
• No longer used unless absolutely needed
• Causes many readability problems
• Repetition structure
– The while and do/while loops (chapter 5)
– The for and foreach loops (chapter 5)
• Flow carts
– Used to map program
– Illustrates the order events will go in
• Rectangle used to show action
• Oval used to show beginning
• Circles used as connectors
• Diamonds used as decisions
– Combination of control structures
• Stacking
– Placing one after another
• Nesting
– Inserting of one structure into another
• The if structure
– Causes the program to make a selection
– Chooses based on conditional
• Any expression that evaluates to a bool type
• True: perform an action
• False: skip the action
– Single entry/exit point
– Require no semicolon in syntax
true
Grade >= 60 print “Passed”
false
false true
Grade >= 60
• Repetition Structure
– An action is to be repeated
• Continues while statement is true
• Ends when statement is false
– Contain either a line or a body of code
• Must alter conditional
– Endless loop
true
Product <= 1000 Product = 2 * product
false
Fig. 4.6 Pseudocode algorithm that uses counter-controlled repetition to solve the class-
average problem.
Class average is 79
Fig. 4.8 Pseudocode algorithm that uses sentinel-controlled repetition to solve the class-
average problem.
Passed: 9
Failed: 1
Raise Tuition
2002 Prentice Hall.
All rights reserved.
29
Enter result (1=pass, 2=fail): 1 Outline
Enter result (1=pass, 2=fail): 2
Enter result (1=pass, 2=fail): 2
Enter result (1=pass, 2=fail): 2
Enter result (1=pass, 2=fail): 2 Analysis.cs
Enter result (1=pass, 2=fail): 2 Program Output
Enter result (1=pass, 2=fail): 1
Enter result (1=pass, 2=fail): 1
Enter result (1=pass, 2=fail): 1
Enter result (1=pass, 2=fail): 1
Passed: 5
Failed: 5
• Assignment operators
– Can reduce code
• x += 2 is the same as x = x + 2
– Can be done with all the math operators
• ++, -=, *=, /=, and %=
• Increment operator
– Used to add one to the variable
– x++
– Same as x = x + 1
• Decrement operator
– Used to subtract 1 from the variable
– y--
• Pre-increment vs. post-increment
– x++ or x--
• Will perform an action and then add to or subtract one from
the value
– ++x or --x
• Will add to or subtract one from the value and then perform an
action
2002 Prentice Hall. All rights reserved.
33
5 Program Output
5
6
5
6
6 2002 Prentice Hall.
All rights reserved.
35
Collapsed
comment
Collapsed
code
Expanded code
Text property
Fig. 4.20 Windows Form Designer generated code reflecting new property values.
Text property
ASimpleProgram_Load method