Making Choices (Chap 3) If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until.

Slides:



Advertisements
Similar presentations
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Advertisements

Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Slide 1 of 64 Lecture C Lesson 3: Conditions and Loops Unit 1: The if Statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Making Choices (Chap 3) If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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.
Unit 3 1 Flow of control H We will talk about: conditional statements  if-then-else  logical and conditional operators  switch repetition statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
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
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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
The for loop.
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
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.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
JavaScript: Control Statements I
Chapter 5: Control Structures II
Repetition-Counter control Loop
JavaScript: Control Statements I
SELECTION STATEMENTS (1)
CSS161: Fundamentals of Computing
The for-loop and Nested loops
3 Control Statements:.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Repetition Statements
Just Enough Java 17-May-19.
Control Statements:.
Presentation transcript:

Making Choices (Chap 3) If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until it has doubled in size.

Expressions and Statements Expression statements are formed by adding a semicolon to the end of certain types of expressions. –An assignment expression is an expression involving an assignment. area = width * height; –A method call expression has no assignment. System.out.println(…);

Blocks Several statements can be grouped into a block using { }. { x = 1; { y = 2; System.out.println(y); } System.out.println(x); }

A block with a declaration { int i = 5 + j; // i is declared in this // block j is from elsewhere … } // end of block, i disappears //can’t access i here

Boolean Expressions Any expression that evaluates to true or false. Relational operators,, >=. Equality operators, ==, !=. For example: int i = 3, j = 4; boolean flag; flag = 5 < 6; flag = (i == j); flag = (j + 2) <= 6;

Logical Operators Operators that take boolean values as operands. x && y - true if x AND y are both true x || y - true if either x OR y are true, or both !x - true if x is false - read NOT x

The if statement if ( BooleanExpression ) Statement BooleanExpr Statement true false

Semicolons and the if statement if (temperature < 32) System.out.println("Warning: Below Freezing!"); System.out.println("It's " + temperature + "degrees"); if (temperature < 32) { System.out.println("Warning Warning Warning!"); System.out.println("Warning: Below Freezing!"); System.out.println("Warning Warning Warning!"); }

Example: Sort three numbers 17 a 6 b 11 c 6 a b 17 c Before After

Pseudocode 1.Place the numbers in boxes a, b, c 2.If the number in a is not larger than the number in b, go to Interchange the numbers in a and b. 4. If the number in b is not larger than the number in c, then go to Interchange the numbers in b and c. 6. If the number in a is not larger than the number in b then go to Interchange the numbers in a and b. 8. Halt

a  first b  second c  third t  a a  b b  t a > b b > c t  a b  c c  t t  a a  b b  t Halt No Yes No

SortInput.java Dissect SortInput.java Hand simulate an execution.

The if-else statement if ( BooleanExpression ) Statement1 else Statement2 BooleanExpr Statement1 true false Statement2

Finding the minimum if (x < y) min = x; else min = y;

A block in an if-else statement if (temperature < 32) { System.out.println("Warning"); System.out.println(32 - temperature + "(F) below Freezing!"); System.out.println("Warning"); } else { System.out.println("It's " + temperature + "degrees fahrenheit."); }

Common Programming Error What is printed by the following if x is 3 and y is 4? if (x < y); System.out.println(x + " is smaller"); if (y < x); System.out.println(y + " is smaller");

Common Programming Error What is printed by the following if temperature is 45? if (temperature < 32) System.out.println("It is now"); System.out.print(32 - temperature); System.out.println(" below freezing."); System.out.println("It's " + temperature + "degrees.");

Nested if-else statements if (ageOfPerson >= 18) if (ageOfPerson < 65) System.out.println("full fare adult"); if (ageOfPerson >= 18 && ageOfPerson < 65) System.out.println("full fare adult");

Nested if-else statements if (temperature < 32) { System.out.println("Warning"); if (temperature < 0) System.out.println((-temperature) + "(F) below zero!"); else System.out.println(32 - temperature + "(F) below Freezing!"); System.out.println("Warning"); else { System.out.println("It is " + temperature); }

Common Programming Error if (18 <= age < 65) … This is not legal syntax.

if-else-if-else-if-else... if (ageOfPerson < 18) System.out.println("child fare"); else { if (ageOfPerson < 65) System.out.println("adult fare"); else System.out.println("senior fare"); }

if-else-if-else-if-else... if (ageOfPerson < 18) System.out.println("child fare"); else if (ageOfPerson < 65) System.out.println("adult fare"); else System.out.println("senior fare");

if-else-if-else-if-else... if (ageOfPerson < 18) System.out.println("child fare"); else if (ageOfPerson < 65) System.out.println("adult fare"); else System.out.println("senior fare");

The dangling else problem if (Expression1) if (Expression2) Statement1 else Statement2 This indentation is misleading.

The dangling else problem if (Expression1) if (Expression2) Statement1 else Statement2 An else always matches the nearest unmatched if.

The dangling else problem if (Expression1){ if (Expression2) Statement1 } else Statement2 Use braces to force the grouping you want.

The while statement while ( BooleanExpression ) Statement BooleanExpr true Statement false Execution enters the loop Continue with the rest of the program

A simple loop class Valentine { public static void main(String[] args) { int howMuch = 0; while (howMuch < 5) { System.out.println("I love you."); howMuch++; }

Average - pseudocode - no while 1. Get a number. 2. If the number is 0 go to step Add the number to the running total. 4. Increment the count of numbers read in. 5. Go back to step Divide the running total by the count of number in order to get the average. 7. Print the average.

Average - pseudocode - using while Get a number. While the number is not 0 do the following: Add the number to the running total. Increment the count of numbers read in. Get a number. (when the loop exits) Divide the running total by the count of number in order to get the average. Print the average.

General form of a while loop InitializationStatement while ( BooleanExpression ) { Statement1 Statement2 … PrepareForNextIteration }

Average.java Dissect Average.java Identify parts of the loop - init, test, nextIter Sentinel controlled loop 0 / 0 is NaN

Common Programming Error How many times does this loop execute? int count = 13; System.out.println("The multiples of " + "13 between 1 and 100 are:"); while (count != 100) { System.out.println(count); count = count + 13; }

The for statement for ( ForInit; BooleanExpr; UpdateExpr ) Statement BooleanExpr true Statement false Execution enters the loop Continue with the rest of the program ForInit

SquareRoots.java Dissect SquareRoots.java Redo loop with a while loop. Modify to declare loop index i inside of the for loop.

Switch Day of week with if-else Day of week with a switch Day of week grouping weekdays and weekends.

A Final Example Write a program to plot an increasing frequency sine wave, given an initial step size in degrees, the number of points to plot, and the rate of increase as a percentage. class PlotSin { public static void main(String[] args) { > while > > }

>  (linesRemaining > 0> >  { > System.out.println('*'); > }

>  // change -1 to 1 into 0 to 78 int numSpaces = Math.sin(Math.toRadians(angle)) * ; while (numSpaces > 0) { System.out.print(' '); } >  linesRemaining--; angle = angel + step; step = step * rate;

>  double step, angle = 0; int pointsRemaining; System.out.println("Enter initial step size."); step = Console.in.readDouble(); System.out.println("Enter number of points."); pointsRemaining = Console.in.readInt(); System.out.println("Enter rate of increase."); rate = Console.in.readDouble(); //Convert to 1 + fractional rate rate = 1 + rate / 100;

import tio.*; class PlotSin { public static void main(String[] args) { //get the step size, number of lines, and rate of increase double step, rate, angle = 0; int pointsRemaining; System.out.println( "Enter the initial step size in degrees."); step = Console.in.readDouble(); System.out.println("Enter the number of points to plot."); pointsRemaining = Console.in.readInt(); System.out.println( "Enter the rate of increase as a percentage."); rate = Console.in.readInt(); rate = (rate / 100);

while (pointsRemaining > 0) { // print some spaces // change -1 to +1 range of sin() into 0 to 78. int numSpaces = (int)Math.round( Math.sin(Math.toRadians(angle)) * ); while (numSpaces > 0) { System.out.print(' '); numSpaces--; } System.out.println('*'); // draw the point // update the variables for the next iteration pointsRemaining--; angle = angle + step; step = step * rate; // incr step => incr frequency }

Enter the initial step size in degrees. 10 Enter the number of points to plot. 40 Enter the rate of increase as a percentage. 0 *

Enter the initial step size in degrees. 10 Enter the number of points to plot. 40 Enter the rate of increase as a percentage. 2 *