Introduction to C++ Programming Language

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
true (any other value but zero) false (zero) expression Statement 2
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
UNIT II Decision Making And Branching Decision Making And Looping
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Computer Science Selection Structures.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Introduction to C++ Programming Language
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Selection—Making Decisions
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Control Structures.
Chapter 4: Control Structures I
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Flow Control Statements
Chapter 4: Control Structures I (Selection)
Conditional Statements
Control Structure Chapter 3.
Topics discussed in this section:
Computer Programming Basics
Selection—Making Decisions
Topics discussed in this section:
Control Structure.
Presentation transcript:

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

CHAPTER 5 SELECTION - MAKING DECISION

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.

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.

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

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

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++  !

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

Logical Operators Truth Table

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

Relational Operators

Logical operator complements

Two-way decision logic

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

A simple if...else statement

Compound statements in an if...else

Complemented if...then statements

A null else statement

A null if statement

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

Dangling else

Dangling else solution

Conditional expression

switch decision logic

switch statement

switch flow

switch results

A switch with break statements

The else…if for Program 5-9

Comparison between if and switch