Perfect squares class identifySquareButLessClever {

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.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
1 More Conditionals Instructor: Mainak Chaudhuri
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
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.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
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
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Chapter 4: Control Structures I
1 Conditionals Instructor: Mainak Chaudhuri
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.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures II
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
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.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
1 Conditionals Instructor: Mainak Chaudhuri
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Chapter 4: Control Structures I
using System; namespace Demo01 { class Program
Chapter 4: Control Structures I
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.
Instructor: Mainak Chaudhuri
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Control Statement Examples
Chapter 5 Control Statements
Chapter 4: Control Structures I
March 29th Odds & Ends CS 239.
The for-loop and Nested loops
TO COMPLETE THE FOLLOWING:
Lec 5.
An Introduction to Java – Part I, language basics
SELECTION STATEMENTS (2)
CS110D Programming Language I
Relational, Logical, and Equality Operators
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
if-else if (condition) { statements1 } else { statements2
Scope of variables class scopeofvars {
Classification of numbers
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
PROGRAM FLOWCHART Selection Statements.
Additional control structures
Control Structure.
Presentation transcript:

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);

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; }

break Another way to achieve the same effect without using break class identifySquare{ 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*i) != n); i++) ; if ((i*i)==n) { System.out.println(n + “ is square of ” + i);

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