The for Loop Syntax: for ( expression1 ; condition ; expression2 ) statement ; for ( expression1 ; condition ; expression2 ) { statement ; } Same as: expression1.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
Looping Yong Choi School of Business CSU, Bakersfield.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
LAB 10.
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
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Announcements Exam 1 Grades Posted on Blackboard.
Selection (if-then-else)
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Control Statements Loops.
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Announcements Exam 1 Grades Posted on Blackboard.
The for Loop Syntax: Same as: for (expression1;condition; expression2)
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Control Statements Loops.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Welcome back! October 11, 2018.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Announcements Exam 1 Grades Posted on blackboard Average: 83.26
Presentation transcript:

The for Loop Syntax: for ( expression1 ; condition ; expression2 ) statement ; for ( expression1 ; condition ; expression2 ) { statement ; } Same as: expression1 ; while ( condition ) { statement ; expression2 ; }

Example for Loop int numEmployees,curNum; System.out.print(“Enter Number of Employees: “); numEmployees = scan.nextInt(); if (numEmployees > 0) { for (curNum = 0; curNum < numEmployees;curNum++) { System.out.println( “Welcome to CorpLand!” ); }

Looping for Input char letterEntered = ‘A’; String stringE; while (letterEntered != ‘Z’) { System.out.print(“Enter a letter: “); stringE = scan.next(); letterEntered = stringE.charAt(0); }

Looping until Flag boolean noMoreData(); boolean done = false; … while (!done) { … // do a bunch of stuff … if (noMoreData() == true) done = true; } System.out.println(“ We’re all through – thank you!”);

Nested Loops int i, j ; for (i=0; i < 10; i++) { for (j=0; j < 10; j++) { System.out.print(i); System.out.print(j); System.out.print(“ “ ); } System.out.println(); }

The do-while Loop Syntax do statement ; while (condition); do { statement ; } while (condition);

do-while Example char letterEntered; String stringE; do { System.out.print(“Enter a letter: “); stringE = scan.next(); letterEntered = stringE.charAt(0); } while (letterEntered != ‘Z’);

What Is This? while (!stringEntered.equals(“apple”)) { System.out.println( “Enter a red fruit: “); stringEntered = scan.next(); } while (!stringEntered.equals(“banana”)) { System.out.println(“Enter a yellow fruit: “); stringEntered = scan.next(); } while (!stringEntered.equals(“pomegranate”)) { System.out.println(“Enter a random fruit: “); stringEntered = scan.next(); }

What Is This? for (i = 0; i < 10 ; i++) System.out.println( “Interesting, isn’t it?”); while (answer.charAt(0) != ‘D’) { System.out.print( “Enter an answer: “); answer = scan.next(); } while (answer.charAt(0) != ‘D’) { System.out.print( “Enter an answer: “); answer = scan.next(); }

What Is This? void kingsChair(int loopCounter) { int num; for(num = 0; num < loopCounter; num++) System.out.println(“AAAAAAAAAAAAAAAAAAAAAAAAAA”); }