1 / 8

Exploring Java For Loops: Examples & Exercises

Learn about Java for loops with syntax, examples, and exercises to enhance your programming skills. Practice tasks included for better understanding.

Download Presentation

Exploring Java For Loops: Examples & Exercises

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Day 2

  2. For loop • The for loop in java uses a local variable initialized in the loop header to iterate through the loop • Syntax: for(initialization; condition; increment) { body }

  3. For loop example for(inti = 0; i < 5; i++) { System.out.println(“i = “ + i); }

  4. output i = 0 i = 1 i = 2 i = 3 i = 4

  5. What’s the output? public class Something { public static void main(String[] args) { System.out.println(“Number\tSquare”); for(inti = 1; i <= 9; i++) { System.out.println(i + “\t\t” + i*i); } } }

  6. output Number Square 1 1 2 4 3 9 4 26 5 25 6 36 7 49 8 64 9 81

  7. What’s it do? public class something { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print(“Please enter a String: “); String str = in.nextLine(); for(inti = 0; i < str.length(); i++) System.out.println(str.charAt(i)); } }

  8. Homework • Pg 260 #3 and #9

More Related