Nested For Loops - PowerPoint PPT Presentation

About This Presentation
Title:

Nested For Loops

Description:

final int VERT = 5; for(int j = 1; j = HORIZ; j )//label horizontal ... System.out.println(); for(int k = 1; k = VERT; k ) System.out.print(k ... – PowerPoint PPT presentation

Number of Views:1555
Avg rating:3.0/5.0
Slides: 12
Provided by: kal772
Category:
Tags: columns | loops | nested | vert

less

Transcript and Presenter's Notes

Title: Nested For Loops


1
Nested For Loops
2
Nested For Loops
  • It is also possible to place a for loop inside
    another for loop.
  • int rows, columns
  •  
  • for (rows1 rowslt5 rows)
  • for (columns1 columnslt10 columns)
  • System.out.print("")
  • System.out.println ()

Outer Loop
Output

Inner Loop
3
Nested For Loops, Example 2
  • //simple loop to print triangle using the
    character
  • //Produces a right triangle with hypoten
    increasing to left
  • public class while_numbers
  • public static void main(String args)
  • int rows, columns
  • for (rows1 rowslt5 rows)
  • for (columns1 columnsltrows columns)
  • System.out.print("")
  • System.out.print("\n")

Output
Outer Loop
Inner Loop
4
Nested For Loops using seconds and minutes
  • Lets write a program in class to count number of
    seconds and minutes in one hour

5
Nested For Loops using seconds and minutes in one
hour
  • //simple loop to print seconds and minutes in one
    hour
  • public class while_numbers
  • public static void main(String args)
  • int seconds,minutes
  • for (minutes0 minutes lt 59 minutes)
  • for (seconds 0 seconds lt 59 seconds )
  • System.out.println(minutes seconds)

6
Nested For Loops using seconds and minutes
  • Lets write a program in class to count number of
    seconds and minutes and hours in one day

7
Nested For Loops using seconds and minutes in one
hour
  • //simple loop to print seconds , minutes and
    hours in one day
  • public class while_numbers
  • public static void main(String args)
  • int seconds,minutes, hours
  • for (hours0 hourslt 23 hours)
  • for (minutes0 minutes lt 59 minutes)
  • for (seconds 0 seconds lt 59 seconds )
  • System.out.println(hours minutes
    seconds)

8
Nested For Loops, Example 3
  • public class Prog1
  • //Produces a right triangle with hypoten
    increasing to right
  • public static void main(String asd)
  • final int MAX 8
  • for(int j 0 j lt MAX j)//
  • for(int k 0 k lt MAX - j k)//draw MAX - j
    blanks
  • System.out.print(' ')
  • for(int k 0 k lt j k)//draw remaining j
    columns as x's
  • System.out.print('x')
  • System.out.println()

9
Nested For Loops, multiplication table
  • public class Program2a
  • //Prelim to producing a multiplication table
  • public static void main(String asd)
  • for(int j 0 j lt 8 j)//label columns
  • for(int k 0 k lt 9 k)
  • System.out.print(k)
  • System.out.println()
  • final int HORIZ 6
  • for(int j 1 j lt HORIZ j)//label
    horizontal
  • System.out.print("\t" j)//tabs every 9
    columns
  • System.out.println()

10
Nested For Loops, multiplication tableexample 2
  • public class Prog2
  • //Produces a multiplication table
  • public static void main(String asd)
  • final int HORIZ 6
  • final int VERT 5
  • for(int j 1 j lt HORIZ j)//label
    horizontal
  • System.out.print("\t" j)//tabs every 9
    columns
  • System.out.println()
  • for(int j 1 j lt HORIZ 1 j)//draw line
  • System.out.print("--------")
  • System.out.println()
  • for(int k 1 k lt VERT k)

11
Good Programming Practices
  • Do not change the value of the counter variable
    in your loop.
  • Do not attempt to manually adjust it to force an
    exit
  • Can cause subtle errors that are difficult to
    catch
  • Instead change your logic
  • Do not put other expressions in the for control
    structure
  • Manipulations of other variables should appear
    before or within the body of the loop depending
    on whether or not you want to repeat them
  • Put a blank line before and after each major
    control structure
  • Try to limit nesting to three levels, if possible
  • More than three levels of indentation can get
    confusing
  • Limit the for control header to one line if
    possible
Write a Comment
User Comments (0)
About PowerShow.com