seungkyu lee - khu.ac.krcvlab.khu.ac.kr/lecture7.pdf · 2016. 10. 18. · conditional change...

37
[CSE10200] Programming Basis (프로그래밍 기초) Chapter 5 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Upload: others

Post on 17-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

[CSE10200] Programming Basis

(프로그래밍 기초)

Chapter 5

Seungkyu Lee

Assistant Professor, Dept. of Computer Engineering

Kyung Hee University

Page 2: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Serial execution of code

boring! inefficient! not very useful! e.g., computer game

Program begins

Do A

Do B

Do Z

… Program ends

Page 3: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Conditionally Changing Program Flow

It would be nice to be able to change which statements ran

and when, depending on the circumstances.

Page 4: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Selection Statement

Selection

statements

Provide a means to conditionally execute

sections of code.

Page 5: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Conditional Change Selection

We need comparisons!

Ex1) if the value in variable “num” is larger than 4, then execute

statement 1. Otherwise, execute statement 2.

Ex2) if the value in variable “num1” is same as that in variable

“num2”, then execute statement 1. Otherwise, execute statement

2.

Ex3) if the value in variable “num1” is either 1 or 2, then execute

statement 1. Otherwise, execute statement 2.

Page 6: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

How to Select? Simple yes/no decision can do everything!

In computer science, we use true/false

How to determine true/false? Logical data and logical operator

If logical data satisfies something, we consider it’s true. Otherwise, it’s false.

Page 7: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

True and False for the Arithmetic Scale

In C++

If a value is zero, it can be used as the logical value false.

If a value is not zero, it can be used as the logical value

true.

Zero <===> False

Nonzero <===> True

Page 8: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Arithmetic Scale Example

• int a = 4; true

• char name = 3 true

• int b = 0; false

• bool isRunning = false; false

• 4 true

• 0 false

Page 9: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Logical Operator • And

– “true” and “true” “true”

– “true” and “false” “false”

– In c++ : “&&”

• Or

– “true” or “false” “true”

– “false” or “false” “false”

– In c++ “||”

• Not

– “not” “true” “false”

– In c++ “!”

Page 10: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Usage

int a = 4, b = 0;

a && b //false

a || b //true

!a //false

!b //true

(a+b) //true

(a-4) //false

Page 11: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Logical Operators Truth Table

Page 12: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Short-Circuit Methods for “and” and “or”

Page 13: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than
Page 14: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Relational Operators

Page 15: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Logical operator complements

Page 16: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than
Page 17: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Two-way decision logic

Page 18: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Two-way decision logic : “if...else” logic flow

Page 19: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

A simple if...else statement

Page 20: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Compound statements in an if...else

Page 21: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Complemented if...then statements

Page 22: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

A null else statement

Page 23: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

A null if statement

Page 24: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than
Page 25: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Nested if statements

else is always paired with the

most recent, unpaired if

Page 26: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than
Page 27: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Dangling else

Page 28: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Dangling else solution

Page 29: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Conditional expression

Page 30: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

switch decision logic

Page 31: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

switch statement

Page 32: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

switch flow

Page 33: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

switch results

Page 34: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

A switch with break statements

Page 35: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

The else…if for Program 5-9

Page 36: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than
Page 37: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture7.pdf · 2016. 10. 18. · Conditional Change Selection We need comparisons! Ex1) if the value in variable “num” is larger than

Comparison between if and switch