CPSC 233 Tutorial 5 February 2 th /3 th, 2015. Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.

Slides:



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

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
ITC 240: Web Application Programming
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
More While Loop Examples CS303E: Elements of Computers and Programming.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
1 CS161 Introduction to Computer Science Topic #8.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
COMP Loop Statements Yi Hong May 21, 2015.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
UCT Department of Computer Science Computer Science 1015F Iteration
PROGRAM CONTROL STRUCTURE
Chapter 6 More Conditionals and Loops
Programming Fundamentals
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 Control structures and Loops
Arrays, For loop While loop Do while loop
Loop Control Structure.
Outline Altering flow of control Boolean expressions
Structured Program Development in C
More Loops.
Java Programming Loops
Coding Concepts (Basics)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
REPETITION STATEMENTS
Loops.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
A LESSON IN LOOPING What is a loop?
JavaScript: Control Statements II
Java Programming Loops
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.
CSCI 1100/1202 February 6, 2002.
Presentation transcript:

CPSC 233 Tutorial 5 February 2 th /3 th, 2015

Java Loop Statements A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is called the body of the loop.

Java Loop Statements The while statement The do-while statement The for Statement

The while Statement

The do-while Statement Similar to a while statement, except that the loop body is executed at least once

The for Statement

Exercise 1 Write a fragment of code that will read words from the keyboard until the word done is entered. For each word except done, report whether its first character is equal to its last character.

Exercise Description Prompt the user to enter a word Read words from the keyboard until the word done is entered For the required loop you can use a a)while statement b)do-while statement Check if the word’s first character is equal to its last character or not

1th Solution using while statement

2th Solution using do-while Statement

Exercise 2 Write a loop that will create a string that is the reverse of a string the user indicates.

Exercise2 Description Prompt the user to enter a string Read String from the keyboard Create a loop that iterates over individual string characters in reverse sequence

Exercise2 Solution

Exercise 3 Write a program that will read from the keyboard until the user enters a positive number. Then it computes the sum of the first n positive odd integers. For example, if n = 5, then you should compute

Exercise3 Description Prompt the user to enter a positive number Read from the keyboard until the user enters a positive number compute the sum of the first n positive odd integers

Exercise3 Description