Intro to Java Day 3 / Loops 1. DAY 3 Java Language Specifications Conditional Loops while do for References: JavaNotes7 pdf bookJavaNotes7 pdf book.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
For(int i = 1; i
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Java Programming Practice.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Introduction to Computers and Programming Midterm Review Sana Odeh.
CS 112 Intro to Computer Science II Sami Rollins Fall 2006.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
CS 112 Intro to Computer Science II Sami Rollins Spring 2007.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Shorthand operators.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Selected Topics in Information Technology Programming Language - JAVA Semester 1/2554.
Textbook Problem Java – An Introduction to Problem Solving & Programming, Walter Savitch, pp.219, Problem 08.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Repetition Statements while and do while loops
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
For Loop Tips And Tricks
Chapter 9 Control Structures.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Midterm 2 Review. Stars and why we use nested loops  Matrix problems (stars)  Other problems involving multiple dimensions  Loops within a process.
Unit 5 The Web Book Test. Unit 5 Test The Web Book Test 1. On the bottom of page 46, why is writing web pages not like writing printed documents ?
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Day 26 Arrays Episode 2. Array Lengths To determine the length of an array, use the command: array_Name.length.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Java Course Review.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Mid Term Review Advanced Programming Ananda Gunawardena
Think What will be the output?
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
الحلقات التكرارية وجمل الدوران (loops)
Chapter 5 Repetition.
System.out.println for console output
Alice in Action with Java
Review for Test1.
Computers & Programming Languages
Intro to Nested Looping
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Intro to Nested Looping
Repetition Control Structure
CS110D Programming Language I
Unit 6 part 3 Test Javascript Test.
Building Java Programs
Conditionals.
Intro to Nested Looping
Review for Test2.
Program Flow.
Introduction To Programming Information Technology , 1’st Semester
Building Java Programs
Intro to Nested Looping
Print the following triangle, using nested loops
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Functions That Do Not Return a Value
Looping Structures.
Intro to Programming (in JavaScript)
Presentation transcript:

Intro to Java Day 3 / Loops 1

DAY 3 Java Language Specifications Conditional Loops while do for References: JavaNotes7 pdf bookJavaNotes7 pdf book

WHILE STATEMENT 3 … while ( ) … The is called the “Continuation Condition” … while ( ) … The is called the “Continuation Condition”

WHILE EXAMPLE Java Code: … int i; i = 0; while ( i < 10 ) { System.out.println(i); i = i + 1; } … Java Code: … int i; i = 0; while ( i < 10 ) { System.out.println(i); i = i + 1; } … 4 Output: Output:

WHILE LOOP NESTING Java Code: … int i, j; i = 0; j = 0; while ( i < 10 ) { j = 0; System.out.printf("i=%d:", i ); while ( j < 2 ) { System.out.printf(" j=%d", j); j = j + 1; } System.out.printf(".\n"); i = i + 1; } … Java Code: … int i, j; i = 0; j = 0; while ( i < 10 ) { j = 0; System.out.printf("i=%d:", i ); while ( j < 2 ) { System.out.printf(" j=%d", j); j = j + 1; } System.out.printf(".\n"); i = i + 1; } … 5 Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1. Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1.

DO WHILE STATEMENT 6 … do { } while ( condition ); … int i = 0; do { i = i + 1; } while ( i < 10 ); // What is the value of i … do { } while ( condition ); … int i = 0; do { i = i + 1; } while ( i < 10 ); // What is the value of i …

DO WHILE LOOP NESTING Java Code: … int i, j; i = 0; j = 0; do { j = 0; System.out.printf("i=%d:", i); do { System.out.printf(" j=%d", j); j = j + 1; } while (j < 2); System.out.printf(".\n"); i = i + 1; } while (i < 10); … Java Code: … int i, j; i = 0; j = 0; do { j = 0; System.out.printf("i=%d:", i); do { System.out.printf(" j=%d", j); j = j + 1; } while (j < 2); System.out.printf(".\n"); i = i + 1; } while (i < 10); … 7 Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1. Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1.

FOR STATEMENT 8 … for ( ; ; ){ }; … int myvar = 10; for (int i=0; i<10; i++) { myvar = myvar + i; } // what is the value of myvar ? … for ( ; ; ){ }; … int myvar = 10; for (int i=0; i<10; i++) { myvar = myvar + i; } // what is the value of myvar ?

FOR LOOP NESTING #1 Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=0; j<2; j++ ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=0; j<2; j++ ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … 9 Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1. Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1.

FOR LOOP NESTING #2 Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=3; j>0; j-- ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=3; j>0; j-- ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … 10 Output: i=0: j=3 j=2 j=1. i=1: j=3 j=2 j=1. i=2: j=3 j=2 j=1. i=3: j=3 j=2 j=1. i=4: j=3 j=2 j=1. i=5: j=3 j=2 j=1. i=6: j=3 j=2 j=1. i=7: j=3 j=2 j=1. i=8: j=3 j=2 j=1. i=9: j=3 j=2 j=1. Output: i=0: j=3 j=2 j=1. i=1: j=3 j=2 j=1. i=2: j=3 j=2 j=1. i=3: j=3 j=2 j=1. i=4: j=3 j=2 j=1. i=5: j=3 j=2 j=1. i=6: j=3 j=2 j=1. i=7: j=3 j=2 j=1. i=8: j=3 j=2 j=1. i=9: j=3 j=2 j=1.

ASSIGNMENT Create 1 or 2 programs using while and for loop, that print only the odd numbers between 0 and 100. example: … 99 Advanced: Print an asterisk “*”, next to the numbers that are divisible by 5 example: 1 3 5* 7 9 … 99

LINKS & REFERENCES Intro to Java Language Web Page Oracle Java Language Specifications JavaNotes-7 pdf book linked.pdf linked.pdf 12