1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }

Slides:



Advertisements
Similar presentations
1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Advertisements

Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Python Programming Language
Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps.
11-May-15 Control Structures part 2. Overview Control structures cause the program to repeat a section of code or choose between different sections of.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Chapter 1 - Getting to know Greenfoot
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
Fundamental Programming: Fundamental Programming Introduction to C++
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
For Friday Finish reading chapter 2 Complete WebCT quiz.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Chapter three Conditional Statements ©
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel J Robot Chapter 5.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
Windows Programming Lecture 03. Pointers and Arrays.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
CS 106A, Lecture 3 Problem-solving with Karel
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Variables Mr. Crone.
Selection (also known as Branching) Jumail Bin Taliba by
Eric Roberts and Jerry Cain
Administrative Matters
So, to simplify an expression using order of operations, you should:
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
karel_part4_functions_2
CS 106A, Lecture 2 Programming with Karel
Expressions and Control Flow in JavaScript
LRobot Game.
Unit-1 Introduction to Java
slides courtesy of Eric Roberts
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
CH5 – Conditional Statements
Presentation transcript:

1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }

2 karel_part3_ifElse Indenting by example How to properly indent while writing if/else structures by example! public void someMethod() { if ( ) { codeGoesHere(); allAdditionalCodeLinesUp(); soAllFirstLettersAreInTheSameColumn(); } else { sameSpacingAfterTheElse(); asWhatWasUsedAfterTheIf(); } untilAnotherIfStatementIsEncountered(); }

3 karel_part3_ifElse A word about indenting How to properly indent while writing if structures! Every time an if is used, each line after the curly bracket, {should be spaced over to the right a constant amount (say 3 spaces). –To simplify, the first letter of each method/line-of-code should be under the ( in the if statement. The same spacing is used between the brackets that follow the else When the if/else is terminated ( closing curly bracket } ), the following lines of code are moved left by the same number of spaces used above (3 was used above). –To simplify, the first letter of each method/line-of-code should be under the i in the if statement. Please review previous slide

4 karel_part3_ifElse A Final Indenting example if ( frontIsClear()) { move(); turnLeft(); } else { turnLeft(); if ( frontIsClear()) { move(); } turnLeft();

5 karel_part3_ifElse Sample Code with Objects if (karel.frontIsClear() ) { karel.move();// no danger of hitting wall } else { karel.turnLeft(); }

6 karel_part3_ifElse Sample Method public void moveOrTurnLeft() { if ( frontIsClear() ) { move(); // no danger of hitting wall } else { turnLeft(); }

7 karel_part3_ifElse Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } if ( facingSouth() ) { turnLeft(); } else { turnRight(); } move(); move();move();move();

8 karel_part3_ifElse Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); }

9 karel_part3_ifElse Simplify – top factoring move(); // changes value of boolean expression if ( beeperOnLeft() ) { turnLeft(); } else { turnRight(); }

10 karel_part3_ifElse being redundant again and again and again ha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } if ( facingNorth() ) { move(); pickTwoBeepers(); turnLeft(); }

11 karel_part3_ifElse Your Assignment Implementing the followAllBreadCrumbs method declared in HanselAndGretelRobot class. The followAllBreadCrumbs method will invoke the followBreadCrumb method 56 times. No method may contain more than seven commands. You will need to implement the followBreadCrumb method as described in handout ( Karel_part2_ifElse.doc ) for details.