Lecture 12. 2 Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.

Slides:



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

Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
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.
Lecture 9. Today’s topic Let’s continue to do exercise (Lecture 8) Character data type –Char String class –String.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
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.
CONTROLLING PROGRAM FLOW
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.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
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.
Control Structures RepetitionorIterationorLooping Part I.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
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.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
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.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
Loop Structures.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
3.5- The while Statement The while statement has the following syntax:
Lab5 PROGRAMMING 1 Loop chapter4.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Repetition Statements
Outline Boolean Expressions The if Statement Comparing Data
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Lecture 12

2 Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement1 is executed. If it is false, the statement2 is executed.

3 Review (If-elseif Statement) if ( condition1 ) { statement1; } else if ( condition2 ) { statement2; } else { statement3; } The condition1 and condition2 must be a boolean expression. It must evaluate to either true or false. If the condition1 is true, the statement1 is executed. If the condition1 is false and condition2 is true, the statement2 is executed. Otherwise statement3 is executed

int MAX = 10; int num = 15; if ( num < MAX ) { System.out.println(“num is smaller?”); } else { System.out.println(“num is bigger!!”); } Print as num is bigger!! Example

Today’s topic Repetition statement –while loop Syntax Infinite loop Nested loop –for loop

Repetition Statements (Loops) Repetition statements allow us to execute a statement or a block of statements multiple times Often we call them as loops Like conditional statements, they are controlled by boolean expressions

7 The while Statement A while statement has the following syntax: If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false while ( condition ) { statement; }

Example of while Statement If the condition of a while loop is false initially, the statement is never executed int count = 1; while (count <= 2) { System.out.println (count); count = count + 1; }   3 1 <= 2 T 2 <= 2 T 3 <= 2 F 8 9 Printed out 1 2

9 Infinite Loops Executing the statements in while loop must eventually make the condition false If not, it never gets out of the loop –this is called an infinite loop, which will execute until the user interrupts the program You should always double check the logic of a program and boolean expression to ensure that your loops will terminate Be careful!!!

Infinite Loops An example of an infinite loop: –There is no update for the value of count!! –This loop will continue executing until the user externally interrupts the program int count = 1; while (count <= 25) { System.out.println (count); } Be careful!!!

Nested Loops The body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely Similarly, you can have nested if statements

Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 2) { count2 = 1; while (count2 <= 3) { System.out.println ("Here"); count2 = count2 + 1; } count1 = count1 + 1; } 2 * 3 = 12

Increment/decrement operator When you have counter in loop, you can use increment/decrement operator ++ –e.g. count++;  count = count + 1; –– –e.g. count--;  count = count – 1;

Example int count = 0; double total = 0.0; while (count < 5) { int num = console.readInt(“Enter num > “); total = total + num; count++; } double average = total / count; console.println( “Average is ” + average ); Make it double variable to calculate average of double type

Exercise! Let’s update MyConverter –Change for loop to while loop –When users enter ‘0’, print out a message and exit The converter will ask users to enter one of conversions they want to perform 0. Quit 1. Mile to Kilometer conversion 2. Fahrenheit to Celsius conversion Other numbers: Error messages

public class MyConverter { public static void main(String[] argv) { JFrame frame = new JFrame(“My Calculator"); IOConsole console = new IOConsole(); frame.getContentPane().add(BorderLayout.CENTER, console); frame.setSize(500, 300); frame.setVisible(true); console.println( “********** MENU ***********”); console.println( “ 0. Quit ” ); console.println( “ 1. Distance (Mile->Kilo)” ); console.println( “ 2. Temperature (F->C) ” ); console.println( “******************************”); next page

int option; while( ) { option = console.readInt( “Which conversion ? > “ ); if ( option == 1 ) { } else if ( option == 2 ) { } else { } Write statements to convert Mile to Kilometer Write statements to convert F to C Print out error messages for users ? Sample 1

int option = -1; while( option > 0 ) { option = console.readInt( “Which conversion ? > “ ); if ( option == 1 ) { } else if ( option == 2 ) { } else { } Write statements to convert Mile to Kilometer Write statements to convert F to C Print out error messages for users Sample 1

int option; boolean quit = false; while( quit == false ) { option = console.readInt( “Which conversion ? > “ ); if ( option == 0 ) { console.println( “Thank you for using!!!”); quit = true; } else if ( option == 1 ) { } Sample 2 …