The for-loop and Nested loops

Slides:



Advertisements
Similar presentations
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Java Programming: From the Ground Up
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Java Fundamentals 4.
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
Chapter 4 – Loops Dr. Larry G. Thomas – University of Toledo / LCCC
Chapter 5: Control Structures II
Loops.
Chapter 5: Control Structures II
Chapter 6 More Conditionals and Loops
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
Computer Programming Methodology Input and While Loop
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
Java Language Basics.
Control Statements Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Truth tables: Ways to organize results of Boolean expressions.
Building Java Programs
Control Statements Loops.
Repetition Statements
Outline Boolean Expressions The if Statement Comparing Data
Random Numbers while loop
Chapter 3 Flow of Control Loops in Java.
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

The for-loop and Nested loops November 21, 2018 ICS102: For Loop

Outline The for Statement Syntax Semantics of the for Statement Nested Loops continue, break, and exit Statements November 21, 2018 ICS102: For Loop

- The for Statement Syntax for (Initializing; Boolean_Expression; Update) Block Note that the three control expressions are separated by two, not three, semicolons Note that there is no semicolon after the closing parenthesis at the beginning of the loop November 21, 2018 ICS102: For Loop

- The for Statement The for statement is most commonly used to step through an integer variable in equal increments It begins with the keyword for, followed by three expressions in parentheses that describe what to do with one or more controlling variables The first expression tells how the control variable or variables are initialized or declared and initialized before the first iteration The second expression determines when the loop should end, based on the evaluation of a Boolean expression before each iteration The third expression tells how the control variable or variables are updated after each iteration of the loop body November 21, 2018 ICS102: For Loop

- Nested Loops Loops can be nested, just like other Java structures When nested, the inner loop iterates from beginning to end for each single iteration of the outer loop for (Initializing; Boolean_Expression; Update) Block 1 Block 1 can contain other loop statements as follows Block 1  for (Initializing; Boolean_Expression; Update) Block 2 November 21, 2018 ICS102: For Loop

- Nested Loops Loops can be nested, just like other Java structures When nested, the inner loop iterates from beginning to end for each single iteration of the outer loop int rowNum, columnNum; for (rowNum = 1; rowNum <=3; rowNum++) { for (columnNum = 1; columnNum <=2; columnNum++) System.out.print(" row " + rowNum + " column " + columnNum); System.out.println(); } November 21, 2018 ICS102: For Loop

- continue, break, and exit Statements … Class test { public static void main( String [] args) { for (int I = 0; I < 10; i++) { statement 1; statement 2; if( cond) contine; statement 3; statement 4; } statement 5; statement 6; November 21, 2018 ICS102: For Loop

… - continue, break, and exit Statements … Class test { public static void main( String [] args) { for (int I = 0; I < 10; i++) { statement 1; statement 2; if( cond) break; statement 3; statement 4; } statement 5; statement 6; November 21, 2018 ICS102: For Loop

… - continue, break, and exit Statements Class test { public static void main( String [] args) { for (int I = 0; I < 10; i++) { statement 1; statement 2; if( cond) exit; statement 3; statement 4; } statement 5; statement 6; November 21, 2018 ICS102: For Loop

For-loop examples November 21, 2018 ICS102: For Loop

Questions Write a Java program which computes the sum of all the odd numbers between 0 and 100. Write a Java program which reads 20 numbers using a scanner and computes their average. Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the number of even numbers. Assume the input integers are all positive. Use a negative number as a sentinel. November 21, 2018 November 21, 2018 ICS102: while & do-while ICS102: For Loop 11

Q1 Solution Write a Java program which computes the sum of all the odd numbers between 0 and 100. int sum = 0; for( int n = 1; n <= 100; n = n + 2) { sum += n; } System.out.println(“The sum is “ + sum); November 21, 2018 November 21, 2018 ICS102: while & do-while ICS102: For Loop 12

Q2 Solution Write a Java program which reads 20 numbers using a scanner and computes their average. Scanner kb = new Scanner(System.in); double x; double sum = 0; While (int cnt = 0; cnt < 20; cnt++) { System.out.println(“Enter a number”); x = kb.nextDouble(); sum += x; } System.out.println(“The Average is “ + sum/cnt); November 21, 2018 November 21, 2018 ICS102: while & do-while ICS102: For Loop 13

Q3 Solution Scanner kb = new Scanner(System.in); int even_cnt = 0; Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the count of even numbers. Assume the input integers are all positive. Use any negative number as a sentinel. Scanner kb = new Scanner(System.in); int even_cnt = 0; int odd_cnt = 0; int n; For(;;) { n = kb.nextInt(); if (n < 0) break; else if ( mod(n,2) == 0) even_cnt++; else odd_cnt++; } System.out.println(“Even = “ + even_count + “ odd = “ odd_cnt); November 21, 2018 November 21, 2018 ICS102: while & do-while ICS102: For Loop 14

Nested-loop examples November 21, 2018 ICS102: For Loop

Questions 1. Write a java program which gives the following output 22 333 4444 55555 Write a java program which prints all the prime numbers less than 1000. November 21, 2018 ICS102: For Loop

Q1 Solution Write a java program which gives the following output 22 333 4444 for(int k = 1; k <= 5; k++) { For ( int j = 1; j <=k; j++) System.out.print(k); System.out.println(); } November 21, 2018 ICS102: For Loop

Q2 solution Write a java program which prints all the prime numbers less than 1000. int n, j; for(int k = 2; k < 100; k++) { n = 0; j = 2; while(n == 0 && j < k/2) { if (mod(k,j) == 0) n++; j++; } if( n ==0) System.out.println(k); November 21, 2018 ICS102: For Loop

THE END November 21, 2018 ICS102: For Loop