🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Week 5 Nested Structures & Loops.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Nested Control Structures Nested Control Structures • Any control structure can be nested within another • Nested control structures can involve both iteration and selection statements • i.e. a loop can be nested within an if else and an if else can be nested within a loop • The nested structures...

Nested Control Structures Nested Control Structures • Any control structure can be nested within another • Nested control structures can involve both iteration and selection statements • i.e. a loop can be nested within an if else and an if else can be nested within a loop • The nested structures may be as complex as is necessary if nested in a for //Find total of all even numbers between 1 and 100 int total = 0; for(int i = 1; i <= 100; i++) { if(i % 2 == 0) { total = total + i; } } System.out.print("Total: " + total); Dissect the program • for loop uses a control variable called i which starts with an initial value of 1 and passes around the loop each time until it reaches 100 • On each pass around the loop the control variable i is examined in an if structure to determine if it is an even number. • When the statement if(i % 2 == 0)evaluates to True the current value of i is added to total and the total is updated ( i will be an even number) • When the loop terminates (i = 101) the value of total is displayed Dissect the program • Modulus Operator • Java uses an operator known as modulus operator % which returns a whole number remainder after integer division • Examples 23 % 5 = 3 (5 divided into 23 will go 4 times with a remainder of 3) 34 % 7 = 6 10 % 3 = 1 24 % 8 = 0 Dissect the program • Checking for an even number • An even number should have a remainder of 0 when it is divided by 2. (Don’t use ordinary division /) • 12 % 2 = 0 • 24 % 2 = 0 • Checking for an odd number • An odd number should have a remainder of 1 when it is divided by 2 • 13 % 2 = 1 • 23 % 2 = 1 Dissect the program • To check this in an if condition we must use == (comparison operator) to check if a calculation is the same as some result • if(12 % 2 == 0) (12%2 = 0. Condition is checking is 0 the same as 0) • if(num % 2 == 0)(num is value of variable) • if(i % 2 == 0) /*use if and do-while to enter unknown number of exam results and count how many students failed the exam. To stop entering results enter -1*/ int failedExams = 0; do{ System.out.print("Enter an exam result: " ); result = keyboardIn.nextInt(); } if(result >= 0 && result < 40) { failedExams++; } }while(result != -1); System.out.print(failedExams+ "students failed their exams "); Dissect the program • Unknown number of exams results are to be entered, therefore must use while or do while loop • As each result is entered it must be checked to see if it is a fail if(result >= 0 && result < 40) • The number of results that are less than 40 are counted using a counter variable called failedExams which is initialized with a value of 0 before we enter the loop. This variable is incremented by 1 each time failedExams++ if a result is less than 40 Dissect the Program • The user can enter as many results as required but must have some way of exiting the loop. In this example a value of -1 will break the loop • So in other words while the user does NOT enter -1 while(result != -1) the loop will continue to execute • Finally when the loop stops executing the number of failed results will be printed to screen. If no results less than 40 were entered this value will remain as 0 Nested loops • Nested loops consist of an outer loop and one or more inner loops • Each time the outer loop is repeated the inner loop is re-entered and started anew • It is essential that one loop be completely embedded within the other – there can be no overlap • Each loop must be controlled by a different counter variable. for loop • To print one star on the screen we can use a statement as follows System.out.print(“*”); • To print 5 stars on screen REPEAT 5 times DISPLAY one star • Repeat System.out.print(“*”); 5 times. for loop to print 5 stars for(int i=1; i<=5; i++) { System.out.print("*"); } Output ***** Nested for loops • If we want to display the following pattern ***** ***** ***** ***** • We want to print ***** 4 times REPEAT 4 times REPEAT 5 times DISPLAY 1 star for(int i=1; i<=5; i++) { System.out.print("*"); } Repeat this 4 times – so we put it inside another loop for(int outer=1; outer<=4; outer++) { for(int inner=1; inner<=5; inner++) { System.out.print("*"); } System.out.println(); } inner outer Flow chart - nested for outer = 1; outer++; inner++; true outer<=4 inner = 1; inner<=5 false false true System.out.print(“*”); System.out.println(); • Consider printing the numbers 1 to 5 1 2 3 4 5 • REPEAT 5 times DISPLAY current value for(int j = 1; j <= 5; j++) { System.out.print(i + " "); } • To print 12345 12345 12345 REPEAT 3 times REPEAT 5 times DISPLAY current value for(int j = 1; j <= 5; j++) { System.out.print(j + " "); } • Use the following code for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 5; j++) { System.out.print(j +" "); } System.out.println(); } outer loop iterates 3 times inner loop repeats 5 x 3 times • Consider the following pattern 11111 22222 33333 44444 55555 • Use the following code for(int i = 1; i <= 5; i++) { for(int j = 1; j <= 5; j++) { System.out.print(i +" "); } System.out.println(); } outer loop repeats 5 times inner loop repeats 5 x 5 times Example Multiplication tables • To display 12 multiplication tables – each consisting of 12 multiplications 1*1 = 1 1*2 = 2 : 1*12 = 12 2* 1 = 2 2* 2 = 4 : 2*12 = 24 : 12 *1 = 12 12* 2 = 24 : 12*12 = 144 To display 1 set of tables: for(int j = 1; j <= 12; j++) { System.out.print(i +"*" +j +"=" +(i*j) +“ "); } To display 12 by 12 set of tables: for(int i = 1; i <= 12; i++) { for(int j = 1; j < 12; j++) { System.out.print(i +“ * " +j +“ = " +(i*j) +“ "); } System.out.println(); }

Use Quizgecko on...
Browser
Browser