© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.

Slides:



Advertisements
Similar presentations
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Advertisements

Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Copyright 2003 Scott/Jones Publishing Making Decisions.
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.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CSI 3125, Preliminaries, page 1 Control Statements.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Flow of Control.
Chapter 3 Branching Statements
Expressions and Control Flow in JavaScript
Control Structures – Selection
Chapter 4: Control Structures I
Selection (if-then-else)
Control Structures: Selection Statement
Flow Control Statements
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Program Flow.
Control Structures: Selection Statement
Control Structure.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4

© 2000 Scott S Albert Flow of Control Computers execute statements sequentially Flow of control statements alter this sequential process –Branching statements choose one or another branch depending on a test –Looping statements repeats one or more statements multiple times

© 2000 Scott S Albert C++ flow control statements Sequence the default C++ automatically executes the next instruction unless you use a branching statement Branching: Selection if if-else if-else if-else if- … - else switch

© 2000 Scott S Albert If Else Statement Branching statement –Format 1 if ( ) is only executed if the expression is true –Format 2 if ( ) else is only executed if the expression is true is only executed if the expression is false

© 2000 Scott S Albert Boolean Expressions Boolean Expression –Expression that can simplifies to true or false –We have already used some Boolean expressions and if statements in our sample programs if (cAnswer == ‘y’) cout<<“thanks”; Simple Expressions –

© 2000 Scott S Albert Comparison Operators equals= nT*5/9-32 == 0 not equal! =sChar != ‘S’ greater than>hours > 40 less than<pay < 0 greater or equal>=radius >= 5.2 less than or equal<=sales <= plan

© 2000 Scott S Albert Boolean Expressions Complex arithmetic expressions can be used rate*43 / 17 <= base * 8 Simple expressions can be combined using –And -- && Both expressions must be true for the result to be true –Or -- || If either expression is true the result is true Each simple expression should be in ( )

© 2000 Scott S Albert Boolean Variables Boolean variables have the value true or false Boolean variables can be assigned –A boolean expression –The value true or false –Another boolean variable A boolean variable can be substituted for a boolean expression

© 2000 Scott S Albert Comparing Objects Comparison operators only work with basic types (int, float, char etc.) To compare classes, like Strings you must –Use a class comparison method if one exists –Use a class method to retrieve the class data into a basic type

© 2000 Scott S Albert Grouping statements What if you need more than one statement to be executed if an if Boolean expression is true? –Statements are grouped by { } –Statement groups can be nested –All statements in a group are treated as a single statement. This is called a statement block

© 2000 Scott S Albert Example int nHours; float nPay, nGross, nOvertime; // Read in nHours, nPay, sOT (overtime paid) if (nHours <= 0) cou<<"Error in hours input value is ”<<nHours; else { if ( nHours > 40 ) nOvertime = (nHours - 40) * ( Pay * 1.5 ); else nOvertime = 0; }

© 2000 Scott S Albert Matching Elses –An else matches up to the nearest preceding if that has not already been spoken for by another else Use blocks to help document your code. if (coffee==‘y’) if (donuts == ‘y’) cout<<“We have coffee and donuts.”; else cout<<“We have coffee, but not donuts.”; else if (tea==‘y’) cout<<“We have no coffee, but tea.”; else cout<<“We have no coffee, but tea.”;

© 2000 Scott S Albert Switch Statement –Multi-branch if statements such as the one used in the previous example are quite common in many programming projects Especially in menu systems –Enter 1 for New Messages –Enter 2 for Greetings –Enter 3 for Administrative options –Enter 4 to Send a message –Enter 5 to end Switch is easier to implement than many if statements

© 2000 Scott S Albert Switch Statement Syntax –switch ( ) { case : break; case : break; default: }

© 2000 Scott S Albert Sample int nMenu; // System.out.println statements listing a menu cout<<“Enter choice”; cin>>nMenu; switch( nMenu ) { case 1: cout<<“Listening to messages”; break; case 2: cout<<“Change greetings”; break; case 3: cout<<“Administrative stuff”; break; case 4: cout<<“Sending message”; break; case 5: cout<<“Goodbye”; break; default: cout<<“Sorry we did not understand you”; }

© 2000 Scott S Albert Switch notes Usually at least one case statement is needed The default statement is optional, but highly recommended If you omit the break statement, the flow of execution will continue into the next case