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
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Advertisements

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.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Slide 1 of 64 Lecture C Lesson 3: Conditions and Loops Unit 1: The if Statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
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.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
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.
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.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
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.
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.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
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.
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.
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.
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.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4 Repetition Statements (loops)
Control Statements: Part 1
Chapter 6 More Conditionals and Loops
JavaScript: Control Statements I
Boolean Expressions and If
Chapter 4 – Control Structures Part 1
JavaScript: Control Statements I
SELECTION STATEMENTS (1)
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.