1 Conditionals Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

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
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
1 Repetition structures Overview while statement for statement do while statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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
Sorting numbers Arrange a list of n numbers a1, a2, a3,..., an in ascending order. A solution: Using Insertion sort. What is Insertion sort? Insertion.
1 Methods Instructor: Mainak Chaudhuri
Java Programming: From the Ground Up
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 5 Loops.
1 Conditionals Instructor: Mainak Chaudhuri
1 Operators and Expressions Instructor: Mainak Chaudhuri
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
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.
1 Announcements B7 tutorial today in CS103 –In CSE department –Ask at entry desk for direction to CS103.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
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.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
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.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Repetition Statements
Numbers How does computer understand numbers? Knows only two symbols
Department of Computer Science
Instructor: Mainak Chaudhuri
Chapter 5: Control Structures II
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Control Structures II
TK1114 Computer Programming
CSS161: Fundamentals of Computing
Control Statements Loops.
Chapter 2.2 Control Structures (Iteration)
class PrintOnetoTen { public static void main(String args[]) {
if-else if (condition) { statements1 } else { statements2
while while (condition) { statements }
Perfect squares class identifySquareButLessClever {
PROGRAM FLOWCHART Selection Statements.
Control Statements Loops.
Repetition Statements
Presentation transcript:

1 Conditionals Instructor: Mainak Chaudhuri

2 Announcements B1 batch will go for tutorial today –In CS102 (Computer Science department ground floor) –After you enter, take right –Ask people, if you are lost – (right after this class)

3 if statement if (condition) { statements } Nested if if (condition1) { statements1 if (condition2) { statements2 } statements3 }

4 Nested if Sometimes possible to simplify nested if if (condition1) { if (condition2) { statements } Same as if ((condition1) && (condition2)) { statements }

5 Example class exampleIf { public static void main(String arg[]) { int x=10, y, z; if ((x%2)==0) { System.out.println(x + “ is even.”); if ((x%3)==0) { System.out.println(x + “ is a multiple of 6.”); y = x/6; } z = x%6; }

6 if-else if (condition) { statements1 } else { statements2 } if within else if (condition1) { statements1 } else { if (condition2) { statements2 } statements3 }

7 if-else if-else if-…-else if (condition1) { statements1 } else if (condition2) { statements2 } else if (condition3) { statements3 } … else { statementsn }

8 Example class greetings { public static void main(String arg[]) { int hour = 3; if ((hour >= 0) && (hour < 12)) { System.out.println(“Good Morning!”); } else if ((hour >= 12) && (hour < 18)) { System.out.println(“Good Afternoon!”); } else if ((hour >=18) && (hour < 24)) { System.out.println(“Good Evening!”); } else { System.out.println(“Bad time!”); }

9 Conditional assignment if ((x%2)==0) { y = x/2; } else { y = (x+1)/2; } Same as y = ((x%2)==0) ? x/2 : (x+1)/2;

10 Integer part and absolute value class integerPart { public static void main(String arg[]) { double x = -3.7; int ipart; double aval; ipart = ((x >= 0) || ((int)x==x)) ? (int)x : (int)(x-1); aval = (x >= 0) ? x : -x; System.out.println(“Integer part of ” + x + “ is ” + ipart + “. Absolute value is ” + aval + “.”); }

11 Integer part and absolute value class integerPartAlternate { public static void main(String arg[]) { double x = -3.7; int ipart; double aval; ipart = ((x < 0) && ((int)x!=x)) ? (int)(x-1) : (int)x; aval = (x < 0) ? -x : x; System.out.println(“Integer part of ” + x + “ is ” + ipart + “. Absolute value is ” + aval + “.”); }

12 Sorting three numbers class sortThree { public static void main(String arg[]) { int x = 2, y = 5, z = 1; int max, mid, min; if ((x > y) && (x > z)) { max = x; if (y > z) { mid = y; min = z; } else { mid = z; min = y; } // next slide

13 Sorting three numbers else { if (y > z) { max = y; if (x > z) { mid = x; min = z; } else { mid = z; min = x; } else { // the remaining two permutations} } // end else }// end main }// end class

14 Loops Needed in problems that require solving the same subproblem over and over –Computing the sum of the first 100 natural numbers and putting the result in y –Algorithm: y = 1; y = y + 2; y = y + 3; … y = y + 100; –Cannot write 99 such additions: use a loop

15 while while (condition) { statements } Can put anything in “statements” –The entire construct is called a while loop –statements are executed until condition is true –Even before executing it the first time condition is evaluated A while loop may not execute even once

16 Example class justAnExample { public static void main(String arg[]) { int x = 5; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); }

17 Example class justAnExample { public static void main(String arg[]) { int x = 15; int y = 0; while (x < 10) { y--; x++; } System.out.println(y); }

18 Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n = 2; int y = 1; while (n <= 100) { y += n; n++; } System.out.println(“Sum of the first ” + (n- 1) + “ natural numbers is ” + y); }

19 Sum of natural numbers class naturalSumAnotherWay { public static void main(String arg[]) { int n = 99; int m = n+1; int y = 100; while (n > 0) { y += n; n--; } System.out.println(“Sum of the first ” + m + “ natural numbers is ” + y) }

20 Announcements There is a class this Saturday , in L7 Batches B9 and B10 will have labs on Saturday , in computer center Midterm-I is on 5 th September, 0930 to 1030, L1, L2, L3, L4, L5, L8, L9 –Odd row odd seats (OROS)

21 Announcements Seating arrangement (OROS) L1: 98292, Y2, Y3, Y4, Y5, up to Y6096 L2: Y6098 to Y6217 L3: Y6218 to Y6285 L4: Y6286 to Y6365 L5: Y6368 to Y6448 L8: Y6452 to Y6508 L9: Y6510 to Y6552 After arriving at your examination hall, if you fail to find an empty seat, please proceed to L9

22 Integer index class integerIndex { public static void main(String arg[]) { int n = 3; double x = 3.14, y = 1.0; int m = n; if (n < 0) { x = 1/x; m = -n; } while (m > 0) { y *= x; m--; } System.out.println(x + “ to the power ” + n + “ is ” + y); }

23 Positive Decimal to Binary class positiveDecimalToBinary { public static void main(String arg[]) { int n = 34, y=0, polyTerm = 1; if (n < 0) { System.out.println(“Sorry, cannot handle negative integers today!”); } else { while (n > 0) { y += (polyTerm*(n%2)); n /= 2; polyTerm *= 10; } System.out.println(“Required binary: ” + y); }

24 do-while do { statements } while (condition); “statements” execute at least once irrespective of condition

25 for loops for (expression1; condition; expression2) { statements } Same as expression1 while (condition) { statements expression2 }

26 Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n; int y = 1; for (n=2; n <=100; n++) { y += n; } System.out.println(“Sum of the first ” + (n-1) + “ natural numbers is ” + y) }

27 Comma operator in for loop for (expression1a, expression2a, …; condition; expression1b, expression2b,…) { statements } Same as expression1a expression2a … while (condition) { statements expression1b expression2b … }

28 Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n; int y; for (n=2, y=1; n <=100; y += n, n++) { } System.out.println(“Sum of the first ” + (n-1) + “ natural numbers is ” + y) }

29 Empty for loop body for (expression1; condition; expression2) { } Same as for (expression1; condition; expression2);

30 Infinite loops Loops that never terminate while (true) { statements } do { statements } while (true);

31 Infinite loops for (expression1; ;expression2) { statements } for (i=0; i > -10; i++) { statements } for (i=0; i<=100; i--) { statements }

32 Perfect squares class identifySquareButLessClever { public static void main (String arg[]) { int n = 48; int i; if (n < 0) { System.out.println (n + “ is not a perfect square.”); } else if ((n==0) || (n==1)) { System.out.println (n + “ is a perfect square.”); } else { for (i=2; i<=n/2; i++) { if ((i*i) == n) { System.out.println(n + “ is square of ” + i); }

33 break In the last example you may want to come out of the for loop as soon as you discover that n is a square –The computation done after this is useless –Use break –Breaks out of the loop (while, do-while, or for) currently you are in for (i=2; i<=n/2; i++) { if ((i*i)==n) { System.out.println(n + “ is square of ” + i); break; }

34 break Another way to achieve the same effect without using break for (i=2; (i<=n/2) && ((i*i) != n); i++) ; if ((i*i)==n) { System.out.println(n + “ is square of ” + i); }