SlideShare a Scribd company logo
1 of 28
Introduction to Computer Science
Computer Programming
Lecture d
This material (Comp 4 Unit 4) was developed by Oregon Health & Science University, funded by the Department
of Health and Human Services, Office of the National Coordinator for Health Information Technology under
Award Number 90WT0001.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
Computer Programming
Learning Objectives - 1
• 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)
2
Computer Programming
Learning Objectives - 2
• 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)
3
Control Structures
• Control structures determine the execution
order of a program
• Conditional statements
– if
– case or switch
• Repetitive statements – loops
– while
– for
– do while
4
“if” Statements in Java
• “if” statement presents 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!");
}
5
“if” and “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");
}
6
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")
7
Conditional Expressions
• Use comparison 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
8
Code Example
• Write an “if” statement that will output the
category for a calculated BMI
BMI Category
< 18.5 Underweight
18.5 – 24.9 Normal
25.0 – 29.9 Overweight
>=30 Obese
Table 1: BMI table.
9
BMI Program Using Nested “If”
10
“while” Loop
• Loops are sections of code that continue
to execute while a certain condition is true
• “while” loop is the simplest loop
11
“while” Loop - Example
count = 5;
while (count >= 0) // while
condition
{
System.out.println(count);
count = count - 1; // value
changes
}
12
“while” Loop Example Output
• Output from the statement
5
4
3
2
1
0
13
“for” Loop - 1
• “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
14
“for” Loop - 2
Example
for (i = 0; i < 5; i++)
{
System.out.println(i);
}
• Output from
example
0
1
2
3
4
15
Example
• Modify BMI program
– Output BMI category
– Calculate BMI more than once
16
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
whether to calculate
another BMI
7. If yes, go back to
step 1
8. If no, end
17
BMI Program, Revised - 1
18
BMI Program, Revised - 2
19
BMI Program, Revised - 3
20
Sample Output - 1
Welcome to the BMI calculator
Enter weight in kg
68
Enter height in m
1.27
BMI is 42.16008432016864
Obese
Do you want to calculate another?
Enter 1 for yes and 0 for no
1
21
Sample Output - 2
Enter weight in kg
55
Enter height in m
1.5
BMI is 24.444444444444443
Normal weight
Do you want to calculate another?
Enter 1 for yes and 0 for no
0
Good Bye!
22
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
23
Modules
• Way of separating code, usually by
function
– Allows for reuse
– Easier to maintain
• Procedures, functions, methods are all
modules
• Objects are as well
24
Module Example
• Example
public void printAreaCircle(double
radius)
{
double area = 3.14*radius*radius;
System.out.println("Area is " +
area);
}
25
Computer Programming
Summary – Lecture d
• Control structures determine the execution
order of the program statements
• “if” statements allow conditional execution
• Loops allow for repeated execution
• Data structures allow data to be grouped
together
• Modules are used to divide code into
smaller units by function or purpose, and
for convenience and clarity
26
Computer Programming
References – Lecture d
References
Eck, D. (2011). Introduction to Programming Using Java (6th ed.). Retrieved from
http://math.hws.edu/javanotes/.
Morley, D., & Parker, C.S. (2010). Chapter 13: Program Development and Programming
Languages. In Understanding Computers Today and Tomorrow, 12th Edition
introductory. Boston: Course Technology.
Parsons, J.J., & Oja, D. (2010). Chapter 12: Computer Programming. In New
Perspectives on Computer Concepts 2011: Comprehensive (13th ed.). Boston:
Course Technology.
Gosling, J. (1995, February). Java: An Overview. Reprinted in: Treichel, J. & Holzer, M.,
(Eds.). Sun Microsystems Laboratories: The First Ten Years 1991−2001. (7-2).
Sierra, K., & Bates, B. (2009). Head First Java (2nd Ed.). O’Reilly Media.
Charts, Tables, Figures
Table 1: BMI Table. Example using more complex conditional expressions.
27
Introduction to Computer Science
Computer Programming
Lecture d
This material was developed by Oregon
Health & Science University, funded by the
Department of Health and Human Services,
Office of the National Coordinator for Health
Information Technology under Award
Number 90WT0001.
28

More Related Content

What's hot

Transfer Learning for Improving Model Predictions in Robotic Systems
Transfer Learning for Improving Model Predictions  in Robotic SystemsTransfer Learning for Improving Model Predictions  in Robotic Systems
Transfer Learning for Improving Model Predictions in Robotic SystemsPooyan Jamshidi
 
1.5.ensemble learning with apache spark m llib 1.5
1.5.ensemble learning with apache spark m llib 1.51.5.ensemble learning with apache spark m llib 1.5
1.5.ensemble learning with apache spark m llib 1.5leorick lin
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3sumitbardhan
 
An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...
An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...
An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...Pooyan Jamshidi
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked ListsAfaq Mansoor Khan
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data typesLavanyaJ28
 
A report on designing a model for improving CPU Scheduling by using Machine L...
A report on designing a model for improving CPU Scheduling by using Machine L...A report on designing a model for improving CPU Scheduling by using Machine L...
A report on designing a model for improving CPU Scheduling by using Machine L...MuskanRath1
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 

What's hot (20)

1 c prog1
1 c prog11 c prog1
1 c prog1
 
Transfer Learning for Improving Model Predictions in Robotic Systems
Transfer Learning for Improving Model Predictions  in Robotic SystemsTransfer Learning for Improving Model Predictions  in Robotic Systems
Transfer Learning for Improving Model Predictions in Robotic Systems
 
1.5.ensemble learning with apache spark m llib 1.5
1.5.ensemble learning with apache spark m llib 1.51.5.ensemble learning with apache spark m llib 1.5
1.5.ensemble learning with apache spark m llib 1.5
 
Packages and Datastructures - Python
Packages and Datastructures - PythonPackages and Datastructures - Python
Packages and Datastructures - Python
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...
An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...
An Uncertainty-Aware Approach to Optimal Configuration of Stream Processing S...
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data types
 
Control statements
Control statementsControl statements
Control statements
 
A report on designing a model for improving CPU Scheduling by using Machine L...
A report on designing a model for improving CPU Scheduling by using Machine L...A report on designing a model for improving CPU Scheduling by using Machine L...
A report on designing a model for improving CPU Scheduling by using Machine L...
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Cs 331 Data Structures
Cs 331 Data StructuresCs 331 Data Structures
Cs 331 Data Structures
 
Co&amp;al lecture-07
Co&amp;al lecture-07Co&amp;al lecture-07
Co&amp;al lecture-07
 
Co&amp;al lecture-08
Co&amp;al lecture-08Co&amp;al lecture-08
Co&amp;al lecture-08
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Co&amp;al lecture-05
Co&amp;al lecture-05Co&amp;al lecture-05
Co&amp;al lecture-05
 

Similar to Computer Programming - Lecture D

Programming with C++
Programming with C++Programming with C++
Programming with C++ssuser802d47
 
Introduction to OpenSees by Frank McKenna
Introduction to OpenSees by Frank McKennaIntroduction to OpenSees by Frank McKenna
Introduction to OpenSees by Frank McKennaopenseesdays
 
Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)
Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)
Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)Peter Tröger
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
 
1 Introduction to C Programming.pptx
1 Introduction to C Programming.pptx1 Introduction to C Programming.pptx
1 Introduction to C Programming.pptxaarockiaabinsAPIICSE
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptxTadiwaMawere
 
Accelerating the Development of Efficient CP Optimizer Models
Accelerating the Development of Efficient CP Optimizer ModelsAccelerating the Development of Efficient CP Optimizer Models
Accelerating the Development of Efficient CP Optimizer ModelsPhilippe Laborie
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax Kleiner
 
Parallel Computing - Lec 6
Parallel Computing - Lec 6Parallel Computing - Lec 6
Parallel Computing - Lec 6Shah Zaib
 
Parallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptxParallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptxkrnaween
 

Similar to Computer Programming - Lecture D (20)

Programming with C++
Programming with C++Programming with C++
Programming with C++
 
Introduction to OpenSees by Frank McKenna
Introduction to OpenSees by Frank McKennaIntroduction to OpenSees by Frank McKenna
Introduction to OpenSees by Frank McKenna
 
Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)
Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)
Dependable Systems - Structure-Based Dependabiilty Modeling (6/16)
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
1 Introduction to C Programming.pptx
1 Introduction to C Programming.pptx1 Introduction to C Programming.pptx
1 Introduction to C Programming.pptx
 
Plc part 3
Plc  part 3Plc  part 3
Plc part 3
 
11 whiteboxtesting
11 whiteboxtesting11 whiteboxtesting
11 whiteboxtesting
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptx
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Accelerating the Development of Efficient CP Optimizer Models
Accelerating the Development of Efficient CP Optimizer ModelsAccelerating the Development of Efficient CP Optimizer Models
Accelerating the Development of Efficient CP Optimizer Models
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
 
Parallel Computing - Lec 6
Parallel Computing - Lec 6Parallel Computing - Lec 6
Parallel Computing - Lec 6
 
1.My Presentation.pptx
1.My Presentation.pptx1.My Presentation.pptx
1.My Presentation.pptx
 
Lect1.pptx
Lect1.pptxLect1.pptx
Lect1.pptx
 
L1
L1L1
L1
 
Parallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptxParallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptx
 

More from CMDLearning

What is Health Informatics - Lecture B
What is Health Informatics - Lecture BWhat is Health Informatics - Lecture B
What is Health Informatics - Lecture BCMDLearning
 
Evolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture DEvolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture DCMDLearning
 
Evolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture CEvolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture CCMDLearning
 
Evolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture BEvolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture BCMDLearning
 
Evolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture AEvolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture ACMDLearning
 
Public Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture CPublic Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture CCMDLearning
 
Public Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture BPublic Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture BCMDLearning
 
Public Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture APublic Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture ACMDLearning
 
Public Health (Part 1) Lecture C
Public Health (Part 1) Lecture CPublic Health (Part 1) Lecture C
Public Health (Part 1) Lecture CCMDLearning
 
Public Health (Part 1) Lecture B
Public Health (Part 1) Lecture BPublic Health (Part 1) Lecture B
Public Health (Part 1) Lecture BCMDLearning
 
Public Health (Part 1) Lecture A
Public Health (Part 1) Lecture APublic Health (Part 1) Lecture A
Public Health (Part 1) Lecture ACMDLearning
 
Regulating Healthcare - Lecture E
Regulating Healthcare - Lecture ERegulating Healthcare - Lecture E
Regulating Healthcare - Lecture ECMDLearning
 
Regulating Healthcare - Lecture D
Regulating Healthcare - Lecture DRegulating Healthcare - Lecture D
Regulating Healthcare - Lecture DCMDLearning
 
Regulating Healthcare - Lecture C
Regulating Healthcare - Lecture CRegulating Healthcare - Lecture C
Regulating Healthcare - Lecture CCMDLearning
 
Regulating Healthcare - Lecture A
Regulating Healthcare - Lecture ARegulating Healthcare - Lecture A
Regulating Healthcare - Lecture ACMDLearning
 
Regulating Healthcare - Lecture B
Regulating Healthcare - Lecture BRegulating Healthcare - Lecture B
Regulating Healthcare - Lecture BCMDLearning
 
Financing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture CFinancing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture CCMDLearning
 
Financing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture BFinancing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture BCMDLearning
 
Financing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture AFinancing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture ACMDLearning
 
Financing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture DFinancing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture DCMDLearning
 

More from CMDLearning (20)

What is Health Informatics - Lecture B
What is Health Informatics - Lecture BWhat is Health Informatics - Lecture B
What is Health Informatics - Lecture B
 
Evolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture DEvolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture D
 
Evolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture CEvolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture C
 
Evolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture BEvolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture B
 
Evolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture AEvolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture A
 
Public Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture CPublic Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture C
 
Public Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture BPublic Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture B
 
Public Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture APublic Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture A
 
Public Health (Part 1) Lecture C
Public Health (Part 1) Lecture CPublic Health (Part 1) Lecture C
Public Health (Part 1) Lecture C
 
Public Health (Part 1) Lecture B
Public Health (Part 1) Lecture BPublic Health (Part 1) Lecture B
Public Health (Part 1) Lecture B
 
Public Health (Part 1) Lecture A
Public Health (Part 1) Lecture APublic Health (Part 1) Lecture A
Public Health (Part 1) Lecture A
 
Regulating Healthcare - Lecture E
Regulating Healthcare - Lecture ERegulating Healthcare - Lecture E
Regulating Healthcare - Lecture E
 
Regulating Healthcare - Lecture D
Regulating Healthcare - Lecture DRegulating Healthcare - Lecture D
Regulating Healthcare - Lecture D
 
Regulating Healthcare - Lecture C
Regulating Healthcare - Lecture CRegulating Healthcare - Lecture C
Regulating Healthcare - Lecture C
 
Regulating Healthcare - Lecture A
Regulating Healthcare - Lecture ARegulating Healthcare - Lecture A
Regulating Healthcare - Lecture A
 
Regulating Healthcare - Lecture B
Regulating Healthcare - Lecture BRegulating Healthcare - Lecture B
Regulating Healthcare - Lecture B
 
Financing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture CFinancing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture C
 
Financing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture BFinancing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture B
 
Financing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture AFinancing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture A
 
Financing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture DFinancing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture D
 

Recently uploaded

(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...
(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...
(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...indiancallgirl4rent
 
raisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
raisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meetraisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
raisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Call Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In Raipur
Call Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In RaipurCall Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In Raipur
Call Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In Raipurgragmanisha42
 
Call Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171Call Girls Service Gurgaon
 
❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...
❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...
❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...Gfnyt.com
 
Muzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Muzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetMuzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Muzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Chandigarh Escorts, 😋9988299661 😋50% off at Escort Service in Chandigarh
Chandigarh Escorts, 😋9988299661 😋50% off at Escort Service in ChandigarhChandigarh Escorts, 😋9988299661 😋50% off at Escort Service in Chandigarh
Chandigarh Escorts, 😋9988299661 😋50% off at Escort Service in ChandigarhSheetaleventcompany
 
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetHubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR Call G...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR   Call G...❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR   Call G...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR Call G...Gfnyt.com
 
(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...
(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...
(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...indiancallgirl4rent
 
bhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
bhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meetbhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
bhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...
Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...
Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...Sheetaleventcompany
 
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real MeetChandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meetpriyashah722354
 
Tirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Tirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetTirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Tirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetMangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Nanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Nanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetNanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Nanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Call Girls Patiala Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Patiala Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Patiala Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Patiala Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Call Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In Faridabad
Call Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In FaridabadCall Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In Faridabad
Call Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In Faridabadgragmanisha42
 

Recently uploaded (20)

(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...
(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...
(Ajay) Call Girls in Dehradun- 8854095900 Escorts Service 50% Off with Cash O...
 
raisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
raisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meetraisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
raisen Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Call Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In Raipur
Call Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In RaipurCall Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In Raipur
Call Girl Raipur 📲 9999965857 ヅ10k NiGhT Call Girls In Raipur
 
Call Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 9907093804 Top Class Call Girl Service Available
 
VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171
 
❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...
❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...
❤️♀️@ Jaipur Call Girl Agency ❤️♀️@ Manjeet Russian Call Girls Service in Jai...
 
Muzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Muzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetMuzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Muzaffarpur Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
 
Chandigarh Escorts, 😋9988299661 😋50% off at Escort Service in Chandigarh
Chandigarh Escorts, 😋9988299661 😋50% off at Escort Service in ChandigarhChandigarh Escorts, 😋9988299661 😋50% off at Escort Service in Chandigarh
Chandigarh Escorts, 😋9988299661 😋50% off at Escort Service in Chandigarh
 
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetHubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR Call G...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR   Call G...❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR   Call G...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Meghna Jaipur Call Girls Number CRTHNR Call G...
 
(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...
(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...
(Sonam Bajaj) Call Girl in Jaipur- 09257276172 Escorts Service 50% Off with C...
 
bhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
bhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meetbhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
bhubaneswar Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...
Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...
Punjab❤️Call girls in Mohali ☎️7435815124☎️ Call Girl service in Mohali☎️ Moh...
 
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real MeetChandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
 
Tirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Tirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetTirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Tirupati Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetMangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Nanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Nanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetNanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Nanded Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Call Girls Patiala Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Patiala Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Patiala Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Patiala Just Call 9907093804 Top Class Call Girl Service Available
 
Call Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In Faridabad
Call Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In FaridabadCall Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In Faridabad
Call Girls Service Faridabad 📲 9999965857 ヅ10k NiGhT Call Girls In Faridabad
 

Computer Programming - Lecture D

  • 1. Introduction to Computer Science Computer Programming Lecture d This material (Comp 4 Unit 4) was developed by Oregon Health & Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number 90WT0001. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
  • 2. Computer Programming Learning Objectives - 1 • 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) 2
  • 3. Computer Programming Learning Objectives - 2 • 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) 3
  • 4. Control Structures • Control structures determine the execution order of a program • Conditional statements – if – case or switch • Repetitive statements – loops – while – for – do while 4
  • 5. “if” Statements in Java • “if” statement presents 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!"); } 5
  • 6. “if” and “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"); } 6
  • 7. 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") 7
  • 8. Conditional Expressions • Use comparison 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 8
  • 9. Code Example • Write an “if” statement that will output the category for a calculated BMI BMI Category < 18.5 Underweight 18.5 – 24.9 Normal 25.0 – 29.9 Overweight >=30 Obese Table 1: BMI table. 9
  • 10. BMI Program Using Nested “If” 10
  • 11. “while” Loop • Loops are sections of code that continue to execute while a certain condition is true • “while” loop is the simplest loop 11
  • 12. “while” Loop - Example count = 5; while (count >= 0) // while condition { System.out.println(count); count = count - 1; // value changes } 12
  • 13. “while” Loop Example Output • Output from the statement 5 4 3 2 1 0 13
  • 14. “for” Loop - 1 • “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 14
  • 15. “for” Loop - 2 Example for (i = 0; i < 5; i++) { System.out.println(i); } • Output from example 0 1 2 3 4 15
  • 16. Example • Modify BMI program – Output BMI category – Calculate BMI more than once 16
  • 17. 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 whether to calculate another BMI 7. If yes, go back to step 1 8. If no, end 17
  • 21. Sample Output - 1 Welcome to the BMI calculator Enter weight in kg 68 Enter height in m 1.27 BMI is 42.16008432016864 Obese Do you want to calculate another? Enter 1 for yes and 0 for no 1 21
  • 22. Sample Output - 2 Enter weight in kg 55 Enter height in m 1.5 BMI is 24.444444444444443 Normal weight Do you want to calculate another? Enter 1 for yes and 0 for no 0 Good Bye! 22
  • 23. 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 23
  • 24. Modules • Way of separating code, usually by function – Allows for reuse – Easier to maintain • Procedures, functions, methods are all modules • Objects are as well 24
  • 25. Module Example • Example public void printAreaCircle(double radius) { double area = 3.14*radius*radius; System.out.println("Area is " + area); } 25
  • 26. Computer Programming Summary – Lecture d • Control structures determine the execution order of the program statements • “if” statements allow conditional execution • Loops allow for repeated execution • Data structures allow data to be grouped together • Modules are used to divide code into smaller units by function or purpose, and for convenience and clarity 26
  • 27. Computer Programming References – Lecture d References Eck, D. (2011). Introduction to Programming Using Java (6th ed.). Retrieved from http://math.hws.edu/javanotes/. Morley, D., & Parker, C.S. (2010). Chapter 13: Program Development and Programming Languages. In Understanding Computers Today and Tomorrow, 12th Edition introductory. Boston: Course Technology. Parsons, J.J., & Oja, D. (2010). Chapter 12: Computer Programming. In New Perspectives on Computer Concepts 2011: Comprehensive (13th ed.). Boston: Course Technology. Gosling, J. (1995, February). Java: An Overview. Reprinted in: Treichel, J. & Holzer, M., (Eds.). Sun Microsystems Laboratories: The First Ten Years 1991−2001. (7-2). Sierra, K., & Bates, B. (2009). Head First Java (2nd Ed.). O’Reilly Media. Charts, Tables, Figures Table 1: BMI Table. Example using more complex conditional expressions. 27
  • 28. Introduction to Computer Science Computer Programming Lecture d This material was developed by Oregon Health & Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number 90WT0001. 28

Editor's Notes

  1. Welcome to Introduction to Computer Science: Computer Programming. This is lecture d. The component, Introduction to Computer Science, provides a basic overview of computer architecture; data organization, representation, and structure; structure of programming languages; networking, and data communication. It also includes the basic terminology of computing.
  2. The learning objectives for this unit, Computer Programming, are to: Define the purpose of programming languages. Differentiate between the different types of programming languages and list commonly used ones. Explain the compiling and interpreting process for computer programs.
  3. Learn basic programming concepts including variable declarations, assignment statements, expressions, conditional statements, and loops. And describe advanced programming concepts including objects and modularity.
  4. In the programming example of the body-mass index, or BMI, calculator in the previous lecture, every statement was executed in the order it appeared. But this is not always the case, and frequently it is necessary to change the order of statement execution in a program. For example, it might be necessary to have a statement execute conditionally, meaning it only executes if some condition is true. There are two types of conditional statements – “if” and “case-or-switch” statements. At other times, a statement may repeat a section of code; this is called looping. There are different types of loops –for example, ”while,” “for,” and “do while.”
  5. “if” statements are conditional, which means they execute only if a specified condition is true. An example of an “if” statement in Java is given on this slide. The first line of the “if” statement begins with “if” followed by a condition in parentheses. When this condition evaluates to true – in this case if weight is less than zero, then the body of the “if” statement executes. The body is the lines of code following the “if” statement, enclosed by curly brackets. In this example, there is only one statement in the “if” body – an output statement of "Error!" There could be multiple statements in the “if” body – as long as they appear between the open and close curly brackets. When the condition is false – in this case, “if” weight is greater than or equal to zero, the body of the “if” statement is skipped. Note that there is no semicolon after the condition of an “if” statement.
  6. “if” statements can also have an “else” clause that provides the statements that execute when the condition is false. So, this example has the same condition as the “if” statement on the previous slide: When the variable “weight” is less than zero, the message "Error!" will be printed to the screen. When the condition is false – if weight is greater than or equal to zero - the message "No error" will be printed to the screen. Within the bodies of the “if” statement and the “else” clause, there can be multiple statements.
  7. It is also possible to build nested “if” statements where multiple conditions are checked. When a condition evaluates to true, the body of that “if” statement executes, but the rest of the statements are skipped. For example, in this code segment, if the variable “number” has a value less than zero, "Negative" is printed and the rest of the “if” statements are skipped. The next statement that executes is the one after the “if” statement; in this case, it is printing "Done" on the screen. If the variable “number” is greater than zero, the statement prints "Positive" to the screen, followed by message "Done". Finally if the variable number is equal to zero, the word "Zero" is printed to the screen, followed by the word "Done".
  8. “If” statements use conditions, which are expressions that are made up of comparisons. If there are multiple comparisons, they are combined using what are called logical operators. The comparison operators that can be used are: Less than or greater than Less than or equal to, or, greater than or equal to Equal to or not equal to Note that the equality operator in Java is a double equal sign; recall that a single equal sign is the assignment operator. “Not equal to” is an exclamation point followed by a single equal sign. If there are multiple comparisons within a single condition, they are combined using logic operators. There is the logic operator, “AND,” which is double ampersands in Java. In order for this logical operator to evaluate to true, both comparisons must be true. The logical operator, OR, is represented in Java by two vertical lines, found above the backward slash on the keyboard. This operator is true when just one of the comparisons is true, or both. The NOT operator is the exclamation point, and it evaluates to true when the comparison is false.
  9. This is an example that uses more complex conditional expressions. The last lecture presented a simple program that calculates the body mass index, or BMI, for a given weight and height. Now, it is time to determine the weight category for BMI. Here is a table that gives the different weight categories for ranges of BMI. For example, a BMI that is less than 18.5 is categorized as “Underweight”. A BMI between 18.5 and 24.9 is “Normal”; a BMI between 25 and 29.9 is “Overweight”. Finally, a BMI greater than or equal to thirty is categorized as “Obese”. The next slide will present an “if” statement that will output the category for the BMI variable’s value in the previous program.
  10. Here is the “if” statement. It would appear in the program after the BMI was calculated. The first “if” statement checks for the underweight category, which is for BMI less than 18.5. The next nested “if” statement checks for normal weight – BMI that is within the range 18.5 to 25.0. Its condition is BMI greater than or equal to 18.5 AND BMI less than 25.0. The next nested “if” statement checks for overweight – BMI in the range twenty-five to thirty. Its condition is BMI greater than or equal to twenty-five AND BMI less than thirty. The final category is obese, which is greater than or equal to thirty. But it is not necessary to include that final condition since this statement includes a final “else” statement. This statement executes only if all the other conditions are not true, which means that the BMI must be greater or equal to thirty. Although the condition could be included, it is not necessary. Note that each condition after the first one is part of an “else” statement containing another “if”. This assures that only one category will be printed out – as soon as one of the conditions is true, the category is output and the rest of the “if” statement is skipped.
  11. This slide looks at loops in Java. Loops are sections of code that are repeated. They will continue to repeat as long as a given condition is true. “while” loops are the simplest loop. The “while” loop contains a condition, similar to the “if” statement. As in the “if” statement, there is no semicolon after the condition specified in the “while” loop. After the condition, the loop body appears; it is enclosed within the curly brackets. All the statements within the loop body will be executed during each repetition of the loop. When the end of the loop body is reached, the program returns to re-evaluate the condition of the “while” loop. If it is still true, then the loop repeats again. If it is not, then the loop is skipped and the first statement after the loop executes.
  12. This slide shows an example of a “while” loop. The example has a variable named “count”, which is assigned the value of five. The variable “count” is used in the condition of the “while” loop. This condition is “count” greater than or equal to zero. The first time through the loop, the condition is true, so the loop executes. The value of “count” is printed and the “count's” value is reduced by one. So, the second time through the loop, “count” is now four, which is still greater than or equal to zero, so the loop executes again. This continues until “count” has been reduced to negative one, at which point the condition is no longer true and the loop is skipped. Note that the value of “count” must change within the loop or the loop will execute forever – an infinite loop. This is a common coding error, often made by inexperienced programmers.
  13. Here is the output from the “while” loop on the previous slide. During each iteration of the loop, the value of “count” is printed and the value is decremented, or reduced, by one. Eventually, the “count” is less than zero, so the loop ends.
  14. A “for” loop is another type of loop. The “for” loop starts with the word "for" and is followed by a three-part statement within parentheses. The first part sets the initial value for what is called the loop control variable. The second part is the condition for the loop; it is usually a comparison using the loop control variable. As long as this condition is true, the loop will continue to execute. The final part is the update – it changes the value of the loop control variable. Presumably, this update will eventually change the loop control variable such that the condition becomes false. “For” loops are useful when the number of iterations of the loop is known.
  15. In this example of a “for” loop, the loop control variable “i” is initialized to zero. The condition is “i” less than five and the update is to increment “i” by one. i++ is shorthand notation for incrementing “i” by one. The loop body is a single statement that outputs the value of “i” to the screen. Here is the output from the previous example. “i” started at the value zero and the loop continued while “i” was less than four, incrementing by one each time. During each iteration, the value of “i” was output, so as shown here, the values zero through four were output by the loop.
  16. This program will modify the BMI calculator program by adding the “if” statement for outputting the BMI category and allowing the program to calculate more than one BMI value.
  17. Here is the design. The program will still need to read in the weight and height from the user. It will calculate the BMI and then output it. Next, it will determine the category for the BMI and output it. Finally, the program will ask the user if they want to calculate another BMI. If they do, it will go back to step one and repeat. If not, the program will end.
  18. Here is the first part of the new program. The entire program would not fit on one slide, so it is shown on three slides. To indicate what has changed, the added code is highlighted – the yellow highlights here show the changes needed to implement the loop. In this case, the program added another variable called “anotherBMI”; it is an integer variable that can be initialized to one. The “while” loop repeats the BMI input and output loop. The condition of the “while” loop is “anotherBMI” equal to one. The first time through, the program has already initialized “anotherBMI” to one so that the “while” loop will execute. The rest of the code shown on the slide is the same as the code for the original program – getting the weight and height from the user. The code is continued on the following slide.
  19. Now, the program will calculate and output the BMI. It still includes the heading of the “while” loop, which was shown on the previous slide, but it skips showing the height and weight input and BMI calculation. The next part in the code outputs the BMI category. This code is highlighted in grey. It is the same “if” statement shown previously. The code is continued on the following slide.
  20. The final part of the loop is also new – this is where the user is asked if he or she wants to calculate another BMI. If the user does want to calculate another BMI, he or she will enter the number one; otherwise the user will enter a “zero”; actually the user could enter anything other than “one” to end the loop. The next value for “anotherBMI” is read in, and the condition for the “while” loop is evaluated again. If “anotherBMI” is one, then the loop will execute again. If not, the loop will be skipped, and the statement following the loop will be executed. In this case, it is a statement that prints "Good Bye!" to the screen.
  21. Here is sample output from the program. As before, the input entered by the user is given in purple. As shown here, the user is welcomed to the BMI calculator and asked to enter weight in kilograms and height in meters. The BMI is calculated and printed; in this case it is 42.16. The category for this BMI is "Obese", which also gets printed. Then the user is asked if he or she wants to calculate another.
  22. The user enters the number one, which means "yes" for this program, and the loop repeats again. The user is once again asked to enter a weight and a height. The BMI is calculated and printed; now it is 24.444, which has the category “normal weight”. Finally, the user is asked if he or she wants to calculate another; this time, the user enters zero, which means "no". The loop is done and the next statement is executed, which outputs "Good Bye!" to the screen.
  23. The next construct common for most programming languages is data structures. Data structures allow programs to store multiple pieces of data together in one entity. Arrays are a very simple data structure that is supported by most programming languages. Here is an example of an array in Java. It stores ten grades, where each grade is of the data type “double”. The syntax for declaring an array is as follows: first, the data type is specified, in this case “double”; it is followed by the empty square brackets that indicate that the object being declared is an array. Next, the name of the array is provided. “new” means that we are creating a new object; it is followed by another “double”. Finally, square-bracket ten square-bracket mean that the size of the array “grade” is 10, in other words it contains 10 elements. Notice the semicolon at the end of the statement. As we learned in the previous lecture, declaration statements in Java must end in a semicolon. An index specifies a particular element in the array; it is placed within the square brackets. In this case, we're assigning the value of 95.0 to the element of the array “grade” with index one, i.e. to the first element of “grade”. There are other, more complex data structures available. Most programming languages have libraries that provide constructs to implement data structures such as linked lists, trees, and hash tables, just to name a few.
  24. The final construct of most programming languages is a way to separate code into modules. Depending on the language, modules may be called procedures, functions, or methods. The purpose of these modules is to perform some task. The task can be written once in a procedure, and then it can be reused. Also, dividing a program into separate modules provides a more organized structure to the code, making it easier to comprehend and maintain.
  25. Here is an example of a method in Java. This method calculates and prints the area of a circle; the radius of the circle is provided as an argument to this method. This method can be called multiple times with different values for the radius. Note that objects can also be considered modules and will be discussed further in the next lecture.
  26. This concludes lecture d of Computer Programming. In summary, control structures determine the order in which statements of the code are executed. “if” statements are control structures that allow for conditional execution. If the condition of an “if” statement is true, then the statements included in the body of the “if” statement are executed. If it is false, those statements are not executed; the statements following the “else” clause are executed instead, if “else” is included. Loops allow for code segments to be repeated. Data structures are ways to store multiple pieces of data together; arrays are an example of a data structure. Finally, modules are used to divide code into smaller units by function or purpose, and for convenience and clarity. Procedures, methods, and classes are all examples of modules.
  27. References slide. No audio.
  28. No audio.