# Chapter 4 - Notes Control Structures I (Selection)

Size: px
Start display at page:

Transcription

1 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 making a choice): Executes certain statements depending on some condition(s). 3. Repetition (loops) : Repeats particular statements a certain number of times depending on some condition(s). B. Examples of Conditional Statements 1. if ( score is greater than or equal to 90 ) grade is A 2. if ( hours worked are less than or equal to 40 ) wages = rate * hours otherwise wages = (rate * 40) * (rate * (hours - 40)) 3. if (temperature is greater than 70 degrees and it is not raining) recommended activity is golfing NOTICE: Certain statements will be executed only if the stated conditions are met. In other words, if the statement is true, the following statement is executed. In example 3, more than one condition must be met: a temperature greater than 70 AND no rain. Conditional statements can have one or more parts. If the statement is false, the statement is skipped or ignored or another statement is executed instead, as in example 2. II. Relational Operators A. Logical (Boolean) Expression: An expression that evaluates to true or false. 1. Logical (Boolean) Values: True or False Page 1

2 2. Relational Operators: Allow the programmer to make comparisons in a program. Arithmetic Operation Relational Operator Meaning = = = Is equal to < < Is less than > > Is greater than <= Is less than or equal to >= Is greater than or equal to!= Is NOT equal to 3. Each of the Relational Operators is a binary operator, because they are suppose to be comparing two values. B. Relational Operators and Simple Data Types 1. Relational Operators can be used on all three simple data types. Expression Meaning Value 8 < 15 8 is less than 15 true 6!= 6 6 is not equal to 6 false 2.5 > is greater than 5.8 false 5.9 <= is less than or equal to 7.5 true WARNING: The comparison of real numbers for equality is usually machine dependent. It is quite possible that on a particular machine = = will evaluate to false. 2. Relational Operators and the char Data Type i. When a character is evaluated with a relational operator, the character is normally converted to its integer equivalent on the ASCII chart. Both characters being compared are then converted to integers and the integers compared for equality, inequality, greater than, etc. Page 2

3 Expression Value of Expression Explanation ' ' < 'a' 'R' > 'T' '+' < '*' '6' <= '>' true false false true The ASCII value of ' ' is 32, and the ASCII value of 'a' is 97. Because 32 < 97 is true, it follows that ' ' < 'a' is true. The ASCII value of 'R' is 82, and the ASCII value of 'T' is 84. Because 82 > 84 is false, it follows that 'R' < 'T' is false. The ASCII value of '+' is 43, and the ASCII value of '*' is 42. Because 43 < 42 is false, it follows that ' + ' < ' * ' is false. The ASCII value of ' 6 ' is 54, and the ASCII value of ' > ' is 62. Because 54 <= 62 is true, it follows that ' 6 ' < ' > ' is true. 3. You should not use comparisons of different data types in your program because the results may be unpredictable. 4. More on Boolean Values: In C++, the Boolean value of true is actually returned as a one (1), while the Boolean value for false is actually returned as a zero (0). Actually, any number that is not zero is accounted as true in C++ Boolean values, but only a zero is accounted as false. C. Relational Operators and the string Type 1. Relational Operators Can be Applied to the string Type 2. Strings are compared character by character, starting with the first character and using the ASCII collating sequence. 3. Character by character comparison continues until either a mis-match is found or the last characters have been compared and are equal. 4. Assume that a program has made the following data declarations: Page 3

4 string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; string str5 = "Big"; Now we can look at the following table using the above declarations to determine how Logical Expressions work with Strings and String Variables. Expression str1 < str2 str1 > "Hen" str3 < "An" str1 = = "hello" str3 <= str4 Value true false true false true 5. Comparing Two Strings of Different Length i. Comparison is made character by character until it reaches the last character of the shorter string. If all characters have been equal, the string with the fewer characters is deemed to be less than the larger string. Examples using the same declared strings above. Expression str4 >= "Billy" str5 <= "Bigger" Value false true III. Logical (Boolean) Operators and Logical Expressions A. Logical (Boolean) Operators in C++ 1. Logical (Boolean) Operators allow the programmer to combine logical expressions. 2. C++ has three Logical (Boolean) Operators as follows: Page 4

5 Operator Description Type Operator! not unary - Require only one operand && and binary - Require two operands or binary - Require two operands 3. Logical operators take only logical values as operands and yield only logical values as results. 4. The! (not) Operator: Putting! in front of a logical expression reverses its value. So!(true) = false, and!(false) = true Expression Value Explanation! ( 'A' > 'B' ) true Because 'A' > 'B' is false,! ( 'A' > 'B' ) is true! ( 6 <= 7 ) false Because 6 <= 7 is true,! ( 6 <= 7 ) is false 5. The && (and) Operator: Expression A Expression B Expression A && Expression B true true true true false false false true false false false false Examples: Expression Value Explanation ( 14 >= 5 ) && ( 'A' < 'B' ) true ( 24 >= 35 ) && ( 'A' < 'B' ) false Because ( 14 >= 5 ) is true, ( 'A' < 'B' ) is true, and true && true is true, the expression evaluates to true. Because ( 24 >= 35 ) is false, ( 'A' < 'B' ) is true, and false && true is false, the expression evaluates to false. Page 5

6 Notice: The && (and) expression is true only when both expressions are true. Only one false expression renders the entire expression false. 6. The (or) Operator: Expression A Expression B Expression A Expression B true true true true false true false true true false false false Examples: Expression Value Explanation ( 14 >= 5 ) ( 'A' > 'B' ) true ( 24 >= 35 ) ( 'A' > 'B' ) false ( 'A' <= 'a' ) ( 7!= 7 ) true Because ( 14 >= 5 ) is true, ( 'A' > 'B' ) is false, and true false is true, the expression evaluates to true. Because ( 24 >= 35 ) is false, ( 'A' > 'B' ) is false, and false false is false, the expression evaluates to false. Because ( 'A' >= 'a' ) is true, ( 7!= 7 ) is false, and true false is true, the expression evaluates to true. Notice: The (or) expression is false only when both expressions are false. Only one true expression renders the entire expression true. B. Order of Precedence: Complex logical expressions can be difficult to evaluate, so an order of precedence is necessary for them also. Page 6

7 1. An expression like the following can yield more than one answer if evaluated in different orders: 11 > 5 6 < 15 && 7 >= 8 T T && F T && F = F (or) evaluated first T F = T (and) evaluated first Precedence of Operators Operators Precedence!, +, - (unary operators) first *, /, % second +, - third <, <=, >=, > fourth = =,!= fifth && sixth seventh = (assignment operator) last Suppose you have the following declarations: bool found = true, flag = false ; int num = 1; double x = 5.2, y = 3.4; int a = 5, b = 8, n = 20 ; char ch = 'B' ; Page 7

8 Examples using the above declarations: Expression Value Explanation!found false Because found is true,!found is false x > 4.0!num!found && ( x >= 0 ) true false false Because x is 5.2 and 5.2 > 4.0 is true, the expression x > 4.0 evaluates to true. Because num is 1, which is nonzero, num is true and so!num is false. In this expression,!found is false. Also, because x is 5.2 and 5.2 >= 0 is true, x >= 0 is true. Therefore, the value of the expression!found && (x >= 0) is false && true, which evaluates to false.!( found && ( x >= 0 ) ) false In this expression, found && ( x >= 0 ) is true && true, which evaluates to true. Therefore, the value of the expression!( found && ( x >= 0 )) is!true, which evaluates to false. x + y <= 20.5 true Because x + y = = 8.6 and 8.6 <= 20.5, it follows that x + y <= 20.5 evaluates to true. ( n >= 0 ) && ( n <= 100 ) true ( 'A' <= ch && ch <= 'Z' ) true ( a + 2 <= b ) &&!flag true Here n is 20. Because 20 >= 0 is true, n >= 0 is true. Also, because 20 <= 100 is true, n <= 100 is true. Therefore, the value of the expression ( n >= 0 ) && ( n <= 100 ) is true && true, which evaluates to true. In this expression, the value of ch is 'B'. Because 'A' <= 'B' is true, 'A' <= ch evaluates to true. Also, because 'B' <= 'Z' is true, ch <= 'Z' evaluates to true. Therefore, the value of the expression ( 'A' <= ch && ch <= 'Z' ) is true && true, which evaluates to true. Now a + 2 = = 7 and b is 8. Because 7 < 8 is true, the expression a + 2 < b evaluates to true. Also, because flag is false,!flag is true. Therefore, the value of the expression ( a + 2 <= b ) &&!flag is true && true, which evaluates to true. Page 8

9 C. Short-Circuit Evaluation 1. In C++, if the first value in an AND (&&) expression is false, it does not continue to evaluate the other side of the expression. It immediately evaluates to false. 2. Similarly, if the first value in an OR ( ) expression is true, it does not evaluate the right side but returns a true for the whole expression. D. The int Data Type and Logical (Boolean) Expressions 1. It is possible to store the value of a logical expression into an int data type variable. 2. Recall that false is counted as zero ( 0 ), and true is a one (1 ) or any nonzero value. 3. The following snippet of code illustrates how this can work. int legalage ; int age ; age = 21 ; legalage = ( age >= 21 ) ; The value of one ( 1 ) is assigned to legalage since age is equal to 21 and is greater than or equal to 21. The value of zero ( 0 ) would have been assigned to legalage had age been less than 21. IV. Selection: if and if... A. Branch Control Structures 1. There are two types of branch control structures that will be talked about in this chapter. i. The if and if... statements which can be used to create oneway, two-way, and multi-way selection statements. ii. The switch statement Page 9

10 B. One-Way Selection: If true, execute statement immediately following the logical expression. If false, skip that statement. 1. Syntax and Use: if ( logical expression ) statement ; Example: if ( score >= 90 ) grade = 'A' ; If the expression score >= 90 evaluates to true, the character 'A' is stored in grade. If the expression is false, the statement grade = 'A' is completely skipped or ignored. 2. Example of a program with a one-way selection statement: The following C++ program finds the absolute value of an integer #include <iostream> using namespace std; int main ( ) int number ; cout << "Please enter an integer > " ; cin >> number ; if ( number < 0 ) number = - number ; cout << endl << "The absolute value is " << number << endl ; system("pause") ; return 0 ; OUTPUT Please enter an integer > The absolute value is 6734 Page 10

11 3. Two Common Mistakes i. Leaving out the parenthesis: a. if score >= 90 Syntax error!! grade = 'A' ; b. Your compiler should let you know about this. ii. Putting a Semicolon after the Logical Expression: a. if ( score >= 90 ) ; Semantic error!! grade = 'A' ; b. Your compiler will not let you know about this!! C. Two-Way Selection: If true, execute statement immediately following the logical expression. If false, the statement immediately following the reserved word is executed. 1. Syntax and Use: if ( logical expression ) statement 1; statement 2; Example : if ( hours > 40.0 ) wages = 40.0 * rate * rate * ( hours ) ; wages = hours * rate ; 2. Sample Program: #include <iostream> #include <iomanip> using namespace std; int main ( ) double wages, rate, hours ; Page 11

12 cout << fixed << showpoint << setprecision(2) ; cout << Enter working hours and rate: ; cin >> hours >> rate ; if ( hours > 40.0 ) wages = 40.0 * rate * rate * ( hours ) ; wages = hours * rate ; cout << endl ; cout << The wages are \$ << wages << endl ; system( pause ) ; return 0 ; OUTPUT Enter working hours and rate: The wages are \$ Common Semantic Errors i. Consider the following statement: if ( score >= 90 ) grade = 'A' ; cout << "The grade is " << grade << endl; The if statement acts only on the grade = 'A' ; statement. Just because the other statement is indented doesn't make it apart of the if statement. The cout statement will execute regardless of whether the if statement is true or false. ii. Another common mistake is assuming the second statement will execute if the first one is false. Example: if ( score >= 60 ) cout << "Passing" << endl ; cout << "Failing" << endl; In this case, if the score is greater than 60, both lines of code will Page 12

13 execute. If you want the second statement to execute if the condition is false, or only the first statement to execute when the condition is true, you must use the if... statement like so: if ( score >= 60 ) cout << "Passing" << endl ; cout << "Failing" << endl ; D. Compound (Block of) Statements: A sequence of statements enclosed in curly braces that functions as if it were a single statement. 1. Syntax and Use: statement 1 ; statement 2 ; : : statement n ; 2. Use compound ( block of ) statements in a conditional statement when you want more than one statement to execute if the condition is true or if the condition is false in an if... statement. Example: #include <iostream> using namespace std; int main ( ) int age ; cout << "Please enter your age here: " << endl ; cin >> age; if ( age > 18 ) cout << "Eligible to vote." << endl ; cout << "No longer a minor." << endl ; Page 13

14 cout << "Not eleigible to vote." << endl ; cout << "Still a minor." << endl ; system("pause") ; return 0 ; OUTPUT Please enter your age here: 16 Not eligible to vote. Still a minor. Press enter to continue... E. Multiple Selections: Nested if: Used when more than two choices need to be made. 1. In a one-way selection ( if ), if the condition is true, the following statement is executed; false, and it isn't. A two-way selection ( if... ) executes one of two statements depending if the statement is true or false. With a multiple selection statement ( nested if statements ), a number of statements can be chosen from to execute depending on the which of the nested if statements is true. 2. Syntax and Use: if ( logical expression ) statement 1; if ( logical expression ) statement 2; if ( logical expression ) statement 3; : : statement n ; Page 14

15 OR if ( logical expression ) statement 1 ; if ( logical expression ) statement 2 ; if ( logical expression ) statement 3 ; : : statement n ; 3. Example Program using multiple selections: #include <iostream> using namespace std ; int main ( ) int score ; cout << "Please enter a score here: " ; cin >> score ; if ( score >= 90 ) cout << "The grade is A" << endl ; if ( score >= 80 ) cout << "The grade is B" << endl ; if ( score >= 70 ) cout << "The grade is C" << endl ; if ( score >= 60 ) cout << "The grade is D" << endl ; cout << "The grade is F" << endl ; system("pause") ; return 0 ; 4. Pairing an With an if i. It can be confusing in a nested if statement to determine which if the statement refers to. Page 15

16 ii. The Rule: In nested if statements, C++ associates the with the most recent incomplete if, or, in other words, the most recent if that has not been paired with an. F. Comparing if... Statements with a Series of if Statements 1. An if.. statement, if done properly, can be rewritten as a series of individual if statements. 2. A series of individual if statements, if done properly, can be rewritten as a single if.. statement. 3. So what is the difference between them? Example of a series of if statements and an if.. statement that performs the exact same evaluation and output. #include <iostream> using namespace std; int main ( ) int month ; cout << "Please enter integer from 1 to 6: " ; cin >> month ; if ( month = = 1 ) cout << "January" << endl ; if ( month = = 2 ) cout << "February" << endl ; if ( month = = 3 ) cout << "March" << endl ; if ( month = = 4 ) cout << "April" << endl ; if ( month = = 5 ) cout << "May" << endl ; if ( month = = 6 ) cout << "June" << endl ; system("pause") ; return 0 ; OUTPUT Page 16

17 Please enter integer from 1 to 6: 5 May Now let's see the same program using the nested if.. statements... #include <iostream> using namespace std; int main ( ) int month ; cout << "Please enter integer from 1 to 6: " ; cin >> month ; if ( month = = 1 ) cout << "January" << endl ; if ( month = = 2 ) cout << "February" << endl ; if ( month = = 3 ) cout << "March" << endl ; if ( month = = 4 ) cout << "April" << endl ; if ( month = = 5 ) cout << "May" << endl ; if ( month = = 6 ) cout << "June" << endl ; system("pause") ; return 0 ; OUTPUT Please enter integer from 1 to 6: 5 May 4. In the first example, every one of the conditional statements will be checked even if the user types in the number one (1). In the second example, as soon as a true condition is met and the corresponding statement(s) executed, the program jumps out of the if.. statement. Therefore, the if.. statement is more efficient. The only time it is not more efficient is if it too has to check all the way Page 17

18 V. Switch Structures down to the last condition. A. A Selection or Branch Structure that does not require the evaluation of a logical expression. 1. Gives the computer the power to choose from among many alternatives. B. Syntax & Use: switch ( expression ) case value 1 : statements 1; break; case value 2 : statements 2 ; break ; : case value n : statements n ; break ; default : statements ; 1. Notice that switch, case, break, and default are all reserved words. 2. The value of the expression ( also called the selector )is usually an identifier ( variable ). The expression or identifier, as well as the values following the case labels, must be of an integral type. Example: integer, character, or boolean value. 3. A particular case should only appear once. 4. One or more statements may follow a case label. 5. How the switch statement executes: i. When the value of the expression is matched against a case value (also called a label ), the statements execute until either a break statement is found or the end of the switch structure is reached. ii. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. Page 18

### C++ 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

### Objectives. 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)

### Chapter 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)

### 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

### Chapter 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

### Chapter 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

### Recognize 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 (>, >=,

### Control 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

### Objectives. 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)

### 5. 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

### C++ 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

### Programming: 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

### Kingdom 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:

### Increment and the While. Class 15

Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment

### V2 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

### Flow 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

### Chapter 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

### CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

### Objectives. 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

### 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

### Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

### Chapter 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

### CS 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

### Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

### The following expression causes a divide by zero error:

Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct

### Topics. 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

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

### In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CS101, Mock Boolean Conditions, If-Then Boolean Expressions and Conditions The physical order of a program is the order in which the statements are listed. The logical order of a program is the order in

### ! 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

### Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ 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

### REPETITION CONTROL STRUCTURE LOGO

CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

### Chapter 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

### Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

### In this chapter, you will:

Java Programming: Guided Learning with Early Objects Chapter 4 Control Structures I: Selection In this chapter, you will: Make decisions with the if and if else structures Use compound statements in an

### Chapter 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 >=

### CSc 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

### Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

1 A Closer Look at the / Operator Used for performing numeric calculations C++ has unary, binary, and ternary s: unary (1 operand) - binary ( operands) 13-7 ternary (3 operands) exp1? exp : exp3 / (division)

### UEE1302(1102) F10: Introduction to Computers and Programming

Computational Intelligence on Automation Lab @ NCTU UEE1302(1102) F10: Introduction to Computers and Programming Programming Lecture 02 Flow of Control (I): Boolean Expression and Selection Learning Objectives

### Control 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

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

### Chapter 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

### BITG 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

### LECTURE 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

### STUDENT 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

### WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

### Lecture 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

### Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

### Chapter 2 Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

### 4.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

### The C++ Language. Arizona State University 1

The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

### Computer 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

### The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

### C/C++ Programming Lecture 7 Name:

1. The increment (++) and decrement (--) operators increase or decrease a variable s value by one, respectively. They are great if all you want to do is increment (or decrement) a variable: i++;. HOWEVER,

### Chapter 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

### Chapter 3 Problem Solving and the Computer

Chapter 3 Problem Solving and the Computer An algorithm is a step-by-step operations that the CPU must execute in order to solve a problem, or to perform that task. A program is the specification of an

### CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,

### Control 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

### Introduction to Programming

Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

### By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

### C++ 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,

### Week 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

### Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

### Input And Output of C++

Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

### Chapter 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

### 2.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

### Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

### C++ 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.)

### Your First C++ Program. September 1, 2010

Your First C++ Program September 1, 2010 Your First C++ Program //*********************************************************** // File name: hello.cpp // Author: Bob Smith // Date: 09/01/2010 // Purpose:

### 1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

1. C++ Overview 1. C++ Overview C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions. Interactive Mode, Batch Mode and Data Files. Common Programming

### DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ Objective: To Learn Basic input, output, and procedural part of C++. C++ Object-orientated programming language

### If Control Construct

If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD 2002 McGraw-Hill, Inc. 1 Boolean Algebra Logical expressions have the one of two values - true or false A rectangle

### In this chapter you will learn:

1 In this chapter you will learn: Essentials of counter-controlled repetition. Use for, while and do while to execute statements in program repeatedly. Use nested control statements in your program. 2

### LECTURE 02 INTRODUCTION TO C++

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 02 INTRODUCTION

### Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

### CHAPTER 3 BASIC INSTRUCTION OF C++

CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers

### A First Program - Greeting.cpp

C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

### Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

### BITG 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

### C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures

### Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018 Repetition statements Control statements change the order which statements are executed Selection :

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

### C++ for Python Programmers

C++ for Python Programmers Adapted from a document by Rich Enbody & Bill Punch of Michigan State University Purpose of this document This document is a brief introduction to C++ for Python programmers

### CSc Introduction to Computing

CSc 10200 Introduction to Computing Lecture 2 Edgardo Molina Fall 2011 - City College of New York Thursday, September 1, 2011 Introduction to C++ Modular program: A program consisting of interrelated segments

### 5. Control Statements

5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

### Understanding main() function Input/Output Streams

Understanding main() function Input/Output Streams Structure of a program // my first program in C++ #include int main () { cout

### ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

### 7/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

### Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

### Chapter 3 - Notes Input/Output

Chapter 3 - Notes Input/Output I. I/O Streams and Standard I/O Devices A. I/O Background 1. Stream of Bytes: A sequence of bytes from the source to the destination. 2. 2 Types of Streams: i. Input Stream:

### 5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

### CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

### Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

ELEC 330 1 Structured Programming Control Structures ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne Algorithm Development Conditional Expressions Selection Statements Loops 206_C3

### CSI33 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

### Lab 10: Alternate Controls

_ Unit 2: Programming in C++, pages 1 of 8 Department of Computer and Mathematical Sciences CS 1410 Intro to Computer Science with C++ 9 Objectives: Lab 10: Alternate Controls The objective of this lab

### Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

### Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Lesson #4 Logical Operators and Selection Statements Control Structures Control structures combine individual instructions into a single logical unit with one entry point at the top and one exit point