Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.

Slides:



Advertisements
Similar presentations
Introduction to Information and Computer Science Computer Programming Lecture e This material (Comp4_Unit5e) was developed by Oregon Health and Science.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
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
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Introduction to Information and Computer Science Computer Programming Lecture b This material (Comp4_Unit5b), was developed by Oregon Health and Science.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Component 4: Introduction to Information and Computer Science Unit 6: Databases and SQL Lecture 3 This material was developed by Oregon Health & Science.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 2.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 5.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright 2010 by Pearson Education Building Java Programs Scanner ; if / else; while loops ; random reading: 3.3 – 3.4, 4.1, 4.5, 5.1, 5.6.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
Building Java Programs
CSC111 Quick Revision.
Introduction to Computer Science
Introduction to Computer Science
Introduction to Computer Science
Building Java Programs Chapter 4
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Building Java Programs
Repetition-Counter control Loop
SELECTION STATEMENTS (1)
Lecture 4: Conditionals
Building Java Programs Chapter 4
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
Lecture Notes – Week 3 Lecture-1
Building Java Programs
Building Java Programs
Factoring if/else code
Building Java Programs
Building Java Programs
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Factoring if/else code
Building Java Programs
Chapter 4: Loops and Iteration
Presentation transcript:

Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC

Computer Programming Learning Objectives Define the purpose of programming languages. (Lecture a) Differentiate between the different types of programming languages and list commonly used ones. (Lecture a) Explain the compiling and interpreting process for computer programs. (Lecture b) Learn basic programming concepts including variable declarations, assignment statements, expressions, conditional statements and loops. (Lectures c, d) Describe advanced programming concepts including objects and modularity. (Lecture e) 2 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Control Structures Control structures determine the execution of a program Conditional statements –if –case or switch Repetitive statements – loops –while –for –do while 3 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

If Statements in Java If statements have a condition When the condition is true, the body of the if statement executes Example: if (weight < 0) //conditions { //body of if System.out.println("Error!"); } 4 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

If Else Statements in Java If statements can include an else clause Else clause executes when condition is false if (weight < 0) { System.out.println("Error!"); } else { System.out.println("No error"); } 5 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Nested If statements If statements can have multiple conditions When number is less than zero " Negative" and "Done" are printed to the screen if (number < 0) { System.out.println("Negative"); } else if (number > 0) { System.out.println("Positive"); } else { System.out.println("Zero"); } System.out.println("Done") 6 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Conditional Expressions Use comparator operators (less than, greater than) = (less than or equal to, greater than or equal to) ==, != (is equal to, is not equal to) Use logical operators to combine comparisons && (AND): Both comparisons must be true || (OR): Either comparison must be true ! (NOT): Condition must be false 7 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Code Example Write an if statement that will output the category for a calculated BMI (body mass index) 8 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d BMICategory < 18.5Underweight Normal Overweight >= 30Obese 5.1 Table: Example of more complex conditional expressions.

if (bmi < 18.5) { System.out.println("Underweight"); } else if ((bmi >= 18.5) && (bmi < 25.0)) { System.out.println("Normal weight"); } else if ((bmi >= 25.0) && (bmi < 30.0)) { System.out.println("Overweight"); } else { System.out.println("Obese"); } 9 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Loops in Java Loops are sections of code that will continue to repeat while a condition is true “While” loop is simplest loop Example count = 5; while (count >= 0) // while condition { System.out.println(count); count = count - 1; //value changes } 10 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

While Loop, contd. Output from statement Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

For Loop “For” loop is another type of loop Used when the number of iterations is known Heading sets loop control variable, compares it, and updates it Example for (i = 0; i < 5; i++) { System.out.println(i); } 12 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

For Loop, contd. Output from example Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Exercise Modify BMI program –Output BMI category –Calculate BMI more than once 14 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Program Design 1.Read in weight (kg) 2.Read in height (m) 3.Calculate BMI BMI = weight/(height * height) 4.Output BMI 5.Output BMI category 6.Prompt user if want to calculate another BMI 7.If yes, go back to step 1 8.If no, end 15 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

import java.util.*; public class CalcBMI { public static void main(String[] args) { double bmi, weight, height; int anotherBMI; Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to the BMI calculator"); anotherBMI = 1; while (anotherBMI == 1) { System.out.println("Enter weight in kg"); weight = keyboard.nextDouble(); System.out.println("Enter height in m"); height = keyboard.nextDouble(); bmi = weight/(height*height); System.out.print("BMI is "); System.out.println(bmi); 16 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

… anotherBMI = 1; while (anotherBMI == 1) { …//input height, weight; calculate BMI if (bmi < 18.5) System.out.println("Underweight"); else if ((bmi >= 18.5) && (bmi < 25.0)) System.out.println("Normal weight"); else if ((bmi >= 25.0) && (bmi < 30.0)) System.out.println("Overweight"); else System.out.println("Obese"); System.out.println("Do you want to calculate another?"); System.out.println("Enter 1 for yes and 0 for no"); anotherBMI = keyboard.nextInt(); } System.out.println("Good Bye!"); } 17 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Sample Output Welcome to the BMI calculator Enter weight in kg 68 (green) Enter height in m 1.27 (green) BMI is Obese Do you want to calculate another? Enter 1 for yes and 0 for no 1 (green) Enter weight in kg 55 (green) Enter height in m 1.5 (green) BMI is Normal weight Do you want to calculate another? Enter 1 for yes and 0 for no 0 (green) Good Bye! 18 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Data Structures Data structures are used for storing multiple pieces of data together Arrays are a simple data structure Example double[] grade = new double[10]; Array of 10 doubles for storing grades grade[1] = 95.0; Other data structures available –Linked lists, trees, hash tables 19 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Modules Way of separating code, usually by function –Allows for reuse –Easier to maintain Procedures, functions, methods are all modules Objects are as well Example public void printAreaCircle(double radius) { double area = 3.14*radius*radius; System.out.println("Area is " + area); } 20 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Computer Programming Summary – Lecture d Control structures affect the order of execution “If” statements allow for conditional execution Loops allow for repeated execution Data structures allow for data to be grouped together Modules are used to encapsulate code 21 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d

Computer Programming References – Lecture d References Eck, David. (2011) Introduction to Programming Using Java, Sixth Edition. [updated 2011 Jul 18; cited 2011 Nov 13]: Available from: Morley Deborah, Parker Charles S. (2010). Chapter 13: Program Development and Programming Languages. In: Understanding Computers Today and Tomorrow.12 th ed. Boston: Course Technology. Parsons JJ, Oja D. (2010). Chapter 12: Computer Programming. In: New Perspectives on Computer Concepts 2011: Comprehensive. 13th ed. Boston: Course Technology. The Java Language: An Overview. [Webpage]. c [updated 2007 Dec 17; cited 21 March 2011]. Available from: Sierra Kathy, Bates Bert. (2009). Head First Java, Second Edition. O’Reilly Media. Charts, Tables, Figures 5.1 Table: Example of more complex conditional expressions. 22 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d