Conditionals and Loops

Slides:



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

Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
CS102--Object Oriented Programming
Project Part 3 Putting the pieces together. Modifications on previous parts In part 1 you created a : BankAccount class, with several fields. Increment.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
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
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Lec 6 Logical Operators String comparison. Review – if statement syntax OR.
Lecture 2: Classes and Objects, using Scanner and String.
Chapter 5 Loops.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent
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");
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
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Day Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 2 Clarifications
CSC111 Quick Revision.
Project 1.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Nested Branches Nested set of statements: Example: Federal Income Tax
Repetition-Sentinel,Flag Loop/Do_While
Iteration.
Something about Java Introduction to Problem Solving and Programming 1.
הרצאה 3 אלמנטים בסיסיים בשפה
Java Fix a program that has if Online time for Monday’s Program
Iteration.
Java Language Basics.
Outline Boolean Expressions The if Statement Comparing Data
Sorts on the AP Exam Insertion Sort.
Lec 5 Nested Control Structures
Module 4 Loops.
Lec 4: while loop and do-while loop
Java Fix a program that has if Online time for Monday’s Program
Three Special Structures – Case, Do While, and Do Until
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
Lec 6 Logical Operators String comparison
Indefinite loop variations
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
More on iterations using
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Conditionals and Loops COMP 202 – Tutorial 3 Conditionals and Loops

If, Else if (x == z && z == (y – 16)) { int x = 7; v = 2*x + y; int y = 23; int z = y – 16; int w, v; if (x == y) { w = 2*x; } else w = 2*y; if (x == z && z == (y – 16)) { v = 2*x + y; w = v + 1; } else { w = 2*y + z; v = w – 1; System.out.println(w + “ “ + v); // What comes out?

String name1 = “Neumann”; if (name1.equals(name2)) System.out.println(name1); else if (name2 == name3) System.out.println(name3); else System.out.println(“No match”);

int x = scan.nextInt(); int y; if (x >0) if (x > 0) y = 1; y = 1; if (x < 0) else if (x < 0) y = -1; y = -1; if (x == 0) else if (x == 0) y = 0; y = 0;

A complicated example int x = 6; int y = 7; int z = 22; int w = 22; int v = 0; if (x == y || z == w) if (y == w) v = z + x; else if (y == w) // else belongs to second if v = z – x; // an else corresponds to the last if before it.

A complicated example int x = 6; int y = 7; int z = 22; int w = 22; int v = 0; if (x == y || z == w) { if (y == w) v = z + x; } else if (y == w) // NOW else belongs to first if v = z – x;

The Conditional Statement int x = scan.nextInt(); int y = scan.nextInt(); int z = (x <= y) ? x+2 : y+2;

for int i, j; for (i = 0; i < 27; i++) j = i^2; int j = 23; for (int i = 0; i < j; i++) System.out.println(i);

Nested for Loop int x,y; for (x = 0; x != 3; x++) { for (y = 0; y != 3; y++) System.out.println("Coordinate "+x+","+y); }

while int i,j; int j = 23; i = 0; int i = 0; while (i<27) { j = i^2; i++; } int j = 23; int i = 0; while (i < j) { System.out.println(i); i++; }

do while int i,j; int j = 23; i = 0; int i = 0; do do { { // Will happen at least once int j = 23; int i = 0; do { System.out.println(i); i++ } while (i < j);

Torment A user is required to pay $230 through a program. Use any loops/conditionals so that the user enters an amount. Then check if the amount is correct, and ask the user to modify it if not.

Answer – for Scanner scan = new Scanner(System.in); final int AMOUNT = 230; // turned to constant int payment = 0; int modification = 0; for (/*empty field*/ ; payment != AMOUNT ; payment+=modification) { System.out.println("Payment given so far: " + payment); System.out.println("Please enter your payment/deduction:"); if (payment < AMOUNT) modification = scan.nextInt(); // If the user over-payed, the next modification will be a deduction. else modification = - scan.nextInt(); }

Answer – do-while Scanner scan = new Scanner(System.in); final int AMOUNT = 230; // turned to constant int payment = 0; int modification = 0; do { System.out.println("Please enter your payment/deduction:"); if (payment < AMOUNT) payment += scan.nextInt(); else payment -= scan.nextInt(); System.out.println("Payment given so far: " payment); } while (payment != AMOUNT);