Application development with Java Lecture 6 Rina Zviel-Girshin.

Slides:



Advertisements
Similar presentations
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Repetition structures Overview while statement for statement do while statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
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.
© 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.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Rina System development with Java Instructor: Rina Zviel-Girshin Lecture 2.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 4: Control Structures II
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.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control Statements: Part1  if, if…else, switch 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Branching statements.
Chapter 6: Loops.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Chapter 6 More Conditionals and Loops
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Control Statements Paritosh Srivastava.
Chap 7. Advanced Control Statements in Java
Control Statements:.
Presentation transcript:

Application development with Java Lecture 6 Rina Zviel-Girshin

Rina Control Structures and Statements There are several types of statements: Expression statements Declaration statements Iteration statements Selection statements Examples: int number; // declaration statement number = 25; // assignment statement number++; // increment expression statement Control flow statements

Rina Control Flow Statements The control structures affect the flow of control in a program. Control flow statements can be divided into two types:. selection statements selection statements – if – switch iteration statementsiteration statements – for – while – do-while

Rina Selection Statement statement selection?

Rina If Statement Doing something under some condition: if (Boolean_condition) statement; Boolean_condition is a Boolean expression that can have true or false values. If the value is true than statement is performed, otherwise (the value is false) statement is skipped.

Rina If Flow Chart statement If ? true false

Rina Boolean Expression Example if ( x<1 || x%2!=0 ) { System.out.println( “x is not a positive even number!”); } if ( y+2 < x && !(y == 17) ) { System.out.println( “y is not 17 and is smaller than x by more than 2!”); }

Rina If Statement Example class FailTest { public static void main(String[] args) { int grade = Integer.parseInt(args[0]); System.out.println( “ Your grade is: ” + grade); if (grade < 60) System.out.println( “ You failed ” ); }

Rina If.. Else Statement A choice between doing two things: if (Boolean_condition) statement1; else statement2; If the Boolean_condition is true, statement1 is executed; if the condition is false, statement2 is executed.

Rina If..Else Flow Chart statement If..else ? false statement true

Rina If.. Else Example class FailTest { public static void main(String[] args) { int grade=Integer.parseInt(args[0]); System.out.println( “ Your grade is: ” +grade); if (grade < 60) System.out.println( “ You failed ” ); else System.out.println( “ You passed!! ” ); }

Rina Nested If’s if (a != b) if (a > b) System.out.println(a + ” is greater”); else System.out.println(b + ” is greater”); else System.out.println(“They are equal!”);

Rina If & If..Else Statements statement If ? true false statement If..else ? false statement true

Rina Switch Statement The switch statement is a choice between doing several things (usually more then two things). The switch statement evaluates an expression, then attempts to match the result to one of a series of values. Execution transfers to statement list associated with the first value that matches.

Rina Switch Syntax switch(exp){ case value1: statement1; break; … case valueN: statementN; break; default: defaultStatement; break; }

Rina Choice of Execution If the value of exp equals to value1 then statement1 is performed. … If the value of exp equals to valueN then statementN is performed. If the value of exp is different from value1,..., valueN then defaultStatement is performed.

Rina Switch Statement Details exp can be an integer variable or an expression that evaluate to an integer value. statementN can be empty or can be a set of instructions. A default case can be added to the end of the list of cases, and will execute if no other case matches.

Rina The Break Statement The break statement is usually used to terminate the statement list of each case. This causes the control to jump to the end of the switch statement and continue. Note: if the break statement is omitted execution continues (“falls”) to the next case!

Rina Example public class ST { public static void main(String[] args) { // variables int action=Integer.parseInt(args[0]); int a=Integer.parseInt(args[1]); int b=Integer.parseInt(args[2]);

Rina Example switch(action) { case 1: System.out.println(a+"+"+b+"="+add(a,b)); break; case 2: System.out.println(a+"*"+b+"="+mult(a,b)); break; default: System.out.println("Illegal input"); }//end of switch }//end of main

Rina Example // methods static int add(int x,int y) { return x+y; } static int mult(int x,int y) { return x*y; } } //end of class

Rina More Assignment Operators Often we perform an operation on a variable, then store the result back into that variable. Java provides assignment operators that simplify that process. Instead of : sum = sum + value; you can write: sum += value;

Rina Assignment Operators List Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

Rina Right Hand Side Expression The right hand side of an assignment operator can be a complete expression. The entire right-hand expression is evaluated first, then combined with the additional operation. result /= (total-MIN) % n; is equivalent to result = result / ((total-MIN) % n);

Rina The Conditional Operator The conditional operator evaluates a Boolean condition that determines which of two expressions is evaluated. condition ? exp1 : exp2 If condition is true, exp1 is evaluated; if it is false, exp2 is evaluated. The result of the chosen expression is the result of the entire conditional operator.

Rina Conditional Operator Usage The conditional operator is similar to an if-else statement, except that it is an expression that returns a value: int max = (a > b) ? a : b; If a is greater that b, then a is assigned to max ; otherwise, b is assigned to max. The conditional operator is ternary, meaning it requires three operands

Rina Conditional Operator Example System.out.println ("Your change is " + count + ((count == 1) ? "Dime" : "Dimes”)); If count equals 1, "Dime" is printed, otherwise "Dimes" is printed

Rina More Control Flow What if we want to repeat some sequence of statements many times? statement

Rina Iteration Statements Iteration statements are also called loop control structures. A loop is a repetition of certain pieces of the code several times. In Java there are Three types of Loops: for loop, while loop, do loop.

Rina For Statement for( start; limit; step_exp) { statement; } start is a statement that initializes the loop. limit is a Boolean statement that determines when to terminate the loop. It is evaluated before each iteration.

Rina For Statement (Cont.) When limit is true statement is performed, when it is false the loop is terminated. step_exp is an expression that is invoked after each iteration of the loop and is called the step of the loop. The for loop is often used for counting from start to limit by a step size.

Rina For Diagram Limit Condition Start true Statement Step false

Rina For Example class For_Example { public static void main(String[] args) { int fact = 1; for(int k=1; k<5; k++) { fact *= k; System.out.println( “ The factorial of “ + k + “ is: “ + fact); }

Rina While Statement while( Boolean_cond) statement; The value of Boolean_cond can be: –true and than the statement is performed –false and than loop terminates The statement is executed over and over until the boolean_condition becomes false.

Rina While Diagram Statement true false Boolean Condition

Rina While Example class While_Example { public static void main(String[] args) { int sum=0,k=0; while(sum<100) { sum=sum+k; k++; System.out.print( “ the sum of 0 to “ + k + ” is ” + sum); }

Rina Do.. While Statement do { statement; } while (Boolean_cond); First, the statement performed once! Then, the Boolean_cond is checked. If it is true the next iteration of the loop is performed. If it is false, the loop terminates.

Rina Do.. While Diagram Boolean Condition Statement true false

Rina Infinite Loops If Boolean_cond in one of the loop structures is always true then loop will be executed forever - it is ‘unbounded’. Such a loop is called an infinite loop. The infinite loop will execute until the program is interrupted.

Rina Infinite Loop Example // Two infinite loops program Class Infinity { public static void main(String[] args){ int count=0; for( ; ; ) System.out.print( “ Infinity ” ); while(count<10) { System.out.println( “ Another infinite loop ” ); System.out.println( “ The counter is “ +counter); }

Rina Nested Loops Example for (int row = 1; row <= numRows; row++) { for (int column = 1; column <= numColumns; column++) { System.out.print(row * column + “ ); } System.out.println(); }

Rina Break inside a Loop The break statement, (already used within the switch statement), can also be used inside a loop When the break statement is executed, control jumps to the statement after the loop (the condition is not evaluated again)

Rina Continue inside a Loop A similar statement to the break is continue inside loops When the continue statement is executed, control jumps to the end of the loop and the condition is evaluated (possibly entering the loop statements again)

Rina Break and Continue Example for (;;) { int n = args[0]; if (n == 0) break; if (n%2) continue; sum += n; } System.out.print( “The sum of all input even numbers is “ + sum);

Rina Any Questions?