Visual C++ Programming: Concepts and Projects

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Control statements Mostafa Abdallah
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Branching statements.
CNG 140 C Programming (Lecture set 3)
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Selection—Making Decisions
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
JavaScript: Control Statements.
JavaScript: Control Statements I
Control Structures.
Topics The if Statement The if-else Statement Comparing Strings
IF if (condition) { Process… }
Control Structures: Selection Statement
3 Control Statements:.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
CprE 185: Intro to Problem Solving (using C)
Control Structures.
Presentation transcript:

Visual C++ Programming: Concepts and Projects Chapter 4A Selection (Concepts)

Objectives Discover what control structures are and why they are important Learn the difference between sequential control structures and selection control structures Compare values using relational operators Use an if statement to select single alternative Use an if...else statement to select one of two alternatives Visual C++ Programming

Objectives (continued) Develop an understanding of logical operators and their use in complex expressions Create nested control structures Become familiar with multiple selection and the switch statement Visual C++ Programming

Control Structures Control structures Three types Are the fundamental building blocks of all programs Control the flow of instruction execution Three types Sequential Every statement is executed in sequence Selection Allows you to skip statements Repetition Allows you to repeat statements Visual C++ Programming

Visual C++ Programming

Sequential Control Structures Linear in nature Each statement is performed in order No statement is skipped No statement is repeated The simplest programs tend to be sequential in nature Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Selection Control Structures Provide alternative course of action Single alternative selection Double alternative selection Multiple alternative selection A Boolean expression is evaluated and used to select a course of action Visual C++ Programming

Visual C++ Programming

Relational Operators A Boolean expression often compares two variables The relationship between two variables is evaluated using relational operators The equal to (==) and not equal to (!=) operators have lowest precedence All relational operators are binary (have two operands) All relational operators are left-to-right associative Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Using if Statements to Provide a Single Alternative Syntax: keyword if followed by a Boolean expression) If a Boolean expression is true then one or more statements are executed If only one task is to be executed it can be listed after the Boolean expression If more than one task is to be executed they must be enclosed in curly brackets { } Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Using if…else Statements to Provide Two Alternatives The keyword else is used to separate the two alternatives If the Boolean expression is true then the statements in the first alternative are selected If the Boolean expression is false then the statements in the second alternative are selected Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Logical Operators Operators with Boolean operands Include operators for “and”, “or” and “not” Examples from everyday life. You may have an exam on Monday but want to go to a concert in another city this weekend. If it is true that I have an exam, then I will not try to get concert tickets If it is not true that I have an exam, then I will try to get tickets. Visual C++ Programming

Logical Operators Operators with Boolean operands Include operators for “and”, “or” and “not” Logical operators Not (!) And (&&) Or (||) Visual C++ Programming

The not Operator (!) Reverses a Boolean value Has highest precedence among logical operators Uses the ! Operator Example: !(score >= 60) Assume that score is 45. Then, the relational expression score >= 60 is false, ! reverses the evaluation to true Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

The and Operator (&&) Used with two Boolean operands – often relational expressions Lower precedence than not (!) Example: if ((score > 0) && (score <= 100)) The operands may both be true The left operand may be true and the right operand false The right operand may be true and the left operand false Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

The and Operator (&&) (continued) If either the left or right operands are false then the entire expression evaluates to false The only way and expression evaluates to true using the and operator (&&) is if both operands are true Visual C++ Programming

Visual C++ Programming

Determining When to Use the and operator (&&) and the or operator (||) Consider a program with two TextBoxes that must contain integers and a ComboBox control to indicate which arithmetic operation to perform Possible errors of omission No data in both TextBoxes Data in one TextBox but not the other No operation selected from the ComboBox Example: if ((txtLeft->Text == “”) && (txtRight->Text == “”)) Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

The or Operator (||) Unlike the and operator (&&) if either the left or right operands are true then the entire expression evaluates to true The only way and expression evaluates to false using the or operator (||) is if both operands are false Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Nested Control Structures Nested control structures are ones that are placed inside of another Often used to implement multiple alternative selection A double alternative (if…else) statement within one alternative of another if…else statement Visual C++ Programming

Visual C++ Programming

Nested Control Structures (continued) Example: a ComboBox can be evaluated to determine whether a selection has been made. Valid ComboBox choices have index values starting at 0 If no selection has been made the SelectedIndex property of a ComboBox is set to -1 by default Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Multiple Alternative Selection Can be implemented with nested if…else statements The statements work like a filter Whichever Boolean expression evaluates to true first controls that path of execution that the program has The if…else if statement is made to accommodate multiple selection without nesting Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

The switch statement also implements multiple selection Keyword switch is followed by an integral value The integral value is used to determine which case will be executed Each case has statements that will be executed Control will transfer out only it a break statement or the end of the switch statement is encountered Visual C++ Programming

Visual C++ Programming

Visual C++ Programming

Summary Control structures are the building blocks of every computer program Types of control structures Sequential – linear, no statement repeated or skipped Selection – allows one or more statements to be skipped under specific conditions Repetition – the topic of Chapter 5 Visual C++ Programming

Summary (continued) Types of selection structures Single alternative (if statement) Double alternative (if…else statement) Multiple alternative Nested if…else statements Multiple alternative if (if…elseif statement) Switch statement All if statements evaluate a Boolean expression Visual C++ Programming

Summary (continued) Boolean expressions often use relational operators >, >=, <, <=, ==, != Complex Boolean expressions can be evaluated using logical operators and (&&) and or (||) Visual C++ Programming