AP COMPUTER SCIENCE A

Similar documents
For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CMPS 11 Introduction to Programming Midterm 1 Review Problems

1. Find the output of following java program. class MainClass { public static void main (String arg[])

JAVA OPERATORS GENERAL

Operators Questions

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Selenium Class 9 - Java Operators

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Midterm Examination (MTA)

H212 Introduction to Software Systems Honors

int x = 42, y = 33; //1 int z = x + y; //2 System.out.println(x + "+" + y + "=" + z); //3

Lesson 7 Part 2 Flags

Tutorial 06. Conditional statement: if then, if else, switch

6 COMPUTER PROGRAMMING

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

CSIS 10A Assignment 4 SOLUTIONS

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

AP CS Unit 3: Control Structures Notes

Programming Basics. Digital Urban Visualization. People as Flows. ia

Lara Technologies Special-Six Test

Chapter 3. Selections

CS 101 Spring 2007 Midterm 2 Name: ID:

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Introduction to Computer Science Unit 2. Exercises

Loops. CSE 114, Computer Science 1 Stony Brook University

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

CSC 1051 Data Structures and Algorithms I

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

1 Short Answer (10 Points Each)

Key Java Simple Data Types

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

CS141 Programming Assignment #6

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CSE 114 Computer Science I

Computational Expression

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

Decisions (If Statements) And Boolean Expressions

Bjarne Stroustrup. creator of C++

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Java Simple Data Types

CONDITIONAL EXECUTION

Loops and Expression Types

Handout 4 Conditionals. Boolean Expressions.

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

1 Short Answer (10 Points Each)

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

CMP 326 Midterm Fall 2015

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Some Practice Midterm Problems

DM503 Programming B. Peter Schneider-Kamp.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Last Class. While loops Infinite loops Loop counters Iterations

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

CSE 142 Sample Midterm Exam #3

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Assignment-1 Final Code. Student.java

Day 2 : Intermediate Concepts 1 Examples

AP Computer Science A Unit 2. Exercises

Software Practice 1 Basic Grammar

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Introduction to Computer Science Unit 2. Notes

King Saud University College of Computer and Information Sciences Computer Science Department

Ch. 6. User-Defined Methods

CS Week 5. Jim Williams, PhD

Recitation: Loop Jul 7, 2008

Question: Total Points: Score:

Oct Decision Structures cont d

Give one example where you might wish to use a three dimensional array

Conditional Programming

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Prof. Navrati Saxena TA: Rochak Sachan

Loops. GEEN163 Introduction to Computer Programming

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Top-Down Program Development

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Java Programming Language. 0 A history

STUDENT LESSON A12 Iterations

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Question 1 [20 points]

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

Assignment 2.4: Loops

CS 101 Exam 2 Spring Id Name

Repetition, Looping. While Loop

1 Short Answer (15 Points Each)

Transcription:

AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1

More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks 2

More operators public class MoreOperators { public static void main(string[] args) { int a = 19; int b = 4; boolean c = true; System.out.println(!c); System.out.println(a!=b); System.out.println(a%b); false true 3 http://apcs.cold.rocks 3

if if(condition) { //BLOCK 1 The condition should be boolean If the condition is true, the block (BLOCK 1) is executed If the condition is false, nothing happens http://apcs.cold.rocks 4

if public class ControlFlow { public static void main(string[] args) { if(true) { System.out.println( red ); if(false) { red System.out.println( blue ); http://apcs.cold.rocks 5

if else if(condition) { //BLOCK 1 else { //BLOCK 2 If the condition is false, else block (BLOCK 2) is executed else should come right after if block http://apcs.cold.rocks 6

if else public class ControlFlow { public static void main(string[] args) { int a=3; int b=5; if(a>b) { else { System.out.print(a); System.out.print( is greater than ); System.out.println(b); 3 is not greater than 5 System.out.print(a); System.out.print( is not greater than ); System.out.println(b); http://apcs.cold.rocks 7

else if if(condition1) { //BLOCK 1 else if(condition2) { //BLOCK 2 else { //BLOCK 3 If CONDITION1 is true, BLOCK1 is executed If CONDITION1 is false, we check CONDITION2 If CONDITION2 is true, BLOCK2. If not, BLOCK3. http://apcs.cold.rocks 8

if else if else if else if else if(condition1) { //BLOCK 1 else if(condition2) { //BLOCK 2 also possible. else if(condition3) { //BLOCK 3 else if(condition4) { //BLOCK 4 else if(condition5) { //BLOCK 5 else if(condition6) { //BLOCK 6 else if(condition7) { //BLOCK 7 else if(condition8) { //BLOCK 8 else { //BLOCK 9 http://apcs.cold.rocks 9

if else if else if else if else public class ControlFlow { public static void main(string[] args) { int score=85; if(score>=90) { System.out.println( You ve got an A ); else if(score>=80) { System.out.println( You ve got a B ); else if(score>=70) { System.out.println( You ve got a C ); else if(score>=60) { System.out.println( You ve got a D ); else { System.out.println( You are F ); You ve got a B http://apcs.cold.rocks 10

Your turn Print what you are supposed to be doing, from the variable time For example, time = 19; > You should be in AP Computer Science class now http://apcs.cold.rocks 11

Input <OPTIONAL> import java.util.scanner; public class ControlFlow { public static void main(string[] args) { Scanner sc = new Scanner(System.in); System.out.println( What s your name? ); String s = sc.nextline(); What s your name? Teemo Good evening, Teemo System.out.print( Good evening, ); System.out.println(s); http://apcs.cold.rocks 12

if else if else if else with input <OPTIONAL> import java.util.scanner; public class ControlFlow { public static void main(string[] args) { Scanner sc = new Scanner(System.in); System.out.println( What s your score? ); int score = sc.nextint(); if(score>=90) { System.out.println( You ve got an A ); else if(score>=80) { System.out.println( You ve got a B ); else if(score>=70) { System.out.println( You ve got a C ); else if(score>=60) { System.out.println( You ve got a D ); else { System.out.println( You are F ); What s your score? 30 You are F http://apcs.cold.rocks 13

while while(condition) { //BLOCK 1 The condition should be boolean Looks just like if BLOCK 1 is repeated if the condition is true Nothing happens if the condition is false Stops when the condition becomes false, or break is executed http://apcs.cold.rocks 14

while public class ControlFlow { public static void main(string[] args) { while(true) { System.out.println( ); http://apcs.cold.rocks 15

while public class ControlFlow { public static void main(string[] args) { int a=1; while(true) { System.out.print( ); System.out.println(a); a=a+1; if(a>5) break; 1 2 3 4 5 http://apcs.cold.rocks 16

while public class ControlFlow { public static void main(string[] args) { int a=1; while(a<=5) { System.out.print( ); System.out.println(a); a=a+1; 1 2 3 4 5 http://apcs.cold.rocks 17

for for(initialization;condition;afterthought) { //BLOCK 1 INITIALIZATION while(condition) { //BLOCK 1 AFTERTHOUGHT Two are equivalent http://apcs.cold.rocks 18

for for(initialization;condition;afterthought) { //BLOCK 1 Works like while, but INITIALIZATION and AFTERTHOUGHT are added. http://apcs.cold.rocks 19

for public class ControlFlow { public static void main(string[] args) { for(int a=1; a<=5; a=a+1) { System.out.print( ); System.out.println(a); 1 2 3 4 5 http://apcs.cold.rocks 20

Abbreviation? a = a + b; a += b; Are equivalent a += 1; a++; Are (almost) equivalent http://apcs.cold.rocks 21

for for(int i=0; i<n; i++) { //THIS BLOCK WILL BE REPEATED n TIMES There are many ways to repeat something n times But this one is the formula http://apcs.cold.rocks 22

Your turn Print all odd numbers between 0 to 100 http://apcs.cold.rocks 23

Nested loops for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { System.out.println( ); How many s? http://apcs.cold.rocks 24

for public class ControlFlow { public static void main(string[] args) { for(int i=0; i<3; i++) { for(int j=0; j<4; j++) { System.out.print( ( ); System.out.print(i); System.out.print(, ); System.out.print(j); System.out.println( ) ); (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) http://apcs.cold.rocks 25

for public class ControlFlow { public static void main(string[] args) { for(int i=0; i<5; i++) { for(int j=0; j<=i; j++) { System.out.print( * ); System.out.println(); * ** *** **** ***** http://apcs.cold.rocks 26

Your turn Print : ***** **** *** ** * Want to challenge? : * ** *** **** ***** http://apcs.cold.rocks 27