Computer Programming Basics

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

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:
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
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
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
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.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection 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.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
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.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
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.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
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.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Introduction to C++ Programming Language
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
Control Structures.
Chapter 4: Control Structures I
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Conditional Statements
Control Structure Chapter 3.
Topics discussed in this section:
Selection—Making Decisions
Topics discussed in this section:
Control Structure.
Presentation transcript:

Computer Programming Basics Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea

CHAPTER 5 SELECTION - MAKING DECISION

Serial execution of code Program begins Do A Do B … Do Z Program ends  boring! inefficient! not very useful! e.g., movie vs. computer game

Conditionally Changing Program Flow It would be nice to be able to change which statements ran and when, depending on the circumstances.

Selection Statement Provide a means to conditionally execute sections of code. Selection statements

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.

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.

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” In c++ : “&&” Or “true” or “false”  “true” “false” or “false”  “false” In c++  “||” Not “not” “true”  “false” In c++  “!”

Usage int a = 4, b = 0; a && b //false a || b //true !a //false !b //true (a+b) //true (a-4) //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