Download presentation
Presentation is loading. Please wait.
1
1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …
2
2 Fall 2007ACS-1903 Incrementing and Decrementing Variables by 1 number = number + 1 ; number += 1; number = number - 1 ; number -= 1 ; number++; ++number; number --; --number;
3
3 Fall 2007ACS-1903 while loops while Pre-test (expression tested before the iteration) Loop could be executed zero times do-while Post-test (expression tested after the iteration) Loop executed at least once
4
4 Fall 2007ACS-1903 Flowchart for while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed while the expression is true. pre-test
5
5 Fall 2007ACS-1903 UML for while Loop statement(s) [expression false] [expression true]
6
6 Fall 2007ACS-1903 Reading keyboard input until a sentinel is reached countStudents = 0; while (studentNumber != 0) { countStudents++; studentNumber=keyboard.nextInt(); } Sometimes systems are designed to process some data until a “special” value is encountered. ReadUntilSentinel.java
7
7 Fall 2007ACS-1903 Flowchart for do-while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed repeatedly until the expression becomes false. post-test
8
8 Fall 2007ACS-1903 UML for while Loop statement(s) [expression false] [expression true]
9
9 Fall 2007ACS-1903 Summing the digits of a number do { sum +=(number % 10); number=number/10; } while (number!=0); Sometimes a process can be used that can or must execute at least once and so it can be structured with a loop with a post-test. SumDigitsOfInteger.java
10
10 Fall 2007ACS-1903 Example studentNumber=keyboard.nextInt(); // get student numbers until the sentinel of 0 is encountered while (studentNumber != 0) { mark = keyboard.nextDouble(); if (mark < 50) grade = "F"; else if (mark < 60) grade = "D"; else if (mark < 65) grade = "C"; else if (mark < 70) grade = "C+"; else grade = "B"; // fill in the rest of this System.out.println(studentNumber+" "+mark+" "+grade); studentNumber=keyboard.nextInt(); } Read student numbers and marks -until sentinel of 0 for student number -if there is a student number, there will be a mark too -assign grades based on marks What is the flowchart or activity diagram for this? marksAndGrades.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.