1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Fundamentals of Python: From First Programs Through Data Structures
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Loops and Iteration for Statements, while Statements and do-while 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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
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 Repetition Statements (loops)
While loop statement condition list
Chapter 4: Control Structures I
Chapter 4 - Program Control
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Control Structures II
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4: Control Structures I
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
import becker.robots.City;
Control Structure Chapter 3.
CIS 110: Introduction to Computer Programming
Chapter 4 - Control Structures: Part 1
Repetition Statements
Branching Statement Condition Statement Condition list 1 list
while loop Condition Statement list
Loops and Iteration CS 21a: Introduction to Computing I
Control Structure.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !

1/30/2016IT 2752 Logical Operators (true || false) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x = 5) (((x % 2) == 0) && ((x % 3) == 0)) || && ! Assume x = 10 true false

1/30/2016IT 2753 De Morgan’s law I am not a female student.  I am not female or I am not a student. I will not be in my office or in the lab.  I will not be in my office and will not be in the lab. !(A && B) is same as !A || !B !(A || B) is same as !A && !B

1/30/2016IT 2754 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F

1/30/2016IT 2755 The Syntax of if and if-else statements if ( condition ) { statement list; } if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if-else statement. Reserved words A Boolean expression (logical expression). In Java, there are two possible values: true and false. A reserved word can’t be used as an identifier

1/30/2016IT 2756 Nested if/else statement if ( condition 1 ) { if ( condition 2 ) { statement list; } else { statement list; }; statement list; } else { statement list; } Indentation indicates the level of statements.

1/30/2016IT 2757 Disney Roller Coaster Ride Ride the roller coaster Give a Mickey Measure the kid’s height, h h < 4 Next Stop No Boy or Girl? Give a Mini Yes boygirl

1/30/2016IT 2758 Disney Ride in Program – OK n = Integer.parseInt( JOptionPane.showInputDialog(“How tall is the kid?”)); // IsBoy = true or false; if (h < 4) if (IsBoy) System.out.println(“Take a Mickey”); else System.out.println(“Take a Mini.”); else System.out.println(“You can ride!!”); System.out.println(“Go to the next stop.”); but not really a good idea; put { } to improve readability

1/30/2016IT 2759 Ambiguity I saw the little girl with a binocular on the mountain top. I saw the little girl with a binocular on the mountain top. I saw the little girl with a binocular on the mountain top. I saw the little girl with a binocular on the mountain top.

1/30/2016IT Rule: else belongs to the nearest if that not yet has an else if (condition1) if (condition2) {.....; } else // not a kid {.....; } Indentation can’t help compiler

1/30/2016IT Boy Scout: Give a badge Is a kid? Welcome No Is a boy? Yes No Give a guidebook Yes No Parent?

1/30/2016IT Boy Scout: in program if (IsKid) { if (IsBoy) { System.out.println(“Take a Badge”); } else // not a kid { if (IsParent) { System.out.println(“Take a Guidebook”); } System.out.println(“Welcome, everybody!.”);

1/30/2016IT Boy Scout: wrong program if (IsKid) if (IsBoy) System.out.println(“Take a Badge”); else // not a kid if (IsParent) System.out.println(“Take a Guidebook”); System.out.println(“Welcome, everybody!.”); if (IsKid) { if (IsBoy) { System.out.println(“Take a Badge”); } else // not a boy { if (IsParent) System.out.println(“Take a Guidebook”); } System.out.println(“Welcome, everybody!.”);

1/30/2016IT Other forms of Nested if/else statements (Cascaded) I if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; if (condition_1) statement_1; if (condition_2) statement_2; if (condition_3) statement_3; if (condition_4) statement_4; if (condition_5) statement_5; V.S.

1/30/2016IT Another form of Nested if/else statement (Cascaded) PointsGrade A 500 – 599B 400 – 499C 300 – 399D 0—299F if (points >= 600) System.out.println( “ A ” ); else if (points >= 500) System.out.println( “ B ” ); else if (points >= 400) System.out.println( “ C ” ); else if (points >= 300) System.out.println( “ D ” ); else System.out.println( “ F ” ); if (points >= 600) System.out.println( “ A ” ); if (points >= 500) System.out.println( “ B ” ); if (points >= 400) System.out.println( “ C ” ); if (points >= 300) System.out.println( “ D ” ); if (points < 300) System.out.println( “ F ” );

1/30/2016IT Other forms of Nested if/else statements (Cascaded) II if (condition_1) if (condition_2) if (condition_3) if (condition_4) statement_1; else statement_2; if ( condition_1 && condition_2 && condition_3 && condition_4 ) statement_1; else statement_2; =?=?

1/30/2016IT Switch vs. Cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; }

1/30/2016IT Another form of Nested if/else statement (Cascaded) PointsGrade A 500 – 599B 400 – 499C D F int g = (int) (point/100); switch (g) { case 6: System.out.println( “ A ” ); break; case 5: System.out.println( “ B ” ); break; case 4: System.out.println( “ C ” ); break; case 3: System.out.println( “ D ” ); break; default: System.out.println( “ F ” ); }

1/30/2016IT Switch vs. Cascaded if/else if (i == 1) statement_1; else if (i == 2) { statement_2; statement_3; } else if (i == 3) statement_3; else if (i == 4) { statement_4; statement_5 statement_6 } else if (i == 5) { statement_5; statement_6; } else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; case 3: statement_3; break; case 4: statement_4; case 5: statement_5; case 6: statement_6; }

1/30/2016IT Definite Loops vs indefinite loops isA definite loop is a loop that number of iterations is known before the loop begins, at least the upper bound is known. I.e., repeat the loop 100 times. is notAn indefinite loop is a loop that number of iterations is not known before the loop begins, at least the upper bound is known. I.e., repeat the loop until some condition reached. Precisely speaking, there is no definite loop in Java.

1/30/2016IT while loop Condition Statement list T F while (Condition) { Statement list } for loop Statement list < n = n for (???;???;???) { Statement list } repeat n times

1/30/2016IT The general format for a for loop for (Initialization_action; Condition; Condition_update) { statement_list; } int n = Integer.parseInt( JOptionPane.showInputDialog( “ input n ” )); int sum=0; for (i=0; i<=n; i++) { sum = sum + i; } System.out.println(“This answer is ” + sum); 12 3 Summation (n-1)+ n

1/30/2016IT for (Initialization_action; Condition; Condition_update) { statement_list; } int n,f=1; n = Integer.parseInt( JOptionPane.showInputDialog( “ input n ” ) ); if (n < 0) System.out.println(“No can do!!”); else for (i=2; i<=n; i++) { f = f*i; } System.out.println(“This answer is ” + f); 12 3 Factorial of n is n  (n-1)  (n-2) ...2  1

1/30/2016IT while Condition Statement list T F while (Condition) { Statement list } while (Condition) ; { Statement list }

1/30/2016IT Compare for and while int i; for (i=2; i<=n; i++) { f *= i; } i=2; while (i<=n) { f *= i; i++; } int i; for (i=0; i<n; i++) {.... } i=0; while (i<n) {.... i++;.... }

1/30/2016IT Another form of loop: Condition Statement list T F do loop do { Statement list } while (condition); This is a common patten E.g., IT CS student has to take IT168 until he/she gets a C or better. don’t forget “;” be careful

1/30/2016IT Another example of using do-loop String answer; do { answer = JOptionPane.showInputDialog( “ y or n? ” ); } while (!answer.equals( “ y ” ) && !answer.equals( “ n ” )); Ask the user to input y (yes) or n (no), where y and n are the only two valid inputs for the program to proceed. This way, we avoid a great chance of accidentally pressing the keyboard. String answer; answer = “ x ” ; while (!answer.equals( “ y ” ) && !answer.equals( “ n ” )) { answer = JOptionPane.showInputDialog( “ y or n? ” ); }

1/30/2016IT Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. is 893 a prime? Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ?. Is (893 % 892 == 0) ? This test is silly but can do the job! We can write a loop to test these.

1/30/2016IT Primality Test int p,r,i; // r: remainder, i: factor i=1; do { i++; r = p % i; // r is the remainder of p divided by i } while (i < p && r != 0); if (i == p) System.out.println( “ a prime number. ” ); else { System.out.println( “ not a prime number. ” ); System.out.println( “ The least factor is ” +i); } do { p = Integer.parseInt( JOptionPane.showInputDialog( “ input an number:)); } while (p <= 1);

1/30/2016IT Break a loop int i = 2; do { r = p % i; if (r == 0) break; i++; } while (i < p); The break statement in a loop will force the program to jump out of the loop immediately. int i = 2; do { r = p % i; i++; } while (i < p && r != 0);

1/30/2016IT Break a loop import javax.swing.JOptionPane;.... public static void marryAsk() { String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (answer.equals("Y") || answer.equals("y")) answer = JOptionPane.showInputDialog( "Really? (y/n)"); } while (!answer.equals("Y") && !answer.equals("y")); JOptionPane.showMessageDialog(null, "Great!!"); } if (answer.equals("F") || answer.equals("f"))break;

1/30/2016IT break and continue The break statement in a for/while loop will force the program to jump out of the for/while loop immediately. The continue statement in a for/while loop will force the program to update the loop condition and then check the condition immediately. for (Initialization_action; Condition; Condition_update) { statement_list; }

1/30/2016IT The Loops with multiple breaking points.... while (......) {.... if (....) break;.... if (....) break;.... if (....) break;.... if (....) break;..... }.... Condition Statement list T F Spaghetti code? Why spaghetti code is bad? Well, this is not spaghetti code!

1/30/2016IT The Loops with multiple breaking points This is Spaghetti code? This is not spaghetti code! All paths come to the same point Multiple-break-loop does not cause the trouble as the spaghetti code does

35 Euclid Algorithm Public int GCD(int a, int b) { int r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return (b); } 1/30/2016IT 275

My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2; if (a < 0) return -1; while (a - st*st != 0) { if (st*st > a) up = st; else down = st; st = (up+down)/2; } return st; } 1.Where can I improve the program? 2.Where is the potential problem? up down 0 a guess st real square root 1/30/201636IT 275

My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2,est; if (a < 0) return -1; est = st*st; `while (a - est > ) { if (est > a) up = st; else down = st; st = (up+down)/2; est = st*st; } return st; } Any problem? What is it? 1/30/201637IT 275

My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2,est; if (a < 0) return -1; est = st*st; while ((a - est > )||(a - est < )) { if (est > a) up = st; else down = st; st = (up+down)/2; est = st*st; } return st; } Still wrong!! What is the roblem? 1/30/201638IT 275

My own square root function (my_sqrt) double my_sqrt(double a) { double up,down,st,est; if (a < 0) return -1; if ( a >= 1) { up=a; down=0;} else { up=1; down=a;} st = (up+down)/2; est = st*st; while ((a - est > ) || (a - est < )) { if (est > a) up = st; else down = st; st = (up+down)/2; est = st*st; } return st; } 1/30/201639IT 275

1/30/2016IT Nested loops (loop in loop) int a = Integer.parseInt( JOptionPane.showInputDialog(“input a:”); int b = Integer.parseInt( JOptionPane.showInputDialog(“input b:”); for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { System.out.print(“*”); } System.out.print(“\n”); } ************* b a

1/30/2016IT Nested loops (2) int a = Integer.parseInt( JOptionPane.showInputDialog(“input a:”); int b = Integer.parseInt( JOptionPane.showInputDialog(“input b:”); for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j > i) break; System.out.print(“*”); } System.out.print(“\n”); } * ** *** **** b a

1/30/2016IT Java Blocks In Java and other C-like languages: a compound statement using { and } A compound statement also serves as a block: while (i < 0) { int c = i*i*i; p += c; q += c; i -= step; } for (int i=0;i<0; i++) { int c = i*i*i; p += c; q += c; i -= step; }

1/30/2016IT Can we do this? for (int i=0; i<n; i++) {.... } int i=2; Scope of red i {}{} Scope of blue i Yes!

1/30/2016IT Can we do this? for (int i=0; i<n; i++) {.... } i=2; Scope of red i {}{} blue i is undefined No!

1/30/2016IT Can we do this? for (int i=0; i<n; i++) {.... } int i=2; {}{} Scope of blue i No!