Chapter 4 flow control structures and arrays


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Chapter 4 flow control structures and arrays

  2. 2. Method of Problem Solving <ul><li>1 Recognize and understand the problem. </li></ul><ul><li>2 Accumulate facts. </li></ul><ul><li>3 Select appropriate theory. </li></ul><ul><li>4 Make necessary assumptions. </li></ul><ul><li>5 Solve the problem. </li></ul><ul><li>6 Verify results. </li></ul><ul><li>Peforming step 5 (Solve the problem) may involve a computer. </li></ul>
  3. 3. The 5 steps in using a computer as a problem-solving tool <ul><li>1 Develop an  Algorithm  and a  Flowchart . </li></ul><ul><li>2 Write the program in a computer language. (i.e. Fortran, C) </li></ul><ul><li>3 Enter the program into the computer. </li></ul><ul><li>4 Test and debug the program. </li></ul><ul><li>5 Run the program, input data, and get the results from the computer. </li></ul>
  4. 4. ALGORITHM <ul><li>An  Algorithm  is just a detailed  sequence  of simple  steps  that are needed to solve a problem. </li></ul>
  5. 5. FLOW CHART <ul><li>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. </li></ul>
  6. 6. HISTORY OF FLOW CHART <ul><li>The first structured method for documenting process flow, the &quot;flow process chart&quot;, 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 . </li></ul>
  7. 7. EXAMPLES
  8. 8. FLOW CHART <ul><li>Remember a computer is only a problem-solving  tool ! (one of the many different tools engineers use in solving problems) . </li></ul>
  9. 9. Basic Symbols
  15. 17. Initializing Objects with Constructors <ul><li>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. </li></ul><ul><li>Scanner input = new Scanner( ); </li></ul>
  16. 18. CONTROL STATEMENTS <ul><li>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. </li></ul>
  17. 19. LOOPING STATEMENTS <ul><li>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. </li></ul><ul><li>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. </li></ul>
  18. 20. if Single-Selection Statement <ul><li>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 </li></ul><ul><li>       If student's grade is greater than or equal to 60             Print &quot;Passed&quot; </li></ul><ul><li>The preceding pseudocode If statement may be written in Java as </li></ul><ul><li>if ( studentGrade >= 60 ) System.out.println( &quot;Passed&quot; ); </li></ul>
  19. 21. If …if else <ul><li>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). </li></ul>
  20. 23. if...else Double-Selection Statement <ul><li>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 </li></ul><ul><li>       If student's grade is greater than or equal to 60             Print &quot;Passed&quot;       Else             Print &quot;Failed&quot; </li></ul><ul><li>The preceding If...Else pseudocode statement can be written in Java as </li></ul><ul><li>if ( grade >= 60 ) </li></ul><ul><li>System.out.println( &quot;Passed&quot; ); </li></ul><ul><li>else </li></ul><ul><li>System.out.println( &quot;Failed&quot; ); </li></ul>
  21. 24. if...else Double-Selection Statement Walang EndIf SA JAVA OK!!!!!
  22. 25. Conditional Operator (?:) <ul><li>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 </li></ul><ul><li>System.out.println( studentGrade >= 60 ? &quot;Passed&quot; : &quot;Failed&quot; ); </li></ul><ul><li>prints the value of println's conditional-expression argument. The conditional expression in this statement evaluates to the string &quot;Passed&quot; if the boolean expression studentGrade >= 60 is true and evaluates to the string &quot;Failed&quot; 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. </li></ul>
  23. 27. Nested if...else Statements <ul><li>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: </li></ul>
  24. 28. 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 &quot;outermost&quot; if...else statement is skipped. Most Java programmers prefer to write the preceding if...else statement as
  25. 29. Dangling-else Problem <ul><li>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, </li></ul><ul><li>if ( x > 5 ) </li></ul><ul><li>if ( y > 5 ) </li></ul><ul><li>System.out.println( &quot;x and y are > 5&quot; ); </li></ul><ul><li>else </li></ul><ul><li>System.out.println( &quot;x is <= 5&quot; ); </li></ul><ul><li>To force the nested if...else statement to execute as it was originally intended, we must write it as follows: </li></ul><ul><li>if ( x > 5 ) </li></ul><ul><li>{ if ( y > 5 ) </li></ul><ul><li>System.out.println( &quot;x and y are > 5&quot; ); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li> System.out.println( &quot;x is <= 5&quot; ); </li></ul>
  26. 30. BLOCKS <ul><li>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. </li></ul>
  27. 34. Switch statement <ul><li>The switch statement is called a multiple-selection statement because it selects among many different actions (or groups of actions). </li></ul>
  28. 36. EXAMPLES
  29. 37. Use && for “and” <ul><li>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. </li></ul><ul><li>Syntax: (statement1) && (statement2) </li></ul><ul><li>Example: </li></ul><ul><li>if ((pressure>min) && (pressure<max)) </li></ul><ul><li>System.out.println(“Pressure is OK.”); </li></ul><ul><li>else </li></ul><ul><li>System.out.println(“Warning: Pressure is out of range…”); </li></ul>
  30. 38. Use || for “ or” <ul><li>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. </li></ul><ul><li>Syntax: (statement1)||(statement2) </li></ul><ul><li>Example: </li></ul><ul><li>if ((salary>expenses) || (savings > expenses)) </li></ul><ul><li>System.out.println(“Solvent”); </li></ul><ul><li>else </li></ul><ul><li>System.out.println(“Bankrupt”); </li></ul>
  31. 39. LOGICAL OPERATOR <ul><li>AND OPERATOR, && , is used with two simple expression , the condition is true only if both individual expressions are true by themselves. </li></ul><ul><li>OR OPERATOR , || , when using the OR operator, the condition is satisfied if either one or both of the two expressions is true. </li></ul><ul><li>NOT OPERATOR, ! , is used to change an expression to its opposite state. </li></ul>
  32. 41. For loop <ul><li>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. </li></ul><ul><li>for (initializing list;expression;altering list) </li></ul><ul><li>statement; </li></ul><ul><li>for ( count = 1; count<10; count = count+1) </li></ul><ul><li>cout<<count; </li></ul><ul><li>for (i=5; i<=15;i=i+2) </li></ul><ul><li>cout<<i; </li></ul>
  34. 43. COUNTING STATEMENT <ul><li>variable = variable + fixed numbers; </li></ul><ul><li>i = i+1; </li></ul><ul><li>count = count + 1; </li></ul><ul><li>j = j – 12; </li></ul><ul><li>kk = kk + 3; </li></ul><ul><li>bullet = bullet + 1000; </li></ul>count++ or ++count count = count + 1 n++ or ++n n = n+1 i++ or ++i i = i + 1 ALTERNATIVE EXPRESSION
  35. 44. COUNTING STATEMENT count-- or --count count = count - 1 n-- or --n n = n-1 i-- or --i i = i - 1 ALTERNATIVE EXPRESSION
  36. 47. Good morning. It's 30 minutes past 8 o'clock on April 13, 1997.
  37. 52. Actually kahit wala yan tatakbo ang program Not good process hangga’t maari iwasan
  38. 53. break statement <ul><li>break; </li></ul><ul><li>Forces an immediate break, or exit, from switch, while and the for and do-while statements presented in the next sections. </li></ul>
  39. 54. Continue statement <ul><li>Is similar to break statement but applied only to loops created with while, do-while and for statements. </li></ul><ul><li>continue; </li></ul>
  40. 55. DIFFRENCE BETWEEN BREAK AND CONTINUE <ul><li>break FORCES AN IMMEDIATE EXIT FROM THE LOOP, AS IT DOES IN THE WHILE LOOP. </li></ul><ul><li>THE continue forces control to be passed to the altering list in a for statement, after which the tested expression is reevaluated. </li></ul>