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.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Java Control Statements
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
8-May-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/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
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.
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:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Condensed Java 19-Apr-17.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The switch Statement, DecimalFormat, and Introduction to Looping
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Controlling Execution Dong Shao, Nanjing Unviersity.
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,
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
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.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
The for loop.
Condensed Java With comparisons to Scala 4-Feb-16.
Application development with Java Lecture 6 Rina Zviel-Girshin.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
The switch Statement, and Introduction to Looping
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecture Sep-18.
Lecture Nov-18.
CIT 590 (basic Java syntax)
Lecture Nov-18.
Sudden Java 27-Nov-18.
(and a review of switch statements)
(and a review of switch statements)
Sudden Java 4-Dec-18.
Condensed Java 7-Dec-18.
3 Control Statements:.
PROGRAM FLOWCHART Iteration Statements.
Additional control structures
Chap 7. Advanced Control Statements in Java
Loops CGS3416 Spring 2019 Lecture 7.
Control Statements:.
Presentation transcript:

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 has the form: if ( condition ) statement-to-execute-if-true ; else statement-to-execute-if-false ; Either statement (or both) may be a compound statement Notice the semicolon after each statement The else part is optional

3 Flowchart for the if-else statement condition? true statement-1 statement-2 false

4 The while loop This is the form of the while loop: while ( condition ) statement ; If the condition is true, the statement is executed, then the whole thing is done again The statement is executed repeatedly until the condition becomes false If the condition starts out false, the statement is never executed at all

5 Flowchart for the while loop condition? statement true false

6 The do-while loop The syntax for the do-while is: do { …any number of statements… } while ( condition ) ; The while loop performs the test first, before executing the statement The do-while statement performs the test afterwards As long as the test is true, the statements in the loop are executed again

7 Flowchart for the do-while loop condition? statement true false

8 The increment operator ++ adds 1 to a variable It can be used as a statement by itself, or within an expression It can be put before or after a variable If before a variable (preincrement), it means to add one to the variable, then use the result If put after a variable (postincrement), it means to use the current value of the variable, then add one to the variable

9 Examples of ++ int a = 5; a++; // a is now 6 int b = 5; ++b; // b is now 6 int c = 5; int d = ++c; // c is 6, d is 6 int e = 5; int f = e++; // e is 6, f is 5 int x = 10; int y = 100; int z = ++x + y++; // x is 11, y is 101, z is 111 Confusing code is bad code, so this is very poor style

10 The decrement operator -- subtracts 1 from a variable It can be used as a statement by itself, or within an expression It can be put before or after a variable If before a variable (predecrement), it means to subtract one from the variable, then use the result If put after a variable (postdecrement), it means to use the current value of the variable, then subtract one from the variable

11 Examples of -- int a = 5; a--; // a is now 4 int b = 5; --b; // b is now 4 int c = 5; int d = --c; // c is 4, d is 4 int e = 5; int f = e--; // e is 4, f is 5 int x = 10; int y = 100; int z = --x + y--; // x is 9, y is 99, z is 109 Confusing code is bad code, so this is very poor style

12 The for loop The for loop is complicated, but very handy Syntax: for ( initialize ; test ; increment ) statement ; Notice that there is no semicolon after the increment Execution: The initialize part is done first and only once The test is performed; as long as it is true, The statement is executed The increment is executed

13 Flowchart for the for loop condition? statements true false increment initialize

14 Parts of the for loop Initialize: In this part you define the loop variable with an assignment statement, or with a declaration and initialization Examples: i = 0 int i = 0 i = 0, j = k + 1 Test, or condition: A boolean condition Just like in the other control statements we have used Increment: An assignment to the loop variable, or an application of ++ or -- to the loop variable

15 Example for loops Print the numbers 1 through 10, and their squares: for (int i = 1; i < 11; i++) { System.out.println(i + " " + (i * i)); } Print the squares of the first 100 integers, ten per line: for (int i = 1; i < 101; i++) { System.out.print(" " + (i * i)); if (i % 10 == 0) System.out.println(); }

16 Example: Multiplication table public static void main(String args[]) { for (int i = 1; i < 11; i++) { for (int j = 1; j < 11; j++) { int product = i * j; if (product < 10) System.out.print(" " + product); else System.out.print(" " + product); } System.out.println(); } }

17 Results

18 When do you use each loop? Use the for loop if you know ahead of time how many times you want to go through the loop Example: Stepping through an array Example: Print a 12-month calendar Use the while loop in almost all other cases Example: Compute the next step in an approximation until you get close enough Use the do-while loop if you must go through the loop at least once before it makes sense to do the test Example: Ask for the password until user gets it right

19 The break statement Inside any loop, the break statement will immediately get you out of the loop If you are in nested loops, break gets you out of the innermost loop It doesn’t make any sense to break out of a loop unconditionally—you should do it only as the result of an if test Example: for (int i = 1; i <= 12; i++) { if (badEgg(i)) break; } break is not the normal way to leave a loop Use it when necessary, but don’t overuse it

20 Multiway decisions The if-else statement chooses one of two statements, based on the value of a boolean expression The switch statement chooses one of several statements, based on the value on an integer ( int, byte, short, or long ) or a char expression In Java 5, the value can also be an enum

21 Syntax of the switch statement The syntax is: switch ( expression ) { case value1 : statements ; break ; case value2 : statements ; break ;...(more cases)... default : statements ; break ; } The expression must yield an integer or a character Each value must be a literal integer or character Notice that colons ( : ) are used as well as semicolons The last statement in every case should be a break; I even like to do this in the last case The default: case handles every value not otherwise handled

22 Flowchart for switch statement expression? statement value

23 Flowchart for switch statement Oops: If you forget a break, one case runs into the next! expression? statement value

24 Example switch statement switch (cardValue) { case 1: System.out.print("Ace"); break; case 11: System.out.print("Jack"); break; case 12: System.out.print("Queen"); break; case 13: System.out.print("King"); break; default: System.out.print(cardValue); break; }

25 The assert statement The purpose of the assert statement is to document something you believe to be true There are two forms of the assert statement: 1. assert booleanExpression ; This statement tests the boolean expression It does nothing if the boolean expression evaluates to true If the boolean expression evaluates to false, this statement throws an AssertionError 2. assert booleanExpression : expression ; This form acts just like the first form In addition, if the boolean expression evaluates to false, the second expression is used as a detail message for the AssertionError The second expression may be of any type except void

26 The End “I think there is a world market for maybe five computers.” —Thomas Watson, Chairman of IBM, 1943 “There is no reason anyone would want a computer in their home.” —Ken Olsen, president/founder of Digital Equipment Corporation, 1977