Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

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.
1 More Conditionals Instructor: Mainak Chaudhuri
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
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:
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.
Chapter 6 Control Structures.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
UNIT II Decision Making And Branching Decision Making And Looping
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
CS1101: Programming Methodology Aaron Tan.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
1 Methods Instructor: Mainak Chaudhuri
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Conditionals Instructor: Mainak Chaudhuri
1 Operators and Expressions Instructor: Mainak Chaudhuri
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 4: Control Structures SELECTION STATEMENTS.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Chapter 4: Control Structures II
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
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.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
1 Examples of Recursion Instructor: Mainak Chaudhuri
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
1 Conditionals Instructor: Mainak Chaudhuri
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Java Fundamentals 4.
UMBC CMSC 104 – Section 01, Fall 2016
ECE Application Programming
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 5: Control Structures II
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
Examples of class: Using System.in.read ()
CS1100 Computational Engineering
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
The Basics of Recursion
Lecture Notes – Week 2 Lecture-2
Classification of numbers
Perfect squares class identifySquareButLessClever {
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Application: Algorithms
Repetition Statements
Additional control structures
while loop Condition Statement list
Control Structure.
CIS 110: Introduction to Computer Programming
Instructor: Mainak Chaudhuri
Presentation transcript:

Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in More Conditionals Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

Primality testing class primalityTestSlow { public static void main (String arg[]) { int n = 42, d; if (n <= 1) { System.out.println(n + “ is not a prime.”); } else { for (d=2; d<=n/2; d++) { if ((n%d)==0) { break; if (d > n/2) { System.out.println(n + “ is a prime.”); } } }

Primality testing class primalityTestLittleBetter { public static void main (String arg[]) { int n = 42, d; if (n <= 1) { System.out.println(n + “ is not a prime.”); } else { for (d=2; d*d<=n; d++) { if ((n%d)==0) { break; if (d*d > n) { System.out.println(n + “ is a prime.”); } } }

continue Allows you to skip parts of a for or while or do-while loop statements Example (want to print two-digit numbers with both digits odd) for (i=10; i<100; i++) { if ((i%2)==0) { continue; } if (((i/10)%2)==1) { System.out.println(i + “ has odd digits.”);

Perfect numbers class perfectNumber { public static void main (String arg[]) { int n = 24; int d, sigma_n = 1+n; for (d=2; d<=n/2; d++) { if ((n%d) != 0) { continue; } sigma_n += d; if (sigma_n == 2*n) { System.out.println (n + “ is perfect.”);

switch-case An alternative of if-else if-…-else switch (expression) { case constant1: // integer or character statements1 case constant2: statements2 … case constantN: statementsN default: statementsD }

switch-case Same as if (expression==constant1) { statements1 … statementsN statementsD } else if (expression==constant2) { statements3 // continued on next slide

switch-case else if (expression==constant3) { statements3 statements4 … statementsN statementsD } else if (expression==constantN) { else {

switch-case with break switch (expression) { case constant1: statements1 break; case constant2: statements2 … case constantN: statementsN default: statementsD }

switch-case with break Same as if (expression==constant1) { statements1 } else if (expression==constant2) { statements2 … else if (expression==constantN) { statementsN else { statementsD

switch-case: more flavors switch (expression) { case constant1: case constant2: statements1 break; case constant3: statements3 … case constantN: statementsN default: statementsD }

switch-case: more flavors Same as if ((expression==constant1) || (expression==constant2)) { statements1 } else if (expression==constant3) { statements3 … else if (expression==constantN) { statementsN else { statementsD

Classification of numbers class classifyNumbers { public static void main (String arg[]) { int n = 8; switch (n) { case 0 : System.out.println(“Zero!”); break; case 1 : System.out.println(“Smallest positive!”); case 2 : System.out.println(“Smallest prime!”); // continued in next slide

Classification of numbers case 3 : System.out.println(“Smallest odd prime!”); break; case 4 : System.out.println(“Smallest prime squared!”); case 5 : System.out.println(“Number of fingers!”); case 6 : System.out.println(“Smallest perfect!”); // continued in next slide

Classification of numbers case 7 : System.out.println(“North seven stars!”); break; case 8 : System.out.println(“Smallest prime cubed!”); case 9 : System.out.println(“Smallest odd prime squared!”); default : System.out.println(“Not a digit!”); } // end of switch } // end of main } // end of class

More classification class differentClassification { public static void main (String arg[]) { int n = 8; switch (n) { case 2: case 3: case 5: case 7: System.out.println(“Prime!”); break; case 1: case 4: case 9: System.out.println(“Square!”); // continued in next slide

More classification case 6: System.out.println(“Perfect!”); break; System.out.println(“Cube!”); case 0: System.out.println(“Zero the Great!”); default: System.out.println(“Not a digit!”); } // end switch } // end main } // end class

More example of switch class rainbow { public static void main (String arg[]) { char c = ‘V’; switch (c) { case ‘V’ : case ‘v’ : System.out.println (“Violet”); break; case ‘I’ : case ‘i’ : System.out.println (“Indigo”); case ‘B’ : case ‘b’ : System.out.println (“Blue”);

More example of switch case ‘G’ : case ‘g’ : System.out.println (“Green”); break; case ‘Y’: case ‘y’ : System.out.println (“Yellow”); case ‘O’ : case ‘o’ : System.out.println (“Orange”); case ‘R’ : case ‘r’ : System.out.println (“Red”); // continued in next slide

More example of switch default : System.out.println (“You are not in rainbow!”); break; }

Nested loops Loop within loop for (i=0; i<=100; i++) { for (j=0; j<=100; j++) { System.out.println (i+j); } Number of loops is called the depth of the nest The innermost loop executes most frequently The outermost loop executes least You can nest while loops within for loops and vice-versa Loop variables should be different for different loops e.g., i and j in this case (what happens if both are i ?)

Nested loops for (i1=p1; i1<q1; i1++) { // outermost statements1 // can be empty for (i2=p2; i2<q2; i2++) { statements2 // can be empty for (i3=p3; i3<q3; i3++) { statements3 // can be empty … for (iN=pN; iN<qN; iN++) { // innermost statementsN } } // How many times does statementsK execute?

All perfect numbers class allPerfectNumbersUptoOneLakh { public static void main (String arg[]) { int n, d, sigma_n; for (n=2; n<=100000; n++) { sigma_n = 1+n; for (d=2; d<=n/2; d++) { if ((n%d)==0) { sigma_n += d; } if (sigma_n == 2*n) { System.out.println (n + “is perfect.”);

Euclid’s division algorithm A fast way to compute the greatest common divisor of two numbers a and b Algorithm (this is not a program) Divide b by a, assign a to b and the remainder to a; loop until a is zero. The value of b at this point is the gcd. Assumes a is less than b Main observation: gcd (a, b) = gcd (a, b-a)

GCD class gcd { public static void main (String arg[]) { int a = 40, b = 24, r, gcd; if ((a==0) || (b==0)) { gcd = (a > b) ? a : b; } else if (a < b) { while (a!=0) { r = b%a; b = a; a = r; gcd = b; // next slide

GCD else { while (b!=0) { r = a%b; a = b; b = r; } gcd = a; System.out.println(“GCD: ” + gcd);

Announcements Mid-term exam I on 31st August 0930 to 1030 Seating arrangement: Y4, Y5, Y7001 to Y7085: L1 OROS Y7092 to Y7233: L2 ERES Y7234 to Y7388: L16 OROS Y7391 to Y7518: L17 ERES Please take seats 15 minutes before the scheduled time Exam syllabus: up to and including conditionals