Control Structures Lecture 3 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
1 LOOPS. 2 Repetitions while Loops do-while Loops for Loops break and continue.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Control Statements.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Using break and continue
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Chapter 3 Control Statements
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CMT Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 4 Loops.
Java Programming Language Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 05 Title: Decision/Selection Control Structures Reference: MalikFarrell, chap 1, Liang Ch 3.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Control Statements.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
3C Technologies & Information Appliances Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Information  HW1 now available  Group Project (Find your group members)  Start thinking about group projects.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
Control statements Mostafa Abdallah
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 06 Title: Iterative/Repetitive Control Structures Reference: MalikFarrell, chap 1, Liang Ch 4.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Control Statements IT108 George Mason University.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Unit-1 Introduction to Java
Chapter 5 Control Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4 Control structures and Loops
Chapter 5 Control Statements
Alice in Action with Java
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 3 Control Statements
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Program Flow.
Chapter 4: Loops and Iteration
Presentation transcript:

Control Structures Lecture 3 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Contents: Control Structures Decision  Using if and if...else  Nested if Statements  Shortcut if Statements  Using switch Statements Repetition  Looping: for, while, and do  Nested loops  Using break and continue

Decisions if Statements switch Statements Shortcut if Statements

if Statements Format: Format: if (booleanExpression) { statement(s); statement(s);} Example: Example: if ((i >= 0) && (i = 0) && (i <= 10)) System.out.println("i is between 0 and 10"); System.out.println("i is between 0 and 10");

The if...else Statement Format: Format: if (booleanExpression) { statement(s)-for-the-true-case; statement(s)-for-the-true-case;}else{ statement(s)-for-the-false-case; statement(s)-for-the-false-case;}

if...else Example if (radius >= 0) { area = radius*radius*PI; area = radius*radius*PI; System.out.println("The area for the System.out.println("The area for the circle of radius " + radius + " is " + area); circle of radius " + radius + " is " + area);}else{ System.out.println("Negative input"); System.out.println("Negative input");}

Nested if Statements Example 3.1: Using Nested if Statements This program reads in year and loan amount and computes the monthly pay and total pay. The interest rate is determined by year. TestIfElse

Shortcut if Statements if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1;

switch Statements switch (year) { case 7: interestRate = 7.25; case 7: interestRate = 7.25; break; break; case 15: interestRate = 8.50; case 15: interestRate = 8.50; break; break; case 30: interestRate = 9.0; case 30: interestRate = 9.0; break; break; default: System.out.println( default: System.out.println( "Wrong Year, enter 7, 15, or 30"); "Wrong Year, enter 7, 15, or 30");}

switch Statement Flow Chart

Repetitions for Loops while Loops do Loops break and continue

for Loops Format: Format: for (control-variable-initializer; continue-condition; adjustment-statement) continue-condition; adjustment-statement){ //loop body; //loop body;} Example: Example: int i; for (i = 0; i<100; i++) { System.out.println("Welcome to Java!” + i); System.out.println("Welcome to Java!” + i);} // for (int i = 0; i<100; i++) // for (int i = 0; i<100; i++)

for Loop Flow Chart

for Loop Examples Examples for using the for loop: F Example 3.2: TestSum.java TestSum TestMulTable F Example 3.3: TestMulTable.java

while Loops Format: Format: while (continue-condition) { // loop-body; // loop-body;} Example 3.4: TestWhile.java Example 3.4: TestWhile.java TestWhile

while Loop Flow Chart

do Loops Format: Format:do{ //loop body; //loop body; } while (continue-condition)

do Loop Flow Chart

The break Keyword

The continue Keyword

Using break and continue Examples for using the break and continue keywords: F Example 3.5: TestBreak.java F Example 3.6: TestContinue.java TestBreak TestContinue