20

Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Embed Size (px)

Citation preview

Page 1: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures
Page 2: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Computing with C#and the .NET Framework

Chapter 3

Software Engineering with

Control Structures

Page 3: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.1 Java relational and equality operators

Operator Symbol

Meaning Example

< less than 31 < 25 is false

<= less than or equal to 464 <= 7213 is true

> greater than -98 > -12 is false

>= greater than or equal to

9 >= 99 is false

== equal to 9 == 12 + 12 is false

!= not equal to 292 != 377 is true

Page 4: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

The if Statement

Type bool Two values, true and falseif (condition)if_true_statement

The condition is a bool expression. If it is true then the if_true_statement will be executed

if (x >2)y = x + 17;

Page 5: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.2 The sequence control flow

Entry

int item1 = 25;

int item2 = 12;

Item2 = item1+15;

Exit

Page 6: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.3 Control flow for the if statement

condition

false

if_true_statement

true

Page 7: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.4 Control Flow for Example 3.2

int hours = int.parse(Console.ReadLine());

Hour

s

>40

Console.WriteLine(

“you worked {0} hours”,

hours);

False

Console.WriteLine(“You “ + “worked overtime this week”)

True

Page 8: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

The if-else statement

Choose between two alternatives

if (condition) if ( x <= 20)

if_true_statement x += 5;

else else

if_false_statement x+=2;

Page 9: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.5 Flow chart for the if-else statement

Condition

if_true_statement if_false_statement

True False

Page 10: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Blocks

Group a sequence of statements inside braces

{

x = 5;

y = -8;

z = x*y;

}

May use a block in an if or if-else statement

Page 11: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.6 Flow chart for if statement with block, step 1

Z <= 10

True

Falsex = 2; y = 7;

Page 12: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.7 Flow chart if statement with block, step 2

Z <= 10

x = 2;

True

False y = 7;

Page 13: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Scientific Notation

Small or large numbers are hard to read

Use exponents instead

3.937E-8 instead of .00000003937

5.88E12 instead of 5,880,000,000,000

E or e

E12 or E+12

Page 14: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Type double

Provides about 16 decimal places

double length = 173.24E5; // exponent

double height 1.83; // fixed

+,-,*,/ operators

Page 15: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Formatted Output

{0:E} Scientific default (three decimal places)

{0:F2} Fixed-point, two decimal places{O:G} General, default (same as {0})

Uses scientific notation if exponent is less than -4 or if the exponent is greater or equal to the number of significant digits in the number

Page 16: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.8 Conversion of mixed-mode expression

2.54

2.54

Original expression

After Conversion

+

+

361

361.0

Page 17: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

while statement

Provides repetition

while (condition)

while_true_statement

while (x < 10)

x += 2;

If x = 5, then x becomes 7, 9 before loop stops

while (x < 10)

x -= 4; //does not terminate if x starts at 5

Page 18: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.9 Flow chart for the while loop

Condition

while_true_statementFalse True

Page 19: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Figure 3.10 Pseudocode for the sum of test scores problem

Read the quantity of scores;

while(count < quantity) {

Read the next score;

Add the score to the total so far;

Increment the count of scores;

}

Display the quantity and the total;

Page 20: Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures

Debugging

Read the code carefully to determine where the error might be located

Add WriteLine statements to get more information

Test thoroughly

Use a debugger if available