Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Selection (decision) control structure Learning objective
Control Structures Corresponds with Chapters 3 and 4.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
C++ for Engineers and Scientists Third Edition
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 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.
Computer Science Selection Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Chapter 4 Selection Structures: Making Decisions.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
 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.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Week 4 Program Control Structure
CPS120: Introduction to Computer Science Decision Making in Programs.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I
Making Choices with if Statements
Engineering Problem Solving with C++, Etter/Ingber
Chapter 8 - JavaScript: Control Statements I
Lecturer CS & IT Department UOS MBDIN
Chapter 4 – Control Structures Part 1
JavaScript: Control Statements I
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
MSIS 655 Advanced Business Applications Programming
Structured Program
Control Structures: Selection Statement
3 Control Statements:.
Week 3 – Program Control Structure
Control Structures: Selection Statement
Selection Control Structure
Controlling Program Flow
Structural Program Development: If, If-Else
Presentation transcript:

Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks

Chapter 3 more Control Structures switch statements multiple-selection Relational operators logical operators

Secondary concepts Keywords Condition operators (c?t:f) Top down design aka step wise refinement Compound or shortcut assignment operators Increment and decrement operators Pre increment (++x) and post increment (x++) Primitive data types and sizes

NOTE: Some versions of the JDK will require a class path be set in the DOS window Type this after setting the path. Note the period on the end is very important. SET CLASSPATH=c:\jdk1.2.1\lib;.

Three control structures Sequence – flow The programs flow Top down Method calls Selection – logic – conditional Choose between alternate code blocks If…then Iterations - Repetition– loops Perform a code block multiple times While For

Structured design Algorithms An algorithm is a solution to a problem. To create a solution you must first understand the problem. Then plan a solution. Algorithms describe: The actions to be taken The order in which to perform the actions

Pseudocode Informal (code like) language used to describe an algorithm. Written in common language Helps programmer think out the algorithm (design). Includes only execution logic.

Pseudocode A trivial example: Get user input for radius Compute diameter Compute circumference and area Display results A single program may have many parts. What is better one big pseudocode or many small ones? Which is easier to read and unserstand

Flowcharts Used to illustrate program flow initial state transition lines action statements final state notes go here get radius Compute Diam Compute area Display area Start End

Now comparisons The comparison statement used in almost every programming language is the “If….then” statement If (a==10) System.out.println(“A is 10”); The word then is implied A semicolon delimits the end of the if….then statement. If (a=10) // may be a logic error If (a<10); // may be a logic error

Logic operators < <= > >= == !=

The if…else statement if (condition) statement; or if (condition) statement else statement; or if (condition) { statements; …. } else { statements; }

Lets write the program to compare two numbers We will use if statements…

Comparing strings Strings are objects and must be compared using special methods. s1.compareto(s2) Returns negative if s1 is less than s2 Returns 0 if s1=s2 Returns positive if s1 is greater than s2 S1.equals(s2) Used to compare objects in general

Grades if (grade>60) system.out.print(“student passed”); grade >60 no Print “passed” get grade yes Do Other commands

Grades with else if (grade>60) system.out.print(“student passed”) else system.out.print(“student failed”); grade >60 no Print “passed” get grade yes Do Other commands Print “failed”

A neat trick The conditional operator a ternary operator (the only one) for example message = (grade>60 ? “passed” : “failed”); Same as If (grade>60) message=“passed” else message = “failed”;

Chained if else commands Rewrite the nested if statements to use chained if else statements.

All of the grades Let’s print the letter grade? For this we will use nested if else statements if (grade>90) print “A” else if (grade>80) print “B” …. What are some other ways to write this?

Other operators Compound or shortcut assignment operators x += 200; // same as x=x+200 x *= 2; // same as x=x*2 also for %=, -= and /=. Increment and decrement operators Pre increment (++x) x=5; y = ++x; // so y will be 6 post increment (x++) x=5; y = x++; // so y will be 5 also for decrement x--; or --x;

Nested if statements If (grade > 70) { if (grade > 80) { if (grade > 90) System.out.println(“A”); else System.out.println(“B”); } else System.out.println(“C”); }

The switch multiple selection statement Like a string of if statements switch( variable ) { case ‘l’: case ‘L’: print “A line”; break; case ‘C’: print “A circle”; break; default: print “Invalid choice”; break; }

Switch errors forgetting a break can be a logic error default case is optional

Logical operators consider: if (speed==0) { if (engineon==true) { print “ok to stop engine”; } or if ((speed==0) && (engineon==true)) print “ok to stop engine”;

More logical operators && conditional and || conditional or & boolean and | boolean or ! not boolean operators always evaluate both expressions

Truth tables illustrate logical operations ABA&&BA||B!B TTTTF TFFTT FTFT- FFFF-

Short circuit evaluation if (speed==0 && engineon==true) print X; if condition A is false then condition B will not be evaluated at all. if (speed==0 || engineon==true) print X; if condition A is true then condition B will not be evaluated at all. This can lead to logic errors if side effect exist in condition B. Boolean operators always evaluate both expressions.

Expression side effects stay away from these while (++x<10) print x; If (retiredFlag==true) &&(++age<65) the increment operator is used as a side effect in the case of this conditional expression. Here is the side effect

Homework #3 (CS211/212 only) Problems 7 and 9 on page 159 Due in one week.