+ All Categories
Home > Education > Chapter 4 flow control structures and arrays

Chapter 4 flow control structures and arrays

Date post: 19-May-2015
Category:
Upload: sshhzap
View: 2,382 times
Download: 2 times
Share this document with a friend
Popular Tags:
69
CHAPTER 4 FLOW CONTROL STRUCTURES JESS DALE DELA CRUZ
Transcript
Page 1: Chapter 4 flow control structures and arrays

CHAPTER 4 FLOW CONTROL STRUCTURES

JESS DALE DELA CRUZ

Page 2: Chapter 4 flow control structures and arrays

Method of Problem Solving

1 Recognize and understand the problem.2 Accumulate facts.3 Select appropriate theory.4 Make necessary assumptions.5 Solve the problem.6 Verify results.Peforming step 5 (Solve the problem) may involve

a computer.

Page 3: Chapter 4 flow control structures and arrays

The 5 steps in using a computer as a problem-solving tool

1 Develop an Algorithm and a Flowchart.

2 Write the program in a computer language. (i.e. Fortran, C)

3 Enter the program into the computer.

4 Test and debug the program.

5 Run the program, input data, and get the results from the computer.

Page 4: Chapter 4 flow control structures and arrays

ALGORITHM

• An Algorithm is just a detailed sequence of simple steps that are needed to solve a problem.

Page 5: Chapter 4 flow control structures and arrays

FLOW CHART

• A flowchart is a common type of diagram, that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.

Page 6: Chapter 4 flow control structures and arrays

HISTORY OF FLOW CHART

• The first structured method for documenting process flow, the "flow process chart", was introduced by Frank Gilbreth to members of ASME in 1921 as the presentation “Process Charts—First Steps in Finding the One Best Way”. Gilbreth's tools quickly found their way into industrial engineering curricula. In the early 1930s, an industrial engineer, Allan H. Mogensen began training business people in the use of some of the tools of industrial engineering at his Work Simplification Conferences in Lake Placid, New York.

Page 7: Chapter 4 flow control structures and arrays

EXAMPLES

Page 8: Chapter 4 flow control structures and arrays

FLOW CHART

• Remember a computer is only a problem-solving tool! (one of the many different tools engineers use in solving problems) .

Page 9: Chapter 4 flow control structures and arrays

Basic Symbols

Page 10: Chapter 4 flow control structures and arrays

BASIC SYMBOLS

Page 11: Chapter 4 flow control structures and arrays

MORE EXAMPLES

Page 12: Chapter 4 flow control structures and arrays

MORE EXAMPLES

Page 13: Chapter 4 flow control structures and arrays

MORE EXAMPLES

Page 14: Chapter 4 flow control structures and arrays

MORE EXAMPLES

Page 15: Chapter 4 flow control structures and arrays
Page 16: Chapter 4 flow control structures and arrays
Page 17: Chapter 4 flow control structures and arrays

Initializing Objects with Constructors

• In fact, Java requires a constructor call for every object that is created. Keyword new calls the class's constructor to perform the initialization. The constructor call is indicated by the class name followed by parentheses.

• Scanner input = new Scanner( );

Page 18: Chapter 4 flow control structures and arrays

CONTROL STATEMENTS

• Java has only three kinds of control structures, which from this point forward we refer to as control statements: the sequence statement, selection statements and repetition statements.

Page 19: Chapter 4 flow control structures and arrays

LOOPING STATEMENTS

• Java provides three repetition statements (also called looping statements) that enable programs to perform statements repeatedly as long as a condition (called the loop-continuation condition) remains true. The repetition statements are the while, do...while and for statements. The while and for statements perform the action (or group of actions) in their bodies zero or more times if the loop-continuation condition is initially false, the action (or group of actions) will not execute. The do...while statement performs the action (or group of actions) in its body one or more times.

• The words if, else, switch, while, do and for are Java keywords. Recall that keywords are used to implement various Java features, such as control statements.

Page 20: Chapter 4 flow control structures and arrays

if Single-Selection Statement • Programs use selection statements to choose among alternative courses of action.

For example, suppose that the passing grade on an exam is 60. The pseudocode statement

      If student's grade is greater than or equal to 60            Print "Passed"

• The preceding pseudocode If statement may be written in Java as

if ( studentGrade >= 60 ) System.out.println( "Passed" );

Page 21: Chapter 4 flow control structures and arrays

If …if else

• The if statement is a single-selection statement because it selects or ignores a single action (or, as we will soon see, a single group of actions). The if...else statement is called a double-selection statement because it selects between two different actions (or groups of actions).

Page 22: Chapter 4 flow control structures and arrays
Page 23: Chapter 4 flow control structures and arrays

if...else Double-Selection Statement

• The if...else double-selection statement allows the programmer to specify an action to perform when the condition is true and a different action when the condition is false. For example, the pseudocode statement

      If student's grade is greater than or equal to 60            Print "Passed"      Else            Print "Failed"

The preceding If...Else pseudocode statement can be written in Java as

if ( grade >= 60 ) System.out.println( "Passed" ); else

System.out.println( "Failed" );

Page 24: Chapter 4 flow control structures and arrays

if...else Double-Selection Statement

Walang EndIf SA JAVA OK!!!!!

Page 25: Chapter 4 flow control structures and arrays

Conditional Operator (?:) • Java provides the conditional operator (?:) that can be used in place of an if...else

statement. This is Java's only ternary operator this means that it takes three operands. Together, the operands and the ?: symbol form a conditional expression. The first operand (to the left of the ?) is a boolean expression (i.e., a condition that evaluates to a boolean value true or false), the second operand (between the ? and :) is the value of the conditional expression if the boolean expression is TRUE and the third operand (to the right of the :) is the value of the conditional expression if the boolean expression evaluates to false. For example, the statement

• System.out.println( studentGrade >= 60 ? "Passed" : "Failed" ); •

prints the value of println's conditional-expression argument. The conditional expression in this statement evaluates to the string "Passed" if the boolean expression studentGrade >= 60 is true and evaluates to the string "Failed" if the boolean expression is false. Thus, this statement with the conditional operator performs essentially the same function as the if...else statement shown earlier in this section. The precedence of the conditional operator is low, so the entire conditional expression is normally placed in parentheses. We will see that conditional expressions can be used in some situations where if...else statements cannot.

Page 26: Chapter 4 flow control structures and arrays
Page 27: Chapter 4 flow control structures and arrays

Nested if...else Statements

• A program can test multiple cases by placing if...else statements inside other if...else statements to create nested if...else statements. For example, the following pseudocode represents a nested if...else that prints A for exam grades greater than or equal to 90, B for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other grades:

Page 28: Chapter 4 flow control structures and arrays

If studentGrade is greater than or equal to 90, the first four conditions will be true, but only the statement in the if-part of the first if...else statement will execute. After that statement executes, the else-part of the "outermost" if...else statement is skipped. Most Java programmers prefer to write the preceding if...else statement as

Page 29: Chapter 4 flow control structures and arrays

Dangling-else Problem • The Java compiler always associates an else with the immediately preceding if

unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what is referred to as the dangling-else problem. For example,if ( x > 5 )

if ( y > 5 ) System.out.println( "x and y are > 5" ); else System.out.println( "x is <= 5" );

To force the nested if...else statement to execute as it was originally intended, we must write it as follows:if ( x > 5 ) { if ( y > 5 ) System.out.println( "x and y are > 5" ); } else System.out.println( "x is <= 5" );

Page 30: Chapter 4 flow control structures and arrays

BLOCKS

• The if statement normally expects only one statement in its body. To include several statements in the body of an if (or the body of an else for an if...else statement), enclose the statements in braces ({ and }). A set of statements contained within a pair of braces is called a block. A block can be placed anywhere in a program that a single statement can be placed.

Page 31: Chapter 4 flow control structures and arrays
Page 32: Chapter 4 flow control structures and arrays
Page 33: Chapter 4 flow control structures and arrays
Page 34: Chapter 4 flow control structures and arrays

Switch statement

• The switch statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).

Page 35: Chapter 4 flow control structures and arrays
Page 36: Chapter 4 flow control structures and arrays

EXAMPLES

Page 37: Chapter 4 flow control structures and arrays

Use && for “and”

• The symbol pair && is the way that you spell “and ” in java. Using &&, you can form a larger Boolean expression out of two smaller Boolean expression.

• Syntax: (statement1) && (statement2)• Example:

if ((pressure>min) && (pressure<max))System.out.println(“Pressure is OK.”);

elseSystem.out.println(“Warning: Pressure is

out of range…”);

Page 38: Chapter 4 flow control structures and arrays

Use || for “ or”

• The symbol || is the way that you spell “ or” in java. Using || you can form a larger boolean expression out of two smaller boolean expression.

• Syntax: (statement1)||(statement2)• Example:

if ((salary>expenses) || (savings > expenses)) System.out.println(“Solvent”);

elseSystem.out.println(“Bankrupt”);

Page 39: Chapter 4 flow control structures and arrays

LOGICAL OPERATOR

• AND OPERATOR,&&, is used with two simple expression , the condition is true only if both individual expressions are true by themselves.

• OR OPERATOR ,||, when using the OR operator, the condition is satisfied if either one or both of the two expressions is true.

• NOT OPERATOR,!, is used to change an expression to its opposite state.

Page 40: Chapter 4 flow control structures and arrays
Page 41: Chapter 4 flow control structures and arrays

For loop

• This statement performs the same functions as the while statement, but uses a different form. In many situations, especially those that use a fixed count condition, the for statement format is easier to use than its while statement equivalent.

• for (initializing list;expression;altering list)statement;for ( count = 1; count<10; count = count+1)cout<<count;for (i=5; i<=15;i=i+2)cout<<i;

Page 42: Chapter 4 flow control structures and arrays

LET’S ANALYZE THE C++ PROGRAM WITH RESPECT TO FOR LOOP

Page 43: Chapter 4 flow control structures and arrays

COUNTING STATEMENT

• variable = variable + fixed numbers;

• i = i+1; • count = count + 1;• j = j – 12; • kk = kk + 3; • bullet = bullet + 1000;

EXPRESSION ALTERNATIVE

i = i + 1 i++ or ++i

n = n+1

n++

or

++n

count = count + 1count++

or ++count

Page 44: Chapter 4 flow control structures and arrays

COUNTING STATEMENT

EXPRESSION ALTERNATIVE

i = i - 1 i-- or --i

n = n-1

n--

or

--n

count = count - 1count-- or --count

Page 45: Chapter 4 flow control structures and arrays
Page 46: Chapter 4 flow control structures and arrays
Page 47: Chapter 4 flow control structures and arrays

Good morning. It's 30 minutes past 8 o'clock on April 13, 1997.

Page 48: Chapter 4 flow control structures and arrays
Page 49: Chapter 4 flow control structures and arrays
Page 50: Chapter 4 flow control structures and arrays
Page 51: Chapter 4 flow control structures and arrays
Page 52: Chapter 4 flow control structures and arrays

Actually kahit wala yan tatakbo ang program

Not good process hangga’t maari iwasan

Page 53: Chapter 4 flow control structures and arrays

break statement

• break;

• Forces an immediate break, or exit, from switch, while and the for and do-while statements presented in the next sections.

Page 54: Chapter 4 flow control structures and arrays

Continue statement

• Is similar to break statement but applied only to loops created with while, do-while and for statements.

• continue;

Page 55: Chapter 4 flow control structures and arrays

DIFFRENCE BETWEEN BREAK AND CONTINUE

• break FORCES AN IMMEDIATE EXIT FROM THE LOOP, AS IT DOES IN THE WHILE LOOP.

• THE continue forces control to be passed to the altering list in a for statement, after which the tested expression is reevaluated.

Page 56: Chapter 4 flow control structures and arrays

NOTE

• KEEPING THE LOOP CONTROL STRUCTURE “CLEAN” IS IMPORTANT AND A GOOD PROGRAMMING PRACTICE……

Page 57: Chapter 4 flow control structures and arrays
Page 58: Chapter 4 flow control structures and arrays
Page 59: Chapter 4 flow control structures and arrays
Page 60: Chapter 4 flow control structures and arrays
Page 61: Chapter 4 flow control structures and arrays
Page 62: Chapter 4 flow control structures and arrays
Page 63: Chapter 4 flow control structures and arrays
Page 64: Chapter 4 flow control structures and arrays
Page 65: Chapter 4 flow control structures and arrays
Page 66: Chapter 4 flow control structures and arrays
Page 67: Chapter 4 flow control structures and arrays
Page 68: Chapter 4 flow control structures and arrays
Page 69: Chapter 4 flow control structures and arrays

Recommended