Similar documents
A Look Back at Arithmetic Operators: the Increment and Decrement

Repetition Structures

PROBLEM SOLVING WITH LOOPS. Chapter 7

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Operators & Expressions

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Programming for Engineers Iteration

Lecture 10. Daily Puzzle

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

C/C++ Programming Lecture 7 Name:

Increment and the While. Class 15

More Programming Constructs -- Introduction

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Repetition CSC 121 Fall 2014 Howard Rosenthal

Program Control Flow

Program Control Flow

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

5. Selection: If and Switch Controls

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

ECE 122 Engineering Problem Solving with Java

Course Outline. Introduction to java

Chapter 4 C Program Control

Pseudocode is an abbreviated version of the actual statement t t (or code ) in the program.

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Introduction. C provides two styles of flow control:


CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

LECTURE 5 Control Structures Part 2

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

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

Final Examination Semester 2 / Year 2010

Chapter 3 Structured Program Development

IT 1033: Fundamentals of Programming Loops

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 3. Selections

Chapter 4 - Notes Control Structures I (Selection)

REPETITION CONTROL STRUCTURE LOGO

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Chapter 4: Control Structures I (Selection)

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Control Structures. Control Structures 3-1

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

Chapter 4: Control structures. Repetition

LESSON 3 CONTROL STRUCTURES

Control Structures (Deitel chapter 4,5)

Control Structures II. Repetition (Loops)

Control structure: Repetition - Part 3

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Chapter 4 C Program Control

COMP Primitive and Class Types. Yi Hong May 14, 2015

Computational Physics - Fortran February 1997

CMPT 125: Lecture 4 Conditionals and Loops

COMP 202 Java in one week

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

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

Add Subtract Multiply Divide

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Repetition Algorithms

Expressions and Assignment Statements

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Problem Solving and Algorithms

Chapter 4: Control structures

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

CSC 1051 Data Structures and Algorithms I

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Dept. of CSE, IIT KGP

n Group of statements that are executed repeatedly while some condition remains true

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

204111: Computer and Programming

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

UNIT- 3 Introduction to C++

Chapter 3 Problem Solving and the Computer

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Operators in java Operator operands.

8.2. ARITHMETIC SEQUENCES AND PARTIAL SUMS

Chapter 7. Expressions and Assignment Statements ISBN

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Lecture 3. More About C

isinstance and While Loops

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Transcription:

Lecture 10

Online Portal http://structuredprogramming.wikispaces.com/

Menus A menu is a list of functions that a program can perform; the user chooses the desired function from the list. E.g., 1 convert to US dollar 2 convert to pounds 3 convert to Euro 4 convert to Yen

Example CLS PRINT INPUT Enter number of Ghana cedis to be convertred, Cedis REM Display the menu PRINT PRINT Please enter one of the following numbers: PRINT TAB(4); 1 - Convert to US dollar PRINT TAB(4); 2 - Convert to pounds PRINT TAB(4); 3 - Convert to Euro PRINT TAB(4); 4 - Convert to Japanese Yen INPUT Code PRINT

REM Perform the conversion and display result CASE 1 Result = Cedis / 1.54 PRINT There are ; Result US dollars in ; cedis GHC CASE 2 Result = Cedis / 2.25 PRINT There are ; Result Pounds in ; cedis GHC CASE 3 Result = Cedis / 3.12 PRINT There are ; Result Euros in ; cedis GHC CASE 4 Result = Cedis * 0.98 PRINT There are ; Result Japanese Yen in ; cedis GHC REM If invalid code was entered, display error message CASE ELSE PRINT Invalid conversion number END SELECT END

Logical Operations An operation that acts on one or more conditions to produce a value of true or false The three most commonly used logical operators are AND, OR, and NOT. The operator AND combines two expressions and produces a value of true only when both of these conditions are true. E.g., IF (Score> 75) AND (Time < 50) THEN PRINT Nme$ END IF

Example A program to print out You passed in CSC 200/201 if score is greater than or equal to 40 and less than 50, else print Not applicable. INPUT Enter your Exam score ; Score IF Score >=40 AND Score <50 THEN PRINT You passed in CSC 200/201 ELSE PRINT Not applicable END IF END

Logical Operations (cont.) The logical operator OR also combines two expressions, but only one of the expressions needs to evaluate as true for the entire statement to be true. E.g., IF (Score> 75) OR (Time < 50) THEN PRINT Nme$ END IF

Logical Operations (cont.) The third logical operator, NOT, is a unary operator (an operator used with only one operand) and is used with a single expression The effect of the NOT is to reverse (negate) the logical value of the expression that it precedes. E.g., NOT (A<B)

Example Write a program to determine the type of triangle. REM REM side1 side3 length of each side CLS INPUT Enter the three sides, side1, side2, side3 PRINT This triangle is ; IF (side1 = side2) AND (side2 = side3) THEN PRINT equilateral ELSEIF (side1 = side2) OR (side2 = side3) OR (side1 = side3) THEN PRINT Isosceles ELSE PRINT scelene END IF END

Repetitive /Loop Control Structurest This design strategy instructs the interpreter to execute a set of instructions repeatedly for a finite number of times Looping is the process of repeating a series of statements as many times as needed The DO LOOP 1. DO WHILE LOOPLOOP 2. DO UNTIL LOOP

The DO WHILE LOOP LOOP A DO WHILE LOOP is executed as long as the expression at the top of the loop is true. If it is false, control transfers to the first statement after LOOP, which makes the loop end

Example A program to add a list of positive integers CLS Sum = 0 INPUT Enter the first number (-1 to quit: ), Number DO WHILE Number <> -1 Sum = sum + Number INPUT Enter the next number (-1 to quit: ), Number LOOP PRINT The Sum is ; Sum END

Controlling Loops Trailer values It is a data value indicating that a loop should stop executing It must be a value that would not ordinarily occur in the input data. The trailer value can be either a numeric value or a character string.

Example A program to calculate employees salaries Rate = 8 INPUT Enter name and number of hours:, Nme$, Hours DO WHILE Nme$ <> Finished Wage = Rate * Hours PRINT Name, Wage PRINT Nme$, wage PRINT INPUT Enter next name and number of hours:, Nme$, Hours PRINT LOOP PRINT Finished END

Controlling Loops Counting Loop A 2 nd method of controlling a loop is to create a loop control variable, called a counter, that t keeps track of the number of times the loop has been executed I ti l th t i i t d In counting loop, the counter is incremented or decremented during each repetition

Example of an infinite it loop Count = 1 DO WHILE Count < 50 PRINT Count LOOP

Infinite it loop, corrected Count = 1 DO WHILE Count < 50 PRINT Count Count = Count +1 LOOP

Example Rate = 8 DO WHILE Counter <= 3 INPUT Enter name and number of hours:, Nme$, Hours Wage = Rate * Hours PRINT Name, Wage PRINT Nme$, wage PRINT Counter = Counter + 1 LOOP PRINT Finished END

The DO UNTIL LOOP The 2 nd type of DO LOOP, the DO UNTIL LOOP is very similar to the DO WHILE LOOP. LOOP The only difference is that the loop is executed until the logical expression becomes true rather than while it is true

Example 1 Rate = 8 INPUT Enter name and number of hours:, Nme$, Hours DO UNTIL Nme$ = Finished Wage = Rate * Hours PRINT Name, Wage PRINT Nme$, wage PRINT INPUT Enter name and number of hours:, Nme$, Hours LOOP PRINT Finished END

Example 2

The EXIT statement t t Sometimes, it becomes necessary to exit a loop prematurely. This occurs commonly when an error condition is encountered. E.g., invalid data entry

Example A program to prompt user to enter names of 12 voters Count = 1 INPUT Enter person s name:, Voter$ INPUT Enter person ss age:, Age DO WHILE Count <= 12 REM If underage, display error message and stop loop IF Age <18 PRINT PRINT Voter$; is not eligible to vote. EXIT DO END IF Count = Count +1 INPUT Enter person s name:, Voter INPUT Enter person s name:, Voter LOOP

Assignment 6 1. (a) What is the output of the program segment : X = 7 DO WHILE X <= 1 PRINT X X = X 1 LOOP 1. (b) Write a DO WHILE LOOP that t counts by fives until a given value (limit) is reached. E.g., if value of limit is 49, the output should be similar to the following: 5 10 15 20 25 30 35 40 45

The FOR NEXT Loop The FOR NEXT is used to create a counting loop E.g., FOR X = 1 TO 6 PRINT X; NEXT X END

Find out more examples