21
June 23, 2022 1 Algorithm and Control Diversion Handle Binary Fork Multiple Choice Iterative /loop CS220 Introduction to CS220 Introduction to Computer Science Computer Science Algorithm and Control Algorithm and Control Software and Computer Science at Software and Computer Science at APU APU Basic Flow of Control and Statements Basic Flow of Control and Statements Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www. apu . edu/clas/computerscience/

May 19, 2015 1 Algorithm and Control Diversion Handle Binary Fork Multiple Choice Iterative /loop CS220 Introduction to Computer Science Algorithm

Embed Size (px)

Citation preview

Page 1: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20231

Algorithm and Control Diversion Handle Binary Fork Multiple Choice Iterative /loop

CS220 Introduction to Computer ScienceCS220 Introduction to Computer ScienceAlgorithm and ControlAlgorithm and Control

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 2: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20232

Algorithm and Control Daily Routine Algorithm

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 3: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20233

Decision Evaluation C++ allows to evaluate conditions in order to do different operations (decision)

Evaluate

Decision Making

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 4: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20234

Decision Evaluation

You can test for a condition being true or false, for various values of an expression.

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

C++ allows to evaluate conditions in order to do different operations (decision)

AND OR Not

&& || !

Both true--> T Either true -->T True --> F

Equal == not equal != non-zero

Logic

Logic

Page 5: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20235

Algorithm and flow of control Diversion Handle Binary Fork Multiple Choice Iterative / Loop

CS220 Introduction to Computer ScienceCS220 Introduction to Computer ScienceAlgorithm and ControlAlgorithm and Control

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 6: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20236

Diversion Handle

Walking

Barking

Calm down

Yes, Scared

No

Walking;if (dog barking ) { be more careful keep watching out

}Continue to walk

Sometimes we have to be interrupted by some sudden event, and after we handle it and continue. Obviously, this is the case with If-statement

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 7: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20237

Diversion Handle an example In Visual GUI Check Box, we will decide to offer discount or not, just depends on the checked status

‘ After Calculate Button is clicked, following will be done

int quantity = Int.Parse(QTextBox.Text);double = double.Parse(PTextBox.Text);double extPrice;double disc15Single = 0.0; // Initializedouble finalPrice;

extPriceSingle = quantityInteger * priceSingle;if (DiscountCheckBox.Checked) { disc15Single = 0.15 * extPriceSingle;} finalPrice = extPriceSingle - disc15Single;

ETextBox.Text = extPriceSingle.ToString("C")DTextBox.Text = disc15Single.ToString("C")FTextBox.Text = finalPrice.ToString("C")

if (discountCheckBox.Checked) { … }

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Page 8: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20238

Algorithm and Control Diversion Evaluation Binary Fork Multiple Choice Iterative / Loop

CS220 Introduction to Computer ScienceCS220 Introduction to Computer ScienceAlgorithm and ControlAlgorithm and Control

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 9: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 20239

Life is uncertain Binary Fork Facing a fork, you have to choose either, then merge and continue your work

Seeking

L/R

turn right

RightLeft

turn left

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 10: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202310

Through Check Box, we will decide to offer discount or not, just depends on the checked status

If discountCheckBox.Checked Then

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

‘ After Calculate Button is clicked, following will be done

int quantity = Int.Parse(QTextBox.Text);double = double.Parse(PTextBox.Text);double extPrice, disc15Single, finalPrice;

extPriceSingle = quantityInteger * priceSingle;if (DiscountCheckBox.Checked) { disc15Single = 0.15 * extPriceSingle;}else { disc15Single = 0.0; }finalPrice = extPriceSingle - disc15Single;

ETextBox.Text = extPriceSingle.ToString("C")DTextBox.Text = disc15Single.ToString("C")FTextBox.Text = finalPrice.ToString("C")

Life is uncertain Binary Fork

Page 11: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202311

Algorithm and Control Diversion Handle Binary Fork Multiple Choice Iterative / Loop

CS220 Introduction to Computer ScienceCS220 Introduction to Computer ScienceAlgorithm and ControlAlgorithm and Control

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 12: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202312

Facing a multi-choice, you have to choose one from many, then merge and continue your work

Seeking

LCR

turn right

RightLeft

turn left straight

Central

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Multi-Choice Grading Scale

Page 13: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202313

Multi-Choice Grading Scale switch and if statement can help us to decide the grading for students’ exam scale

int score; char yourGrade; … … if (score >= 90) yourGrade = ‘A’; else if (score >=80) yourGrade = ‘B’; else if (score >=70) yourGrade = ‘C’; else if (score >=60) yourGrade = ‘D’; else yourGrade = ‘F’ ;

April 18, 2023

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

int score;char yourGrade; … …switch (yourGrade) { case ‘A’: score is 90+

case ‘B’: score is 80+

case ‘C’: score is 70+

case ‘D’: score is 60+

default: score is below 60 }

Page 14: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202314

Algorithm and Control Diversion Handle Binary Fork Multiple Choice Iterative / Loop

CS220 Introduction to Computer ScienceCS220 Introduction to Computer ScienceAlgorithm and ControlAlgorithm and Control

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 15: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202315

Algorithm and Control Daily Routine Algorithm

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 16: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202316

For / Loop statement Basic concept about loop a loop or iterative control structure stands for life long process.

for (fclass; class continues; nclass) { Goto class;}

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 17: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202317

while / loop statement Basic concept about loop

First class;while (class continues) { Goto class; Next class;}

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 18: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202318

do while / loop statement Basic concept about loop

first class;do { goto class; next class;} while (class continues)

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Page 19: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202319

while statement guards first while statement

Do Until pass_is_valid keep_tryingLoop

Do While Pass_is_valid keep_doingLoop

Arrogant Securitywhile (check first) {

keep_doing_something

}

Check it first

SSS: Strict to be safe & securedWWW: Too strict to have a try

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Other new forms

Page 20: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202320

do while statement Check-out last in do-while statement

Do keep_tryingLoop Until Satisfied

Do keep_enjoyingLoop While Want

Warm Waitress do {

keep_doing_something

} while (no check) Check it last

SSS: loose enough to have try WWW: Too loose to be safe & secured

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

Other new forms

Page 21: May 19, 2015 1  Algorithm and Control  Diversion Handle  Binary Fork  Multiple Choice  Iterative /loop CS220 Introduction to Computer Science Algorithm

April 18, 202321

That is all

Thank you very much!

Questions?

Software and Computer Science at APUSoftware and Computer Science at APUBasic Flow of Control and Statements Basic Flow of Control and Statements

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/