Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Repetition Structures: Nested Loops CSC 1401: Introduction to Programming with Java Week 6 – Lecture 2 Wanda M. Kunkle.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.
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
Fundamentals of C and C++ Programming Control Structures and Functions.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Chapter 4: Loops and Files
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
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.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1. Agenda for loop Short-handed notation How to use break and continue 2.
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,
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 4: Control Structures II
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 9 Repetition.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
JavaScript: Control Statements I
Chapter 4 Repetition Structures
Chapter 5 Repetition.
For Loops October 12, 2017.
Chapter 4 Repetition Structures
Iteration with While You can say that again.
Chapter 9 Control Structures.
Lecture Notes – Week 3 Lecture-2
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Lec 6 Loop Statements Introduction to Computer Programming
Building Java Programs
Looping and Repetition
Control Statements:.
Presentation transcript:

Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle

2 Repetition Structures A repetition structure is used when the same action(s) are to be executed repeatedly. There are several types: A repetition structure is used when the same action(s) are to be executed repeatedly. There are several types: while while for for do-while do-while

3 Repetition Structures while while Usage: Usage: Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true May never execute if the result of the initial test is false May never execute if the result of the initial test is false Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Example: int counter = 0; while (counter < 100) { // Loops as long as counter is less than 100 out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 } Example: int counter = 0; while (counter < 100) { // Loops as long as counter is less than 100 out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 }

4 Repetition Structures for for Usage: Usage: "Does it all", so to speak "Does it all", so to speak Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true May never execute if the result of the initial test is false May never execute if the result of the initial test is false Often used to specify that an action is to be repeated a pre-determined number of times Often used to specify that an action is to be repeated a pre-determined number of times Format: for (LCV Initialization; Condition; LCV Update ) { // LCV stands for “loop control variable.” Statement(s) // Braces are only needed when there are multiple statements } Format: for (LCV Initialization; Condition; LCV Update ) { // LCV stands for “loop control variable.” Statement(s) // Braces are only needed when there are multiple statements } Example: int counter; for (counter = 0; counter < 100; counter++) // Loops as long as counter is less than 100 out.writeln(counter); // Display counter Example: int counter; for (counter = 0; counter < 100; counter++) // Loops as long as counter is less than 100 out.writeln(counter); // Display counter

5 Constants A constant is a variable that must be initialized with a constant expression when it is declared and which cannot be modified thereafter (within the program). A constant is a variable that must be initialized with a constant expression when it is declared and which cannot be modified thereafter (within the program). The syntax used to identify a variable as a constant in Java is: public static final Type Variable = Constant ; The syntax used to identify a variable as a constant in Java is: public static final Type Variable = Constant ; Example: public static final int base = 2; Example: public static final int base = 2; The word public means that there are no restrictions on where the name base can be used The word public means that there are no restrictions on where the name base can be used The word final means that 2 is the final value assigned to base The word final means that 2 is the final value assigned to base We’ll address the meaning of the word static later. We’ll address the meaning of the word static later.

6 Constants Why use constants? Why use constants? A constant is useful when you need to use the same value repeatedly within a program. A constant is useful when you need to use the same value repeatedly within a program. One advantage to using a constant is that if you have to change it, you only have to change it one place in the program. One advantage to using a constant is that if you have to change it, you only have to change it one place in the program.

7 Sample Programs Now let’s look at some sample programs that demonstrate the use of for and while loops and constants: Now let’s look at some sample programs that demonstrate the use of for and while loops and constants: twoPowers.java twoPowers.java twoPowers.java twoPowersWithFor.java twoPowersWithFor.java twoPowersWithFor.java