WHILE STATEMENTS. This is the common structure of While statements initialize variables //initialize while (condition) { // test perform calculations.

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Programming Concepts (Part A) Ping Hsu. What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The switch Statement, DecimalFormat, and Introduction to Looping
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Java Programming: From the Ground Up
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the arithmetic and concatenation operators to provide.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Algorithm Design.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Decision Making and Branching (cont.)
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.
Controlling Program Flow with Decision Structures.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements I
Lecture 07 More Repetition Richard Gesick.
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.
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Chapter 6: Repetition Statements
Computing Fundamentals
Computer Science Core Concepts
ICT Programming Lesson 3:
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
PROGRAM FLOWCHART Iteration Statements.
Question 1a) What is printed by the following Java program? int s;
Chap 7. Advanced Control Statements in Java
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

WHILE STATEMENTS

This is the common structure of While statements initialize variables //initialize while (condition) { // test perform calculations and //loop change variables involved in the condition // body }

TRACING WHILE STATEMENT //Compute _ int sum = 0; cntr = 1; while (cntr <= 100) { sum += cntr; // point p cntr++; // point q } System.out.println(sum);

FOR STATEMENT The for statement combines initialization, condition test and update into a single expression. for (initialize counter; test counter; update counter) statement; //one statement inside the loop body

To understand the loop fully, we must analyze the way which the variables change on each iteration through the loop. On the 100 th iteration, cntr is increased to 101, so there is never a 101 st iteration, and we are confident that the sum is computed correctly. Iteration NumberValue of CNTR at point PValue of Sum at point PValue of CNTR at point Q …….…… …

TRACING PROBLEM //Display the sum of the integers between a startingValue // and an endingValue, using a designated increment. int cntr, sum, startingValue, endingValue, increment; startingValue = 10; endingValue = 100; increment = 7; sum = 0; cntr = startingValue; while (cntr <= endingValue){ sum += cntr; cntr += increment; } System.out.println(sum);

COUNTING BACKWARDS PROBLEM Work in BlueJ and turn in. You must use a While statement. Run the counter backward, to display the square roots of the number 25, 20, 15 and 10. Call the variable number.

REWRITE THE FOLLOWING PROBLEM USING A FOR LOOP //Display the sum of the integers between a startingValue // and an endingValue, using a designated increment. int cntr, sum, startingValue, endingValue, increment; startingValue = 10; endingValue = 100; increment = 7; sum = 0; cntr = startingValue; while (cntr <= endingValue){ sum += cntr; cntr += increment; } System.out.println(sum);

This is a rewrite of the program we traced using the chart. //Computer _ int sum = 0; cntr; for (cntr = 1; cntr <= 100; cntr++) sum += cntr; System.out.println(sum);

DECLARING THE LOOP CONTROL VARIABLE IN A FOR LOOP The loop control variables in the examples shown thus far have been declared outside of and above the loop structure. You can declare the variable inside the loop header. for (int r = 1; r <= 10; r ++) System.out.println(r);

IF STATEMENT The if statement is a conditional control statement of a decision statement, which executes a set of statements when a condition is true. if( ) { }

The condition of an if statement is a Boolean expression, which evaluates to either true or false.

IF-ELSE STATEMENT if (some condition){ do stuff; } else { do stuff; } What and does this mean? It means if some condition is true, do stuff 1, and if it is false, do stuff 2.

IF-ELSE IF STATEMENT The if-else if statement is used to decide among three or more actions and looks like this: if (condition) { statement } else if (condition) { statement } else { statement }

The logic used in developing an if-else if statement is important. For example, when testing a range of numbers, if conditions must be properly ordered because statements are executed for the first true condition only and then the program flow continues to the next statement after the if-else if.

Once upon a time in a faraway land, Jack visited his cousin Jill in the country and offered to milk the cow. Here are the instructions Jill gave Jack: Fetch the cow from the fields; Tie her in the stall; Milk her into the bucket; Pour the milk into the bottles; Drive her back into the field; Clean the bucket;

A year later, Jack visited again. In the meantime, Jill had acquired a herd of cows, some red and some black. This time, when jack offered to help, Jill gave him a more complex list of instructions. Herd the cows from the field into the west paddock; while (there are any cows left in the west paddock); fetch a cow from the west paddock: tie her in the stall; if (she is red){ milk her into the red bucket; pour the milk into red bottles; }else{ milk her into the black bucket; pour the milk into black bottles; } put her into the east paddock; } Herd the cows from the east paddock back into the fields; Clean the buckets;

ASSIGNMENT 1. Why does Jill use a while statement in her instructions to Jack? 2. Why does Jill use an if-else statement in her instructions to Jack?

GROUP ASSIGNMENT

Write pseudocode control statements that are similar in style to the farm example for your assigned statement. Also create a flow chart using the white paper. a. If a checker piece is red, then put it on a red square; otherwise, put it on a black square. b. If your shoes are muddy, then take them off and leave them outside the door. c. Pick up all the blocks on the floor and put them into a bag.

ADDITIONAL RELATIONAL OPERATORS OperatorWhat it Means >Greater than >=Greater than or equal to <Less than <=Less than or equal to ==Equal to !=Not equal to

Suppose a = 3c = 10 b = 7d = -20 Then based on the chart: a < b is true a <= b is true a == b is false a != b is true a – b > c + d is true a < b < c is invalid a == b ==c is invalid