Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ Programming Language

Similar presentations


Presentation on theme: "Introduction to C++ Programming Language"— Presentation transcript:

1 Introduction to C++ Programming Language
Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea

2 CHAPTER 5 SELECTION - MAKING DECISION

3 Making Decision Serial execution of code  boring! inefficient! not very useful! ex) movie vs. computer game It would be nice to be able to change which statements ran and when, depending on the circumstances. The selection statements They provide a means to conditionally execute sections of code.

4 Logical Data 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.

5 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

6 Arithmetic Scale Example
int a = 4;  true char name = 3  true int b = 0;  false bool isRunning = false;  false 4  true 0  false

7 Logical Operator And True and true  true True and false  false
Operator in C++  && Or True or false  true False or false  false Operator in c++  || Not Not true  false Operator in C++  !

8 Usage int a = 4, b = 0; a && b  false a || b  true !a  false

9 Logical Operators Truth Table

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

11

12 Relational Operators

13 Logical operator complements

14

15 Two-way decision logic

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

17 A simple if...else statement

18 Compound statements in an if...else

19 Complemented if...then statements

20 A null else statement

21 A null if statement

22

23 else is always paired with the most recent, unpaired if
Nested if statements else is always paired with the most recent, unpaired if

24

25 Dangling else

26 Dangling else solution

27 Conditional expression

28 switch decision logic

29 switch statement

30 switch flow

31 switch results

32 A switch with break statements

33 The else…if for Program 5-9

34

35 Comparison between if and switch


Download ppt "Introduction to C++ Programming Language"

Similar presentations


Ads by Google