UEE1302(1102) F10: Introduction to Computers and Programming
|
|
- Hillary Sherman
- 6 years ago
- Views:
Transcription
1 Computational Intelligence on Automation NCTU UEE1302(1102) F10: Introduction to Computers and Programming Programming Lecture 02 Flow of Control (I): Boolean Expression and Selection Learning Objectives You should be able to describe: Relational Expressions Logical Expressions if- Statement Nested if and if Chain Statements switch Statement Common Programming Errors PRO_02 PROF. HUNG-PIN(CHARLES) WEN 2 Control Structures Flow of Control: the order in which a program s statements are executed Normal flow is sequential Selection and Repetition statements allow programmer to alter normal flow Selection: selects a particular statement to be executed next selection is from a well-defined set Repetition: allows a set of statements to be repeated statement 1 statement 2 statement N Flow of Execution false stmt_no bool_expr true stmt_yes true stop_cond false stmt_loop (A) sequence (B) selection (C) repetition PRO_02 PROF. HUNG-PIN(CHARLES) WEN 3 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 4
2 Relational Expressions All computers are able to compare numbers can be used to create an intelligence-like facility Relational Expressions: expressions used to compare operands format: a relational operator connecting 2 variables and/or constant operands examples of valid relational expressions: age > 40 length <= 50 flag == done PRO_02 PROF. HUNG-PIN(CHARLES) WEN 5 Math Relational Operators (1/2) meaning C++ notation Example = Equal to == 2*x == 7*y Not equal to!= ans!= n > Greater than > income > Greater than or equal to >= age >= 18 < Less than < count < base + 10 Less than or equal to <= height <= 170 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 6 Relational Operators (2/2) Relational Expressions (conditions): are evaluated to yield a numerical result condition that is true evaluates to 1 condition that is false evaluates to 0 Example: The relationship 2.0>3.3 is always false, therefore the expression has a value of 0 Logical Operators (1/2) More complex conditions can be created using basic logical operations AND symbol: &&, OR symbol : NOT symbol :! AND operator, &&: Used with 2 simple expressions Example: (age > 40) && (term < 10) Compound condition is true (has value of 1) only if age > 40 and term < 10 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 7 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 8
3 Logical Operators (2/2) OR operator, : Used with two simple expressions Example: (age > 40) (term < 10) Compound condition is true if age > 40 or if term < 10 or if both conditions are true NOT operator,!: Changes an expression to its opposite state If expr_a is true, then!expr_a is false Precedence of Operations Operator!(unary),, ++, Associativity right to left *, /, % left to right +, left to right <, <=, >, >= left to right ==,!= left to right && left to right left to right =, +=, =, *=, /= right to left PRO_02 PROF. HUNG-PIN(CHARLES) WEN 9 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 10 Precedence Examples Arithmetic before logical x + 1 > 2 x + 1 < -3 means (x + 1) > 2 (x + 1) < -3 Short-circuit evaluation (x >= 0) && (y > 1) Be careful with increment operators! Ex:(x > 1) && (y++) Integers as Boolean values All non-zero values true Zero value false PRO_02 PROF. HUNG-PIN(CHARLES) WEN 11 DeMorgan's Laws Two propositional logic rules negation of conjunction : (A B) = A B negation of disjunction : (A B) = A B Suppose A and B are logical expressions!(a && B) (!A) (!B)!(A B) (!A) && (!B) Principle of double negation!(!a) A Example:!(YourAge < Min YourAge > Max) (YourAge >= Min && YourAge <= Max) PRO_02 PROF. HUNG-PIN(CHARLES) WEN 12
4 Selection (I): One-Way if Example of One-Way if false decision_ maker true action_stmt Formal syntax of one-way selection : if (decision_maker) //no ; here action_stmt; decision_maker : is a logical expression decides whether to execute the action statement if decision_maker is true, execute action_stmt if decision_maker is false, bypass action_stmt Example 1: if (score >= 90) grade = A ; Example 2: absolute value if (ivar < 0) ivar = -ivar; cout << absolute value = << ivar << endl; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 13 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 14 Selection (I): Two-Way if- Choice of two alternate statements based on condition expression Formal syntax : if (decision_maker) // no semicolon here action_stmt_yes; // no semicolon here action_stmt_no; decision_maker: decide which one of two statements to run if the evaluation is true, run action_stmt_yes if the evaluation is false, run action_stmt_no Example of Two-Way if- Example 1: pass or fail if (score >= 60) cout << Pass << endl; cout << Fail << endl; Example 2: overtime payment if (hours > 40) pay = rate* *rate*(hours-40); pay = rate*hours; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 15 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 16
5 Compound Statement What if we want to execute multiple statements in action? Compound statement ( a.k.a. a block of statements): { statement 1; statement 2; statement n; } a compound statement is treated as a single statement Example of Compound Statements if (age >= 18) { } { } cout << "Eligible to vote." << endl; cout << "No longer a minor." << endl; cout << "Not eligible to vote. << endl; cout << "Still a minor." << endl; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 17 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 18 Nested if Nesting: one control statement in another An is associated with the most recent if that has not been paired with an Example 1: Tax Computation if (income > ) tax = income * 0.50; if (income > ) tax = income * 0.30; if (income > ) tax = income * 0.20; tax = income * 0.10; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 19 p21 Avoid Excessive Indentation Use if chain instead of nested if Example 2: Grading if (score >= 80.0) grade = A ; if (score >= 70.0) grade = B ; if (score >= 60.0) grade = C ; grade = F ; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 20
6 Compare if Chain and Multiple if Question: What s the difference?? if Chain if (month == 1) cout<< Jan <<endl; if (month == 2) cout<< Feb <<endl; if (month == 3) cout<< Mar <<endl; eles if (month == 11) cout<< Nov <<endl; cout<< Dec <<endl; Multiple if if (month == 1) cout<< Jan <<endl; if (month == 2) cout<< Feb <<endl; if (month == 3) cout<< Mar <<endl; if (month == 11) cout<< Nov <<endl; if (month == 12) cout<< Dec <<endl; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 21 Common Pitfalls on if- Operator "=" vs. operator "==" "assignment versus equality" Example: What s the problem?? if (age = 20) cout<< Happy 20-year old birthday <<endl; Using = instead of == in the if- statement causes the most difficult errors Hard to debug due to no error message Nested if statements needs braces {} to clearly indicate the desired structure PRO_02 PROF. HUNG-PIN(CHARLES) WEN 22 Selection (II):?: Selection (III): switch (1/2) Conditional operator (?:?:) takes three arguments (ternary) equivalent to if- Syntax for the conditional operator: var = expr_1? expr_2 : expr_3; if expr_1 is true, assign expr_2 to var if expr_1 is false, assign expr_3 to var Ex: bool taxable = (age >=18)? true:false; Exercise: how to rewrite the tax computation example by using (?:) PRO_02 PROF. HUNG-PIN(CHARLES) WEN 23 A new stmt for controlling multiple branches Syntax format: switch ( control_expr ) { // start of compound statement case literal_1 : //<-terminated with a colon statement_1; statement_2; case literal_2 : //<-terminated with a colon statement_3; default : //<-terminated with a colon statement_n; } // end of switch and compound stmt PRO_02 PROF. HUNG-PIN(CHARLES) WEN 24
7 Selection (III): switch (2/2) Rewrite Month Example Four new keywords used: switch, case, default and break Function: control_expr following switch is evaluated must compare to an literal Result compared sequentially to alternative case values until a match is found Statements following matched case are executed When break reached, switch terminates If no match found, run default statement block Using if Chain if (month == 1) cout<< Jan <<endl; if (month == 2) cout<< Feb <<endl; if (month == 3) cout<< Mar <<endl; eles if (month == 11) cout<< Nov <<endl; cout<< Dec <<endl; Using switch switch (month) { case 1: cout<< Jan <<endl; case 2: cout<< Feb <<endl; case 11: cout<< Nov <<endl; default: cout<< Dec <<endl; } PRO_02 PROF. HUNG-PIN(CHARLES) WEN 25 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 26 Common Pitfalls on switch Forgetting No compiler error Execution simply falls through other cases until Ex: if month is equal to 13 in the previous example, screen still displays " Dec" Best usage: Menus Provides clearer big-picture view Shows menu structure effectively Each branch is one menu choice switch Menu Example switch (response) { case 1 : // Execute menu option 1 case 2 : // Execute menu option 2 case 3 : // Execute menu option 3 default: cerr << "Please enter a valid response."; } Good habit to enumerate all known cases and prompt by cerr if unknown case occurs PRO_02 PROF. HUNG-PIN(CHARLES) WEN 27 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 28
8 Cable Bill Exercise This programming exercise calculates a customer s bill for a local cable company Residential customer rates: Bill processing fee: $4.50 Basic service fee: $20.50 Premium channel: $7.50 per channel Business customer rates: Bill processing fee: $15.00 Basic service fee: $75.00 for first 10 connections and $5.00 for each additional connection Premium channel cost: $50.00 per channel for any number of connections Requirements Ask user for account number and customer code Assume R or r stands for residential customer and B or b stands for business customer PRO_02 PROF. HUNG-PIN(CHARLES) WEN 29 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 30 Input: Input and Output Customer account number Customer code Number of premium channels For business customers, number of basic service connections Output: Customer s account number Billing amount Program Analysis (1/2) The purpose of the program is to calculate and print billing amount Calculating the billing amount requires: Customer for whom the billing amount is calculated (residential or business) Number of premium channels to which the customer subscribes For a business customer, you need: Number of basic service connections Number of premium channels PRO_02 PROF. HUNG-PIN(CHARLES) WEN 31 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 32
9 Program Analysis (2/2) Data needed to calculate the bill, such as bill processing fees and the cost of a premium channel, are known quantities The program should print the billing amount to two decimal places PRO_02 PROF. HUNG-PIN(CHARLES) WEN 33 Algorithm Design Set precision to two decimal places Prompt user for account number and customer type If customer type is R or r Prompt user for number of premium channels Compute and print the bill If customer type is B or b Prompt user for number of basic service connections and number of premium channels Compute and print the bill PRO_02 PROF. HUNG-PIN(CHARLES) WEN 34 Variables // variable to store customer s account # int accountnumber; // variable to store customer code char customertype; // variable to store # of subscribed // premium channels int numofpremchannels; // variable to store # of basi // connections int numofbasicservconn; Named Constants // for residential customers const double RES_BILL_PROC_FEES = 4.50; const double RES_BASIC_SERV_COST = 20.50; const double RES_COST_PREM_CHANNEL = 7.50; // for business customers const double BUS_BILL_PROC_FEES = 15.00; const double BUS_BASIC_SERV_COST = 75.00; const double BUS_BASIC_CONN_COST = 5.00; const double BUS_COST_PREM_CHANNEL = 50.00; // variable to store the billing amount double amountdue; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 35 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 36
10 Residential customers: Bill Formulas (1/2) Business customers: Bill Formulas (2/2) amountdue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numofpremchannels * RES_COST_PREM_CHANNEL; if (numofbasicservconn <= 10) amountdue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numofpremchannels * BUS_COST_PREM_CHANNEL; amountdue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numofbasicservconn - 10) * BUS_BASIC_CONN_COST + numofpremchannels * BUS_COST_PREM_CHANNEL; PRO_02 PROF. HUNG-PIN(CHARLES) WEN 37 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 38 Main Algorithm (1/3) 1. Output floating-point numbers in fixed decimal with decimal point and trailing zeros Output floating-point numbers with two decimal places set the precision to two decimal places 2. Prompt user to enter account number 3. Get customer account number 4. Prompt user to enter customer code 5. Get customer code (r, R, b, or B) Main Algorithm (2/3) 6. If the customer code is r or R, Prompt user to enter number of premium channels Get the number of premium channels Calculate the billing amount Print account number and billing amount PRO_02 PROF. HUNG-PIN(CHARLES) WEN 39 PRO_02 PROF. HUNG-PIN(CHARLES) WEN 40
11 Main Algorithm (3/3) 7. If customer code is b or B, Prompt user to enter number of basic service connections Get number of basic service connections Prompt user to enter number of premium channels Get number of premium channels Calculate billing amount Print account number and billing amount 8. If customer code is other than r, R, b, or B, output an error message and exit. PRO_02 PROF. HUNG-PIN(CHARLES) WEN 41 Summary (1/3) Boolean expressions can be composed of relational and/or logical operations Relational Expressions (conditions): Are used to compare operands A condition that is true has a value of 1 A condition that is false has a value of 0 More complex conditions can be constructed from relational expressions using logical operators, && (AND), (OR), and! (NOT) Apply DeMorgan s Law properly PRO_02 PROF. HUNG-PIN(CHARLES) WEN 42 Summary (2/3) if- statements select between two alternative statements based on the value of an expression if- statements can contain other if- statements nested if If braces are not used, each statement is associated with the closest unpaired if if chain: a multi-way selection statement Each statement (except for the final ) is another if- statement Compound statement: any # of individual statements enclosed within braces {} PRO_02 PROF. HUNG-PIN(CHARLES) WEN 43 Summary (3/3) Condition operator (?:): equivalent to if- Ternary: takes three arguments switch statement: multi-way selection the value of an integer expression is compared to a sequence of integer or character constants or constant expressions (literals) program execution transferred to first matching case execution continues until optional break statement is encountered PRO_02 PROF. HUNG-PIN(CHARLES) WEN 44
C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection) Objectives In this chapter, you will: Learn about control structures Examine relational
More informationChapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection) 1 Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover
More informationObjectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators
Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationChapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.
Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection) Control Structures A computer can proceed: In sequence Selectively (branch) - making
More informationChapter 4 - Notes Control Structures I (Selection)
Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by
More informationControl Structures. Lecture 4 COP 3014 Fall September 18, 2017
Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls
More informationChapter 4: Control Structures I
Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore
More informationTopics. Chapter 5. Equality Operators
Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator
More informationC++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Malak Abdullah The Evolution of Programming Languages (cont'd.)
More information5. Selection: If and Switch Controls
Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation
More information7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING
KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve
More informationObjectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures
Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationControl Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping
Control Structures A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping Conditional Execution if is a reserved word The most basic syntax for
More informationProgramming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures
Chapter 4: Control Structures I Java Programming: Program Design Including Data Structures Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form
More informationSTUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode
STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level
More informationSlide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto
CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style
More informationChapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.
Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2
More informationFlow of Control. Flow of control The order in which statements are executed. Transfer of control
1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationV2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed
Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationChapter 4: Making Decisions
Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to
More informationChapter 4: Making Decisions
Chapter 4: Making Decisions CSE 142 - Computer Programming I 1 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >=
More informationChapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Copyright 2011 Pearson Addison-Wesley. All rights reserved.
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationSelection Statements. Pseudocode
Selection Statements Pseudocode Natural language mixed with programming code Ex: if the radius is negative the program display a message indicating wrong input; the program compute the area and display
More informationChapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.
Chapter 2 Flow of Control Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Boolean Expressions Building, Evaluating & Precedence Rules Branching Mechanisms if-else switch Nesting if-else
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationLECTURE 04 MAKING DECISIONS
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 04 MAKING DECISIONS
More informationChapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.
Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and
More informationChapter 2. C++ Basics
Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables
More informationChapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments
Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights
More informationChapter 3 Selection Statements
Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are
More informationChapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14
Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to
More informationWeek 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.
Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than
More informationswitch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++
Comp Sci 1570 Introduction to C++ Outline 1 Outline 1 Outline 1 switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2;
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 04 - Conditionals Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Control Structure Conditions
More informationIntroduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.
Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and
More informationUEE1302 (1102) F10: Introduction to Computers and Programming
Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,
More informationChapter 7. Additional Control Structures
Chapter 7 Additional Control Structures 1 Chapter 7 Topics Switch Statement for Multi-Way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements 2 Chapter
More informationKingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS
Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS Objectives By the end of this section you should be able to:
More informationAbsolute C++ Walter Savitch
Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents
More informationMotivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018
Motivating Examples (1.1) Selections EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG 1 import java.util.scanner; 2 public class ComputeArea { 3 public static void main(string[] args)
More informationconditional statements
L E S S O N S E T 4 Conditional Statements PU RPOSE PROCE DU RE 1. To work with relational operators 2. To work with conditional statements 3. To learn and use nested if statements 4. To learn and use
More informationStatements execute in sequence, one after the other, such as the following solution for a quadratic equation:
Control Structures Sequence Statements execute in sequence, one after the other, such as the following solution for a quadratic equation: double desc, x1, x2; desc = b * b 4 * a * c; desc = sqrt(desc);
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence
More informationChapter 4 : Selection (pp )
Page 1 of 40 Printer Friendly Version User Name: Stephen Castleberry email Id: scastleberry@rivercityscience.org Book: A First Book of C++ 2007 Cengage Learning Inc. All rights reserved. No part of this
More informationLecture 6. Drinking. Nested if. Nested if s reprise. The boolean data type. More complex selection statements: switch. Examples.
// Simple program to show how an if- statement works. import java.io.*; Lecture 6 class If { static BufferedReader keyboard = new BufferedReader ( new InputStreamReader( System.in)); public static void
More informationComputer Programming : C++
The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program
More informationC++ Program Flow Control: Selection
C++ Program Flow Control: Selection Domingos Begalli Saddleback College, Computer Science CS1B, Spring 2018 1 / Domingos Begalli CS1B Spring 2018 C++ Introduction 1/19 19 Control program flow control structures
More informationChapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationMotivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool
Chapter 3: Selections and Conditionals CS1: Java Programming Colorado State University Motivations If you assigned a negative value for radius in Listing 2.2, ComputeAreaWithConsoleInput.java, the program
More informationLecture 3 Tao Wang 1
Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers
More informationCSCI 1061U Programming Workshop 2. C++ Basics
CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output
More informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
More informationLecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences
Lecture 5 Selection Statements Review from last week cin and cout directives escape sequences member functions formatting flags manipulators cout.width(20); cout.setf(ios::fixed); setwidth(20); 1 What
More informationMaltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement
Maltepe University Computer Engineering Department Algorithms and Programming Chapter 4: Conditionals - If statement - Switch statement Control Structures in C Control structures control the flow of execution
More informationCS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm
CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm 1 16 quizzes taken Average: 37.9 Median: 40.5 Quiz 1 Results 16 Scores: 45 45 44 43 43 42 41 41 40 36 36 36 34 31 28 21 Avg
More informationControl Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.
EEE-117 COMPUTER PROGRAMMING Control Structures Conditional Statements Today s s Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical
More informationIntroduction to Programming using C++
Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know
More informationUEE1302 (1102) F10 Introduction to Computers and Programming (I)
Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data
More informationPRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops
PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical
More informationRelational Operators and if. Class 10
Relational Operators and if Class 10 Data Type a data type consists of two things: Data Type a data type consists of two things: a set of values Data Type a data type consists of two things: a set of values
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification
More information4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme
Chapter 4: 4.1 Making Decisions Relational Operators Simple Program Scheme Simple Program Scheme So far our programs follow a simple scheme Gather input from the user Perform one or more calculations Display
More informationBITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)
BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) 1 Learning Outcomes At the end of this lecture, you should be able to: 1. Explain the concept of selection control structure
More informationChapter 3. Selections
Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of
More informationChapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition
Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable
More informationChapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School Objectives Construct and evaluate Boolean expressions Understand how to use selection statements to make decisions Design and test
More informationProgramming, numerics and optimization
Programming, numerics and optimization Lecture A-2: Programming basics II Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428 March
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,
More informationObject-Oriented Programming
Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,
More informationAdd Subtract Multiply Divide
ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting
More informationCSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this
More information3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.
Chapter 2: Problem Solving Using C++ TRUE/FALSE 1. Modular programs are easier to develop, correct, and modify than programs constructed in some other manner. ANS: T PTS: 1 REF: 45 2. One important requirement
More informationDefinition: Data Type A data type is a collection of values and the definition of one or more operations on those values.
Data Types 1 Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values. Base Data Types All the values of the type are ordered and atomic.
More informationDecisions. Arizona State University 1
Decisions CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 4 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More information! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char
Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated
More informationChapter 2 - Control Structures
Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure
More information2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program
More informationAssoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions
More informationIntroduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)
Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program
More informationRecognize the correct ordering of decisions in multiple branches Program simple and complex decision
Lesson Outcomes At the end of this chapter, student should be able to: Use the relational operator (>, >=,
More informationThere are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.
Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationCS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay
CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Lecture 4, Conditional execution of instructions Friday, August
More informationConditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions
1999 UW CSE CSE / ENGR 142 Programming I Conditionals Chapter 4 Read Sections 4.1-4.5, 4.7-4.9 4.1: Control structure preview 4.2: Relational and logical operators 4.3: if statements 4.4: Compound statements
More informationBITG 1233: Introduction to C++
BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token
More informationConditions, logical expressions, and selection. Introduction to control structures
Conditions, logical expressions, and selection Introduction to control structures Flow of control In a program, statements execute in a particular order By default, statements are executed sequentially:
More informationExample. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable
CS 201 Selection Structures (2) and Repetition Debzani Deb Multiple-Alternative Decision Form of Nested if Nested if statements can become quite complex. If there are more than three alternatives and indentation
More informationBBM 101 Introduc/on to Programming I Fall 2013, Lecture 4
BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Ahmet Selman Bozkir, Gultekin Isik, Yasin Sahin 1 Today Condi/onal Branching Logical Expressions
More informationControl Structures: The IF statement!
Control Structures: The IF statement! 1E3! Topic 5! 5 IF 1 Objectives! n To learn when and how to use an IF statement.! n To be able to form Boolean (logical) expressions using relational operators! n
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationQUIZ: What value is stored in a after this
QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer
More informationLoops and Files. of do-while loop
L E S S O N S E T 5 Loops and Files PURPOSE PROCEDURE 1. To introduce counter and event controlled loops 2. To work with the while loop 3. To introduce the do-while loop 4. To work with the for loop 5.
More informationChapter 3. More Flow of Control
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-2 Flow Of Control Flow of control refers to the
More informationLecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
More information