CHAPTER 4 FLOW CONTROL STRUCTURES
JESS DALE DELA CRUZ
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.
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.
ALGORITHM
• An Algorithm is just a detailed sequence of simple steps that are needed to solve a problem.
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.
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.
EXAMPLES
FLOW CHART
• Remember a computer is only a problem-solving tool! (one of the many different tools engineers use in solving problems) .
Basic Symbols
BASIC SYMBOLS
MORE EXAMPLES
MORE EXAMPLES
MORE EXAMPLES
MORE EXAMPLES
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( );
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.
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.
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" );
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).
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" );
if...else Double-Selection Statement
Walang EndIf SA JAVA OK!!!!!
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.
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:
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
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" );
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.
Switch statement
• The switch statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).
EXAMPLES
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…”);
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”);
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.
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;
LET’S ANALYZE THE C++ PROGRAM WITH RESPECT TO FOR LOOP
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
COUNTING STATEMENT
EXPRESSION ALTERNATIVE
i = i - 1 i-- or --i
n = n-1
n--
or
--n
count = count - 1count-- or --count
Good morning. It's 30 minutes past 8 o'clock on April 13, 1997.
Actually kahit wala yan tatakbo ang program
Not good process hangga’t maari iwasan
break statement
• break;
• Forces an immediate break, or exit, from switch, while and the for and do-while statements presented in the next sections.
Continue statement
• Is similar to break statement but applied only to loops created with while, do-while and for statements.
• continue;
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.
NOTE
• KEEPING THE LOOP CONTROL STRUCTURE “CLEAN” IS IMPORTANT AND A GOOD PROGRAMMING PRACTICE……