Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Control structures  Algorithm & flowchart  If statements  While statements.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Introduction to Computers and Programming Lecture 5 New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
The University of Texas – Pan American
CIS Computer Programming Logic
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Introduction to Java Java Translation Program Structure
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter 2.1 Control Structures (Selection)
Lecturer CS & IT Department UOS MBDIN
Chapter 4 – Control Structures Part 1
Programming Fundamentals
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1
Structural Program Development: If, If-Else
Presentation transcript:

Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University

Road Map Review Div / Mod Reverse digits of a number boolean type Algorithms Representing programs –Pseudocode –Flow charts Control structures –overview –introduction to if statement Reading –Chapter 2: 2.10 –Chapter 3: 3.1 – 3.2.2

3 div mod worksheet review

4 Why do we use constants in our programs instead of literal values? Is the following constant declaration good style? final int total = 5; Name 4 different numeric data types? Is there a difference between the way 4.0 (double) and 4 (int) are represented by the computer? What does it mean to cast a value? When must you explicitly cast a value? If you mix numeric data types on the right hand side of a gets operator, what happens?

5 Review What happens if you try to place a literal value in an integer type that is too big for the variable? –For example: byte b = 999; –For example: int i = ; What if you go beyond an integer type’s bounds using arithmetic? Which has higher precedence: –+ (addition) –% (modulus)

6 Boolean values Java provides a type just for true and false evaluation. Named after George Boole, the English mathematician who published “An investigation into the Laws of Thought” in 1854 which began Boolean logic. Any Boolean expression will evaluate to either true or false.

Relational Operators Not Equal to!= Equal to== Less than or equal to<= Greater than or equal to>= Less than< Greater than> MeaningOperator  2003 Prentice Hall, Inc. All rights reserved.

8 Example import javax.swing.JOptionPane; public class BoolTest { public static void main(String[] args) { boolean boolVar; boolVar = false; System.out.println ("boolVar: " + boolVar); int a = 10; int b = 10; boolVar = ( a == b); System.out.println ("boolVar: " + boolVar); System.out.println (a == b); System.out.println (a != b); System.out.println (a < b); System.out.println (a <= b); System.out.println (a > b); System.out.println (a >= b); } boolVar: false boolVar: true truefalsefalsetruefalsetrue

Equality v. Assignment Remember Gets not Equals! ( grade = 100 ) Will not evaluate to true or false In this case, we are using a single = character. (We really want to use == )

Introduction to Problem Solving with Computers Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing a program: –Know what “building blocks” are available –Use good programming principles

Algorithms Computing problems –All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of –Actions to be executed –The order in which these actions are to be executed Program control –Specify order in which statements are to executed Examples of problems: –Determining the class average for a final exam –Sorting a list of names in alphabetical order  2000 Prentice Hall, Inc. All rights reserved.

Pseudocode –Artificial, informal language that helps us develop algorithms –Similar to everyday English –Not actually executed on computers –Helps us “think out” a program before writing it Easy to convert into a corresponding Java program Consists only of executable statements –For example, declarations and import statements are not used in pseudocode.  2000 Prentice Hall, Inc. All rights reserved.

What is a “Flow Chart?” A flow chart is a visual tool that helps you understand the flow of your program. –Graphical representation of program structure Different shapes have different meaning.

Flow Chart Basics 1 Rectangles represent statements of work. For example: System.out.println(); Diamonds (decision symbol) contain conditions flow line flow line Connector symbol

Control Structures

Control the flow of a program Normally, in Java, statements are executed in sequential order Control structures allow the programmer to specify that a statement other than the next be executed –i.e. programmer can control what’s executed in which order

Three Basic Control Structures All programs can be written with just these types of structures –Sequence structure Statements run one after the other –Selection structure Depending on a condition, do one thing; otherwise, do something else Examples in Java: if, if else, and switch. –Repetition structure Repeat some actions over and over Examples in Java: for loops, while loops, and do/while loops.

The if structure Pseudocode: if some Boolean expression is true do this Example: if ( x == y ) { System.out.println(" x is equal to y!" ) ; } Every procedural / OO programming language has some form of an if statement.

Another if example if ( temperature >= 85 ) { System.out.println( "It is hot out!" ); }

if Flow Chart print “It is hot” temperature >=85 true false

if with a twist: if else Pseudocode: if some Boolean expression is true do this otherwise do something else Example: if ( grade >= 65 ) System.out.println ( "You passed!" ); else System.out.println ( "You failed!" );

if/else Flow Chart grade >=60 Print “You passed” Print “You failed” True False

Blocks To run several lines of code together, you must include them within a block For example: if ( grade >= 60 ) { System.out.println ( "You passed!" ); System.out.println ( "Congratulations!" ); }

Indentation Everything within the block of code (even if it is an implicit block because we only use one statement) should be indented –helps you see the block at a quick glance. Avoid writing code like this: if (grade >= 65) { System.out.println("You passed!!!\n"); System.out.println ("Congratulations!\n"); } This is valid Java code, but it is not easy to view the block: bad style

25 Common error: misplaced semi-colon Remember, Java requires that you use a semicolon to terminate a statement. A complete if statement is formed as follows: if (boolean expression) Statement or block of code; Therefore, if you place a semicolon after the conditional as in if (boolean expression); Statement or block of code; The compiler will interpret the semicolon as a null statement. In other words, nothing will happen if the expression evaluates to true and the statement of block of code will be executed whether or not the boolean expression is true.

Example import javax.swing.JOptionPane; public class PassFail { public static void main(String[] args) { int grade; String gradeAsString; gradeAsString = JOptionPane.showInputDialog(null,"What is your grade?"); grade = Integer.parseInt (gradeAsString); /* find out if grade is passing */ if ( grade >= 65 ) { System.out.println ( "You passed!!!" ); System.out.println ( "Congratulations!" ); } else { System.out.println ("You failed!"); } System.exit (0); }