CMT4001 -- Programming Software Applications Week 3 Dr. Xiaohong Gao TP Room B107 Control Structures.

Slides:



Advertisements
Similar presentations
/ 281 Internet Applications Ahmed M. Zeki Sem – / Chapter 9.
Advertisements

Control Structures Selections Repetitions/iterations
More on Algorithms and Problem Solving
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 Control structures  Algorithm & flowchart  If statements  While statements.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
Introduction to Computers and Programming Lecture 5 New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Lecture 3 Structured Program Development in C
Chapter 4 Control Statements: Part I
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
C++ If….Else Statements and Flowcharts October 10, 2007.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1 Outline 4.1Introduction 4.2Algorithms 4.3Pseudocode 4.4Control.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter 7 JavaScript: Control Statements, Part 1
Branching statements.
Algorithm: procedure in terms of
Java™ How to Program, 10/e Late Objects Version
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Microsoft Visual Basic 2005 BASICS
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Program Control using Java - Theory
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Structural Program Development: If, If-Else
Presentation transcript:

CMT Programming Software Applications Week 3 Dr. Xiaohong Gao TP Room B107 Control Structures

Contents  To become familiar with control structures  To become familiar with the if Selection Structure  To become familiar with the if/else selection structure  To become familiar with nested control structures  To become familiar with nested if/else structures

Control Structures Before writing a program  Have a thorough understanding of problem Carefully planned approach for solving it While writing a program  Know what “building blocks” are available  Use good programming principles Computing problems  All can be solved by executing a series of actions in a specific order

Control Structures Algorithm  Procedure in terms of actions to be executed order in which these actions are to be executed  Example: " Rise and Shine " algorithm Get out of bed, take off pajamas, take a shower, get dressed, eat breakfast, carpool to work Program control  Specify order in which statements are to executed

Control Structures Pseudocode  Artificial, informal language Helps develop algorithms  Similar to everyday English  Not actually executed on computers  “Think out” a program before writing it in a programming language Easy to convert into a corresponding Java program Consists only of executable statements  Declarations are not executable statements  Actions: input, output, calculation

Control Structures Bohm and Jacopini –Selection structures Three types : if, if/else, and switch –Repetition structures Three types : while, do/while, and for Keywords –Words reserved for Java –Cannot be used as identifiers or variable names

Control Structures

Flowchart  Drawn using symbols connected by arrows called flowlines  Graphical representation of an algorithm Rectangle symbol (action symbol) –Indicates any type of action. Oval symbol:  Indicates beginning or end of a program, or a section of code  Oval containing "Begin" is first symbol Oval containing "End" is last symbol When drawing a portion, small circles used  Useful for algorithms, but psuedocode generally preferred

Control Structures Flowchart of sequence structure  Two actions performed in order total = total + grade; add grade to total add 1 to counter counter = counter + 1;

The if Selection Structure // JavaVendorMachine0.java – no decision import java.io.*; public class JavaVendor Machine0 { public static void main(String[]args) { System.out.println(“Drop cup into position”); System.out.println(“Pour hot water through filter”); System.out.println(“Pour cream into cup”); System.out.println(“Open door”); }

The if Selection Structure import java.io.*; public class CoffeeVendorMachine { public static void main(String[] args) throws Exception { char userChoice; //create a char field //to hold the selection //tell the customer what the options are System.out.println("Enter 1 for cream:"); System.out.println("Enter 2 for no cream:");

The if Selection Structure // get the user's selection userChoice = (char) System.in.read(); System.out.println("Drop small cup into position."); System.out.println("Pour hot water through filter"); //dispense a cream if the user enter '1' //nothing otherwise; if(userChoice==1) System.out.println("Poor cream into cup"); System.out.println("Open door"); }

The if Selection Structure Output of the program: Enter 1 for cream: Enter 2 for no cream: 1 Drop small cup into position. Pour hot water through filter. Pour cream into cup. Open door. (Cont..)

The if Selection Structure Output of the program: Enter 1 for cream: Enter 2 for no cream: 2 Drop small cup into position. Pour hot water through filter. Open door.

The if Selection Structure Selection structure  Used to choose among alternative courses of action Pseudocode: If user choice is 1, print “Pour cream into cup”  If condition true Print statement executed and program goes on to next statement  If condition false Print statement is ignored and the program goes onto the next statement Java ignores whitespace characters

The if Selection Structure if ( condition ) statement; Condition body Boolean condition Keyword if (userChoice ==1) System.out.println(“Pour cream into cup”);

The if Selection Structure Selection structure –Pseudocode statement If user choice is 1 than print “add cream” Pseudocode statement in Java if ( userChoice==1 ) System.out.println( “Pour cream into cup" ); Corresponds closely to pseudocode Flowcharts –Diamond symbol - decision symbol Important - indicates an decision is to be made Contains expression that can be true or false

The if Selection Structure Flowcharts  Diamond symbol has two paths Indicates what to do if condition true or false  Decision can be made on anything that evaluates to a value of data type boolean true or false true false userChoice== 1 print “Add cream” if is a single-entry/ single-exit structure

The if/else Selection Structure Selection structures – if Only performs an action if condition true – if/else Performs different action when condition true than when condition false Pseudocode If user choice is ‘S’print ”Provide a small coffee”” else Print “Provide a large coffee”

The if/else Selection Structure Java code: if(userChoice ==‘S’) { System.out.println(“Drop a small cup into place”); System.out.println(“dispose 8 oz.hot water”); } else { System.out.println(“Drop a large cup into place”); System.out.println(“Dispose 12 oz.hot water”); }

The if/else Selection Structure import java.io.*; public class CoffeeVendorMachine1 { public static void main(String[] args) throws Exception { char userChoice; //create a char //field to hold the selection //tell the customer what the options are System.out.println(); System.out.println("Enter S for small cup\n"); System.out.println("Enter L for large cup\n"); System.out.println("Enter your selection, please:");

The if/else Selection Structure //get the user's selection userChoice = (char) System.in.read(); //dispense a small cup if the user enters 'S', // a large cup otherwise if (userChoice == 'S') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); }

The if/else Selection Structure else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz.hot water"); } System.out.println("Open door"); }

The if/else Selection Structure Program’s output: Enter S for small cup. Enter your selection, please: Small Drop small cup into position. Dispense 8 oz.hot water. Open door. (Cont…)

The if/else Selection Structure Program’s output: Enter S for small cup. Enter your selection, please: Large Drop large cup into position. Dispense 12 oz.hot water. Open door.

The if/else Selection Structure if (condition) { statement; // if condition true statement; } else { statement; // if condition false statement; } False branch True branch Boolean test expression Keywords Braces

The if/else Selection Structure Flowchart of if / else structure  Note that only symbols (besides circles and arrows) are Rectangles: actions Diamonds: decisions  Action/decision model model of computing Programmer's job to assemble structures by stacking and nesting, then the define actions and decisions truefalse print “large cup” print “Small cup” userChoice==‘S’

Nested if/else structures  Test for multiple cases  Place if / else structures inside if / else structures  If first condition met, other statements skipped

Nested if/else structures Nested structures if ( studentGrade >= 90 ) System.out.println( "A" ); else if ( studentGrade >= 80 ) System.out.println( "B" ); else if ( studentGrade >= 70 ) System.out.println( "C" ); else if ( studentGrade >= 60 ) System.out.println( "D" ); else System.out.println( "F" ); else only executes when the if condition fails..

Nested if/else structures // program using nested logic import java.io.*; public class CoffeeVendorMachine5 { public static void main(String[] args) throws Exception { char userChoice;//create a char field to hold // the selection //tell the customer what the options are System.out.println(); System.out.println("Bold, Brazilian Roast (Extra Octane)"); System.out.println(" 1 - Small");

Nested if/else structures System.out.println(" 2 - Large"); System.out.println("\nSuper Soother Italian Expres:"); System.out.println(" 3 - Small"); System.out.println(" 4 - Large"); System.out.println("Enter your selection, please:"); System.out.flush(); //get the user's selection userChoice = (char) System.in.read(); //decide between Brazilian filter and Italian //filter if ( userChoice=='1' || userChoice == '2')

Nested if/else structures { System.out.println("Put Brazilian Roast filter in place"); if (userChoice =='1') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz. hot water"); }

Nested if/else structures } else { System.out.println("Put Italian Expresso filter in place"); if (userChoice =='3') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz.hot water"); }

Nested if/else structures } System.out.println("Open door"); }

The if/else Selection Structure Important note  Java always associates else with previous if unless braces ( {} ) present Dangling-else problem if ( x > 5 ) if ( y > 5 ) System.out.println( "x and y are > 5" ); else System.out.println( "x is <= 5" );

The if/else Selection Structure  Does not execute as it appears, executes as if ( x > 5 ) if ( y > 5 ) System.out.println( "x and y are > 5" ); else System.out.println( "x is <= 5" );

The if/else Selection Structure Important note  Must force structure to execute as intended Use braces to indicate that second if in body of first if ( x > 5 ) { if ( y > 5 ) System.out.println( "x and y are > 5" ); } else System.out.println( "x is <= 5" ); Compound statements  Compound statement - set of statements within braces Can be used wherever a single statement can  if expects one statement in its body  To enclose multiple statements, enclose them in braces

CoffeeVendorMachine5.java //program using nested logic import java.io.*; public class CoffeeVendorMachine5 { public static void main(String[] args)throws Exception { char userChoice; //create a char //field to hold the selection //tell the customer what the options are System.out.println(); System.out.println("Bold,Brazilian Roast(Extra Octane)"); System.out.println(" 1 - Small"); System.out.println(" 2 - Large"); System.out.println("\nSuper Soother Italian Expresso:");

CoffeeVendorMachine5.java System.out.println(" 3 - Small"); System.out.println(" 4 - Large"); System.out.println("Enter your selection,please:"); System.out.flush(); //get the user's selection userChoice = (char) System.in.read(); //decide between Brazilian filter and Italian filter if ( userChoice=='1' || userChoice == '2') { System.out.println("Put Brazilian Roast filter in place");

CoffeeVendorMachine5.java if (userChoice =='1') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz. hot water"); } else { System.out.println("Put Italian Expresso filter in place");

CoffeeVendorMachine5.java if (userChoice =='3') { System.out.println("Drop small cup into position"); System.out.println("Dispense 8 oz.hot water"); } else { System.out.println("Drop large cup into position"); System.out.println("Dispense 12 oz.hot water"); } } System.out.println("Open door"); }

Summary Control Structure Selection Structure