Soumettre la recherche
Mettre en ligne
Control Structures: Part 1
•
Télécharger en tant que PPT, PDF
•
1 j'aime
•
143 vues
Andy Juan Sarango Veliz
Suivre
3 ciclo Universidad Nacional de Ingeniería
Lire moins
Lire la suite
Ingénierie
Signaler
Partager
Signaler
Partager
1 sur 40
Télécharger maintenant
Recommandé
Control Structures
Control Structures
Ghaffar Khan
Control structures i
Control structures i
Ahmad Idrees
04 control structures 1
04 control structures 1
Jomel Penalba
Control structure
Control structure
Samsil Arefin
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
Tech
Control structure of c
Control structure of c
Komal Kotak
Control structures in c
Control structures in c
baabtra.com - No. 1 supplier of quality freshers
Control structure in c
Control structure in c
baabtra.com - No. 1 supplier of quality freshers
Recommandé
Control Structures
Control Structures
Ghaffar Khan
Control structures i
Control structures i
Ahmad Idrees
04 control structures 1
04 control structures 1
Jomel Penalba
Control structure
Control structure
Samsil Arefin
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
Tech
Control structure of c
Control structure of c
Komal Kotak
Control structures in c
Control structures in c
baabtra.com - No. 1 supplier of quality freshers
Control structure in c
Control structure in c
baabtra.com - No. 1 supplier of quality freshers
Control structures ii
Control structures ii
Ahmad Idrees
Control statement-Selective
Control statement-Selective
Nurul Zakiah Zamri Tan
Control structure C++
Control structure C++
Anil Kumar
Control structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
Control structures in Java
Control structures in Java
Ravi_Kant_Sahu
Control structures in C
Control structures in C
baabtra.com - No. 1 supplier of quality freshers
C Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
Control Structures in Visual Basic
Control Structures in Visual Basic
Tushar Jain
Selection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
Control statements in c
Control statements in c
Sathish Narayanan
operators and control statements in c language
operators and control statements in c language
shhanks
Unit 5. Control Statement
Unit 5. Control Statement
Ashim Lamichhane
Control structures selection
Control structures selection
Online
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
CIMAP
Operators in java
Operators in java
Then Murugeshwari
C programming decision making
C programming decision making
SENA
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
Control statement in c
Control statement in c
baabtra.com - No. 1 supplier of quality freshers
Ch1 principles of software development
Ch1 principles of software development
Hattori Sidek
Ch04
Ch04
Arriz San Juan
Csphtp1 04
Csphtp1 04
HUST
more loops lecture by Professor Evan korth
more loops lecture by Professor Evan korth
hammad ali
Contenu connexe
Tendances
Control structures ii
Control structures ii
Ahmad Idrees
Control statement-Selective
Control statement-Selective
Nurul Zakiah Zamri Tan
Control structure C++
Control structure C++
Anil Kumar
Control structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
Control structures in Java
Control structures in Java
Ravi_Kant_Sahu
Control structures in C
Control structures in C
baabtra.com - No. 1 supplier of quality freshers
C Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
Control Structures in Visual Basic
Control Structures in Visual Basic
Tushar Jain
Selection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
Control statements in c
Control statements in c
Sathish Narayanan
operators and control statements in c language
operators and control statements in c language
shhanks
Unit 5. Control Statement
Unit 5. Control Statement
Ashim Lamichhane
Control structures selection
Control structures selection
Online
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
CIMAP
Operators in java
Operators in java
Then Murugeshwari
C programming decision making
C programming decision making
SENA
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
Control statement in c
Control statement in c
baabtra.com - No. 1 supplier of quality freshers
Ch1 principles of software development
Ch1 principles of software development
Hattori Sidek
Ch04
Ch04
Arriz San Juan
Tendances
(20)
Control structures ii
Control structures ii
Control statement-Selective
Control statement-Selective
Control structure C++
Control structure C++
Control structures in C++ Programming Language
Control structures in C++ Programming Language
Control structures in Java
Control structures in Java
Control structures in C
Control structures in C
C Programming: Control Structure
C Programming: Control Structure
Control Structures in Visual Basic
Control Structures in Visual Basic
Selection Statements in C Programming
Selection Statements in C Programming
Control statements in c
Control statements in c
operators and control statements in c language
operators and control statements in c language
Unit 5. Control Statement
Unit 5. Control Statement
Control structures selection
Control structures selection
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
Operators in java
Operators in java
C programming decision making
C programming decision making
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
Control statement in c
Control statement in c
Ch1 principles of software development
Ch1 principles of software development
Ch04
Ch04
Similaire à Control Structures: Part 1
Csphtp1 04
Csphtp1 04
HUST
more loops lecture by Professor Evan korth
more loops lecture by Professor Evan korth
hammad ali
Control structure
Control structure
Liam Delector
04.ppt
04.ppt
AqeelAbbas94
java - Introduction , control structures
java - Introduction , control structures
sandhyakiran10
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Prashant Sharma
Core java
Core java
kasaragaddaslide
Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
Amr E. Mohamed
Java Tutorial
Java Tutorial
ArnaldoCanelas
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
Govind Samleti
Java tut1
Java tut1
Sumit Tambe
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
Graham Royce
Java tut1
Java tut1
Sumit Tambe
Javatut1
Javatut1
desaigeeta
3 j unit
3 j unit
kishoregali
03b loops
03b loops
Manzoor ALam
9781439035665 ppt ch05
9781439035665 ppt ch05
Terry Yoast
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
alanfhall8953
Chap05
Chap05
Terry Yoast
Similaire à Control Structures: Part 1
(20)
Csphtp1 04
Csphtp1 04
more loops lecture by Professor Evan korth
more loops lecture by Professor Evan korth
Control structure
Control structure
04.ppt
04.ppt
java - Introduction , control structures
java - Introduction , control structures
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Core java
Core java
Synapseindia reviews.odp.
Synapseindia reviews.odp.
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
Java Tutorial
Java Tutorial
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
Java tut1
Java tut1
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
Java tut1
Java tut1
Javatut1
Javatut1
3 j unit
3 j unit
03b loops
03b loops
9781439035665 ppt ch05
9781439035665 ppt ch05
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
Chap05
Chap05
Plus de Andy Juan Sarango Veliz
Examen final de CCNA Routing y Switching Academia OW
Examen final de CCNA Routing y Switching Academia OW
Andy Juan Sarango Veliz
Criptología de empleo en el Esquema Nacional de Seguridad
Criptología de empleo en el Esquema Nacional de Seguridad
Andy Juan Sarango Veliz
Alfabetización Informática - 3. Navegador Web
Alfabetización Informática - 3. Navegador Web
Andy Juan Sarango Veliz
Alfabetización Informática - 2. Test de Conceptos Básicos
Alfabetización Informática - 2. Test de Conceptos Básicos
Andy Juan Sarango Veliz
Alfabetización Informática - 1. Conceptos Básicos
Alfabetización Informática - 1. Conceptos Básicos
Andy Juan Sarango Veliz
Gestión y Operación de la Ciberseguridad
Gestión y Operación de la Ciberseguridad
Andy Juan Sarango Veliz
Tecnologías de virtualización y despliegue de servicios
Tecnologías de virtualización y despliegue de servicios
Andy Juan Sarango Veliz
3. wordpress.org
3. wordpress.org
Andy Juan Sarango Veliz
2. wordpress.com
2. wordpress.com
Andy Juan Sarango Veliz
1. Introducción a Wordpress
1. Introducción a Wordpress
Andy Juan Sarango Veliz
Redes de Computadores: Un enfoque descendente 7.° Edición - Capítulo 9
Redes de Computadores: Un enfoque descendente 7.° Edición - Capítulo 9
Andy Juan Sarango Veliz
Análisis e Implementación de una Red "SDN" usando controladores "Open Source"
Análisis e Implementación de una Red "SDN" usando controladores "Open Source"
Andy Juan Sarango Veliz
Software Defined Radio - Capítulo 5: Modulación Digital I
Software Defined Radio - Capítulo 5: Modulación Digital I
Andy Juan Sarango Veliz
Software Defined Radio - Capítulo 4: Modulación FM
Software Defined Radio - Capítulo 4: Modulación FM
Andy Juan Sarango Veliz
Software Defined Radio - Capítulo 3: Modulación AM
Software Defined Radio - Capítulo 3: Modulación AM
Andy Juan Sarango Veliz
Software Defined Radio - Capítulo 2: GNU Radio Companion
Software Defined Radio - Capítulo 2: GNU Radio Companion
Andy Juan Sarango Veliz
Software Defined Radio - Capítulo 1: Introducción
Software Defined Radio - Capítulo 1: Introducción
Andy Juan Sarango Veliz
MAE-RAV-ROS Introducción a Ruteo Avanzado con MikroTik RouterOS v6.42.5.01
MAE-RAV-ROS Introducción a Ruteo Avanzado con MikroTik RouterOS v6.42.5.01
Andy Juan Sarango Veliz
Los cuatro desafíos de ciberseguridad más críticos de nuestra generación
Los cuatro desafíos de ciberseguridad más críticos de nuestra generación
Andy Juan Sarango Veliz
ITIL Foundation ITIL 4 Edition
ITIL Foundation ITIL 4 Edition
Andy Juan Sarango Veliz
Plus de Andy Juan Sarango Veliz
(20)
Examen final de CCNA Routing y Switching Academia OW
Examen final de CCNA Routing y Switching Academia OW
Criptología de empleo en el Esquema Nacional de Seguridad
Criptología de empleo en el Esquema Nacional de Seguridad
Alfabetización Informática - 3. Navegador Web
Alfabetización Informática - 3. Navegador Web
Alfabetización Informática - 2. Test de Conceptos Básicos
Alfabetización Informática - 2. Test de Conceptos Básicos
Alfabetización Informática - 1. Conceptos Básicos
Alfabetización Informática - 1. Conceptos Básicos
Gestión y Operación de la Ciberseguridad
Gestión y Operación de la Ciberseguridad
Tecnologías de virtualización y despliegue de servicios
Tecnologías de virtualización y despliegue de servicios
3. wordpress.org
3. wordpress.org
2. wordpress.com
2. wordpress.com
1. Introducción a Wordpress
1. Introducción a Wordpress
Redes de Computadores: Un enfoque descendente 7.° Edición - Capítulo 9
Redes de Computadores: Un enfoque descendente 7.° Edición - Capítulo 9
Análisis e Implementación de una Red "SDN" usando controladores "Open Source"
Análisis e Implementación de una Red "SDN" usando controladores "Open Source"
Software Defined Radio - Capítulo 5: Modulación Digital I
Software Defined Radio - Capítulo 5: Modulación Digital I
Software Defined Radio - Capítulo 4: Modulación FM
Software Defined Radio - Capítulo 4: Modulación FM
Software Defined Radio - Capítulo 3: Modulación AM
Software Defined Radio - Capítulo 3: Modulación AM
Software Defined Radio - Capítulo 2: GNU Radio Companion
Software Defined Radio - Capítulo 2: GNU Radio Companion
Software Defined Radio - Capítulo 1: Introducción
Software Defined Radio - Capítulo 1: Introducción
MAE-RAV-ROS Introducción a Ruteo Avanzado con MikroTik RouterOS v6.42.5.01
MAE-RAV-ROS Introducción a Ruteo Avanzado con MikroTik RouterOS v6.42.5.01
Los cuatro desafíos de ciberseguridad más críticos de nuestra generación
Los cuatro desafíos de ciberseguridad más críticos de nuestra generación
ITIL Foundation ITIL 4 Edition
ITIL Foundation ITIL 4 Edition
Dernier
NFPA 5000 2024 standard .
NFPA 5000 2024 standard .
DerechoLaboralIndivi
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
roncy bisnoi
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
ranjana rawat
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
roncy bisnoi
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
Asutosh Ranjan
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Christo Ananth
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
rknatarajan
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Suman Jyoti
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ranjana rawat
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
ManishPatel169454
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
M Maged Hegazy, LLM, MBA, CCP, P3O
Dernier
(20)
NFPA 5000 2024 standard .
NFPA 5000 2024 standard .
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
Control Structures: Part 1
1.
© 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 Statement 4.6 if else Selection Statement 4.7 while Repetition Statement 4.8 Formulating Algorithms: Case Study 1 (Counter- Controlled Repetition) 4.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) 4.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) 4.11 Compound Assignment Operators 4.12 Increment and Decrement Operators 4.13 Primitive Types 4.14 (Optional Case Study) Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures: Part 1
2.
© 2003 Prentice
Hall, Inc. All rights reserved. 2 4.1 Introduction • We learn about Control Structures – Structured-programming principle – Control structures help build and manipulate objects (Chapter 8)
3.
© 2003 Prentice
Hall, Inc. All rights reserved. 3 4.2 Algorithms • Algorithm – Series of actions in specific order • The actions executed • The order in which actions execute • Program control – Specifying the order in which actions execute • Control structures help specify this order
4.
© 2003 Prentice
Hall, Inc. All rights reserved. 4 4.3 Pseudocode • Pseudocode – Informal language for developing algorithms – Not executed on computers – Helps developers “think out” algorithms
5.
© 2003 Prentice
Hall, Inc. All rights reserved. 5 4.4 Control Structures • Sequential execution – Program statements execute one after the other • Transfer of control – Three control statements can specify order of statements • Sequence structure • Selection structure • Repetition structure • Activity diagram – Models the workflow • Action-state symbols • Transition arrows
6.
© 2003 Prentice
Hall, Inc. All rights reserved. 6 Fig 4.1 Sequence structure activity diagram. add grade to total add 1 to counter Corresponding Java statement: total = total + grade; Corresponding Java statement: counter = counter + 1;
7.
© 2003 Prentice
Hall, Inc. All rights reserved. 7 Java Keywords abstract assert boolean break byte case catch char class continue default do double else extends final finally float for if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while Keywords that are reserved, but not currently used const goto Fig. 4.2 Java keywords.
8.
© 2003 Prentice
Hall, Inc. All rights reserved. 8 4.4 Control Structures • Java has a sequence structure “built-in” • Java provides three selection structures – if – If…else – switch • Java provides three repetition structures – while – do…while – do • Each of these words is a Java keyword
9.
© 2003 Prentice
Hall, Inc. All rights reserved. 9 4.5 if Single-Selection Statement • Single-entry/single-exit control structure • Perform action only when condition is true • Action/decision programming model
10.
© 2003 Prentice
Hall, Inc. All rights reserved. 10 Fig 4.3 if single-selections statement activity diagram. [grade >= 60] [grade < 60] print “Passed”
11.
© 2003 Prentice
Hall, Inc. All rights reserved. 11 4.6 if…else Selection Statement • Perform action only when condition is true • Perform different specified action when condition is false • Conditional operator (?:) • Nested if…else selection structures
12.
© 2003 Prentice
Hall, Inc. All rights reserved. 12 Fig 4.4 if…else double-selections statement activity diagram. [grade >= 60][grade < 60] print “Failed” print “Passed”
13.
© 2003 Prentice
Hall, Inc. All rights reserved. 13 4.7 while Repetition Statement • Repeat action while condition remains true
14.
© 2003 Prentice
Hall, Inc. All rights reserved. 14 Fig 4.5 while repetition statement activity diagram. [product <= 1000] [product > 1000] double product value merge decision Corresponding Java statement: product = 2 * product;
15.
© 2003 Prentice
Hall, Inc. All rights reserved. 15 4.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) • Counter – Variable that controls number of times set of statements executes • Average1.java calculates grade averages – uses counters to control repetition
16.
© 2003 Prentice
Hall, Inc. All rights reserved. 16 Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Fig. 4.6 Pseudocode algorithm that uses counter- controlled repetition to solve the class-average problem.
17.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 17 Average1.java gradeCounter Line 21 1 // Fig. 4.7: Average1.java 2 // Class-average program with counter-controlled repetition. 3 import javax.swing.JOptionPane; 4 5 public class Average1 { 6 7 public static void main( String args[] ) 8 { 9 int total; // sum of grades input by user 10 int gradeCounter; // number of grade to be entered next 11 int grade; // grade value 12 int average; // average of grades 13 14 String gradeString; // grade typed by user 15 16 // initialization phase 17 total = 0; // initialize total 18 gradeCounter = 1; // initialize loop counter 19 20 // processing phase 21 while ( gradeCounter <= 10 ) { // loop 10 times 22 23 // prompt for input and read grade from user 24 gradeString = JOptionPane.showInputDialog( 25 "Enter integer grade: " ); 26 27 // convert gradeString to int 28 grade = Integer.parseInt( gradeString ); 29 Declare variables; gradeCounter is the counter Continue looping as long as gradeCounter is less than or equal to 10
18.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 18 Average1.java 30 total = total + grade; // add grade to total 31 gradeCounter = gradeCounter + 1; // increment counter 32 33 } // end while 34 35 // termination phase 36 average = total / 10; // integer division 37 38 // display average of exam grades 39 JOptionPane.showMessageDialog( null, "Class average is " + average, 40 "Class Average", JOptionPane.INFORMATION_MESSAGE ); 41 42 System.exit( 0 ); // terminate the program 43 44 } // end main 45 46 } // end class Average1
19.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 19 Average1.java
20.
© 2003 Prentice
Hall, Inc. All rights reserved. 204.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) • Sentinel value – Used to indicated the end of data entry • Average2.java has indefinite repetition – User enters sentinel value (-1) to end repetition
21.
© 2003 Prentice
Hall, Inc. All rights reserved. 21 Initialize total to zero Initialize counter to zero Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “No grades were entered” Fig. 4.8 Class-average problem pseudocode algorithm with sentinel-controlled repetition.
22.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 22 Average2.java 1 // Fig. 4.9: Average2.java 2 // Class-average program with sentinel-controlled repetition. 3 import java.text.DecimalFormat; // class to format numbers 4 import javax.swing.JOptionPane; 5 6 public class Average2 { 7 8 public static void main( String args[] ) 9 { 10 int total; // sum of grades 11 int gradeCounter; // number of grades entered 12 int grade; // grade value 13 14 double average; // number with decimal point for average 15 16 String gradeString; // grade typed by user 17 18 // initialization phase 19 total = 0; // initialize total 20 gradeCounter = 0; // initialize loop counter 21 22 // processing phase 23 // get first grade from user 24 gradeString = JOptionPane.showInputDialog( 25 "Enter Integer Grade or -1 to Quit:" ); 26 27 // convert gradeString to int 28 grade = Integer.parseInt( gradeString ); 29
23.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 23 Average2.java Line 31 Line 45 30 // loop until sentinel value read from user 31 while ( grade != -1 ) { 32 total = total + grade; // add grade to total 33 gradeCounter = gradeCounter + 1; // increment counter 34 35 // get next grade from user 36 gradeString = JOptionPane.showInputDialog( 37 "Enter Integer Grade or -1 to Quit:" ); 38 39 // convert gradeString to int 40 grade = Integer.parseInt( gradeString ); 41 42 } // end while 43 44 // termination phase 45 DecimalFormat twoDigits = new DecimalFormat( "0.00" ); 46 47 // if user entered at least one grade... 48 if ( gradeCounter != 0 ) { 49 50 // calculate average of all grades entered 51 average = (double) total / gradeCounter; 52 53 // display average with two digits of precision 54 JOptionPane.showMessageDialog( null, 55 "Class average is " + twoDigits.format( average ), 56 "Class Average", JOptionPane.INFORMATION_MESSAGE ); 57 58 } // end if part of if...else 59 loop until gradeCounter equals sentinel value (-1) Format numbers to nearest hundredth
24.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 24 Average2.java 60 else // if no grades entered, output appropriate message 61 JOptionPane.showMessageDialog( null, "No grades were entered", 62 "Class Average", JOptionPane.INFORMATION_MESSAGE ); 63 64 System.exit( 0 ); // terminate application 65 66 } // end main 67 68 } // end class Average2
25.
© 2003 Prentice
Hall, Inc. All rights reserved. 254.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) • Nested control structures
26.
© 2003 Prentice
Hall, Inc. All rights reserved. 26 Initialize passes to zero Initialize failures to zero Initialize student to one While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition” Fig 4.10 Pseudocode for examination-results problem.
27.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 27 Analysis.java Line 19 Line 29 1 // Fig. 4.11: Analysis.java 2 // Analysis of examination results. 3 import javax.swing.JOptionPane; 4 5 public class Analysis { 6 7 public static void main( String args[] ) 8 { 9 // initializing variables in declarations 10 int passes = 0; // number of passes 11 int failures = 0; // number of failures 12 int studentCounter = 1; // student counter 13 int result; // one exam result 14 15 String input; // user-entered value 16 String output; // output string 17 18 // process 10 students using counter-controlled loop 19 while ( studentCounter <= 10 ) { 20 21 // prompt user for input and obtain value from user 22 input = JOptionPane.showInputDialog( 23 "Enter result (1 = pass, 2 = fail)" ); 24 25 // convert result to int 26 result = Integer.parseInt( input ); 27 28 // if result 1, increment passes; if...else nested in while 29 if ( result == 1 ) 30 passes = passes + 1; Loop until student counter is greater than 10 Nested control structure
28.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 28 Analysis.java 31 32 else // if result not 1, increment failures 33 failures = failures + 1; 34 35 // increment studentCounter so loop eventually terminates 36 studentCounter = studentCounter + 1; 37 38 } // end while 39 40 // termination phase; prepare and display results 41 output = "Passed: " + passes + "nFailed: " + failures; 42 43 // determine whether more than 8 students passed 44 if ( passes > 8 ) 45 output = output + "nRaise Tuition"; 46 47 JOptionPane.showMessageDialog( null, output, 48 "Analysis of Examination Results", 49 JOptionPane.INFORMATION_MESSAGE ); 50 51 System.exit( 0 ); // terminate application 52 53 } // end main 54 55 } // end class Analysis
29.
© 2003 Prentice
Hall, Inc. All rights reserved. 29 4.11 Compound Assignment Operators • Assignment Operators – Abbreviate assignment expressions – Any statement of form • variable = variable operator expression; – Can be written as • variable operator= expression; – e.g., addition assignment operator += • c = c + 3 – can be written as • c += 3
30.
© 2003 Prentice
Hall, Inc. All rights reserved. 30 Assignment operator Sample expression Explanation Assigns Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; += c += 7 c = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g Fig. 4.12 Arithmetic assignment operators.
31.
© 2003 Prentice
Hall, Inc. All rights reserved. 31 4.12 Increment and Decrement Operators • Unary increment operator (++) – Increment variable’s value by 1 • Unary decrement operator (--) – Decrement variable’s value by 1 • Preincrement / predecrement operator • Post-increment / post-decrement operator
32.
© 2003 Prentice
Hall, Inc. All rights reserved. 32 Operator Called Sample expression Explanation ++ preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides. ++ postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1. -- predecrement --b Decrement b by 1, then use the new value of b in the expression in which b resides. -- postdecrement b-- Use the current value of b in the expression in which b resides, then decrement b by 1. Fig. 4.13 The increment and decrement operators.
33.
© 2003 Prentice
Hall, Inc. All rights reserved. Outline 33 Increment.java Line 13 postincrement Line 21 preincrement 1 // Fig. 4.14: Increment.java 2 // Preincrementing and postincrementing operators. 3 4 public class Increment { 5 6 public static void main( String args[] ) 7 { 8 int c; 9 10 // demonstrate postincrement 11 c = 5; // assign 5 to c 12 System.out.println( c ); // print 5 13 System.out.println( c++ ); // print 5 then postincrement 14 System.out.println( c ); // print 6 15 16 System.out.println(); // skip a line 17 18 // demonstrate preincrement 19 c = 5; // assign 5 to c 20 System.out.println( c ); // print 5 21 System.out.println( ++c ); // preincrement then print 6 22 System.out.println( c ); // print 6 23 24 } // end main 25 26 } // end class Increment 5 5 6 5 6 6 Line 13 postincrements c Line 21 preincrements c
34.
© 2003 Prentice
Hall, Inc. All rights reserved. 34 Operators Associativity Type ++ -- right to left unary postfix ++ -- + - (type) right to left unary * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality ?: right to left conditional = += -= *= /= %= right to left assignment Fig. 4.15 Precedence and associativity of the operators discussed so far.
35.
© 2003 Prentice
Hall, Inc. All rights reserved. 35 4.13 Primitive Types • Primitive types – “building blocks” for more complicated types • Java is strongly typed – All variables in a Java program must have a type • Java primitive types – portable across computer platforms that support Java
36.
© 2003 Prentice
Hall, Inc. All rights reserved. 36 Type Size in bits Values Standard boolean true or false [Note: The representation of a boolean is specific to the Java Virtual Machine on each computer platform.] char 16 'u0000' to 'uFFFF' (0 to 65535) (ISO Unicode character set) byte 8 –128 to +127 (–27 to 27 – 1) short 16 –32,768 to +32,767 (–215 to 215 – 1) int 32 –2,147,483,648 to +2,147,483,647 (–231 to 231 – 1) long 64 –9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (–263 to 263 – 1) float 32 Negative range: –3.4028234663852886E+38 to –1.40129846432481707e–45 Positive range: 1.40129846432481707e–45 to 3.4028234663852886E+38 (IEEE 754 floating point) double 64 Negative range: –1.7976931348623157E+308 to –4.94065645841246544e–324 Positive range: 4.94065645841246544e–324 to 1.7976931348623157E+308 (IEEE 754 floating point) Fig. 4.16The Java primitive types.
37.
© 2003 Prentice
Hall, Inc. All rights reserved. 37 4.14 (Optional Case Study) Thinking About Objects: Identifying Class Attributes • Classes have attributes (data) – Implemented in Java programs as variables – Attributes of real-world objects • Radio (object) – Station setting, volume setting, AM or FM (attributes) • Identify attributes – Look for descriptive words and phrases in problem statement – Each identified word and phrase is a candidate attribute • e.g., “the elevator is moving” – “is moving” corresponds to boolean attribute moving • e.g., “the elevator takes five seconds to travel between floors” – corresponds to int attribute travelTime • int travelTime = 5; (in Java)
38.
© 2003 Prentice
Hall, Inc. All rights reserved. 38 Class Descriptive words and phrases ElevatorShaft [no descriptive words or phrases] Elevator moving summoned current floor destination floor capacity of only one person five seconds to travel between floors Person unique waiting / moving current floor Floor first or second; capacity for only one person FloorButton pressed / reset ElevatorButton pressed / reset FloorDoor door closed / door open ElevatorDoor door closed / door open Bell [no descriptive words or phrases] Light turned on / turned off Fig. 4.17 Descriptive words and phrases from problem statement.
39.
© 2003 Prentice
Hall, Inc. All rights reserved. 39 4.14 (Optional Case Study) Thinking About Objects: Identifying Class Attributes (Cont.) • UML class diagram – Class attributes are place in the middle compartment – Attributes are written language independently • e.g., attribute open of class ElevatorDoor – open : Boolean = false • May be coded in Java as – boolean open = false;
40.
© 2003 Prentice
Hall, Inc. All rights reserved. 40 Fig. 4.18 Classes with attributes. Person ID : Integer moving : Boolean = true currentFloor : Integer Elevator moving : Boolean = false summoned : Boolean = false currentFloor : Integer = 1 destinationFloor : Integer = 2 capacity : Integer = 1 travelTime : Integer = 5 ElevatorShaft Floor floorNumber : Integer capacity : Integer = 1 ElevatorButton pressed : Boolean = false FloorButton pressed : Boolean = false ElevatorDoor open : Boolean = false Light lightOn : Boolean = false Bell FloorDoor open : Boolean = false
Télécharger maintenant