IT 1033: Fundamentals of Programming Loops

Similar documents
CSC 1051 Data Structures and Algorithms I

Quiz Determine the output of the following program:

LEC03: Decisions and Iterations

Fundamentals of Programming. Budditha Hettige Department of Computer Science

Banner HR Leave Entry/Approver Guide Monthly CHS and OSU-Tulsa. Revised

Arrays IT 1033: Fundamentals of Programming

Problem Solving and Algorithms

Loops / Repetition Statements

[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";

3 The L oop Control Structure

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Fundamentals of Programming

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Chapter 1 Lab Algorithms, Errors, and Testing

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

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

Chapter 4: Control structures. Repetition

Fundamentals of Programming Data Types & Methods

Introduction to the Java Basics: Control Flow Statements

A Look Back at Arithmetic Operators: the Increment and Decrement

IDENTIFY WAYS OF REPRESENTING ALGORITHMS.

Lab 09: Advanced SQL

REPETITION CONTROL STRUCTURE LOGO

Chapter 4: Control structures

Repetition Structures

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

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

LAB 5: REPETITION STRUCTURE(LOOP)

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Control Structures II. Repetition (Loops)

Chapter Two: Program Design Process and Logic

Fundamentals of Programming Session 13

PROBLEM SOLVING WITH LOOPS. Chapter 7

CS111: PROGRAMMING LANGUAGE II

Algorithms and Conditionals

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

LAB 5: REPETITION STRUCTURE(LOOP)

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

Course Outline. Introduction to java

CMPT 125: Lecture 4 Conditionals and Loops

Loops. CSE 114, Computer Science 1 Stony Brook University

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Fundamental of Programming (C)

Loops / Repetition Statements

Program Control Flow

Program Control Flow

Fundamentals of Programming

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Theory of control structures

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Programming for Engineers Iteration

Loops (while and for)

Introduction to Computer Science using JAVA

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

CSC 121 Spring 2017 Howard Rosenthal

Building Java Programs

IMS1906: Business Software Fundamentals Tutorial exercises Week 5: Variables and Constants

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

COMP 111. Introduction to Computer Science and Object-Oriented Programming

OER on Loops in C Programming


Chapter 4 Introduction to Control Statements

Computational Physics - Fortran February 1997

BRANCHING if-else statements

Information Science 1

Flow Charts. Visual Depiction of Logic Flow

Information Science 1

Kadi Sarva Vishwavidyalaya, Gandhinagar

11.3 Function Prototypes

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

(I m not printing out these notes! Take your own.)

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

COMPUTER SCIENCE. Paper 1

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Multiple Choice Questions ( 1 mark)

Unit 1 Lesson 4. Introduction to Control Statements

Agenda. Strings 30/10/2009 INTRODUCTION TO VBA PROGRAMMING. Strings Iterative constructs

Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming

Unit 7. Lesson 7.1. Loop. For Next Statements. Introduction. Loop

Dept. of CSE, IIT KGP

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

Repetition with for loops


Introduction. C provides two styles of flow control:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

STUDENT LESSON A12 Iterations

Repetition, Looping CS101

Repetition Structures Chapter 9

Lecture 7 Tao Wang 1

CS110D: PROGRAMMING LANGUAGE I

Transcription:

IT 1033: Fundamentals of Programming Loops Budditha Hettige Department of Computer Science

Repetitions: Loops A loop is a sequence of instruction s that is continually repeated until a certain condition is reached. The statement may be repeated For a specific number of items For an indeterminate number of times, depending on the truth or falsity of some condition.

Loops C++ provides three types of loops for loops (1- n times) Repeat a section of code known number of times while loops (0 more times) Loop is used to repeat a specific block of code an unknown number of times do while loops (1 more times) A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block

Repeat some work Do some repeated work Initialization (Start number) Condition (do repeat action until satisfy some condition) Update (Next Value) Example Initialization Start with 1 Condition Count up to 50 Update Count 1 by 1

For Loop Syntax for (initialization; condition; update) { statement(s) } Example Initialization Condition Update

Exercise 6.1 Write a C++ program to find the factorial of a given number Example:

Answer

Nested for Loops A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting. Syntax for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }

Exercise 6.2 Write a C++ program to display the following Multiplication table

Exercise 6.3 1. Write a c++ program to print the following figure a) * b) 1 ** 12 *** 123 **** 1234 ***** 12345

While Loop Allows the repetition of a statement based on the truth value of a condition Can run 0 to infinite times

While Loop Syntax while (Condition) { statement(s) } Example Initialization Condition Update

Exercise 6.4 Write a C++ program to accept numbers until the user enters a 999 and output the sum of the given numbers

Exercise 6.5 Create a java program to identify the given number is Palindrome number or not. Read number as an integer Find the number is Palindrome or not Print the results Hint: A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461

Answers

Do-while Loop do while loop also depends on a condition, but unlike while loop, its condition is evaluated at the bottom of the loop, after the body has already executed.

Do-while Loop Syntax do { statement(s) } while (condition); Example Initialization Condition Update

Loops comparison For while do-while

Loops comparison

Exercise 6.6 Accept numbers until the user enters a 999 and output the average of the given numbers

Exercise 6.7 Using do-while loop, create a java program to display the selected option in the following menu; [1] Enter data [2] Print data [3] Exit the program do-while Switch

Answer

Exercise 6.8 Write a C++ program to read N number of integers and find the total and average. N is an input 1, 2, 3.. N Use for, while and do-while loops Draw 3 flow chart for the above 3 programs

Exercise 6.9 Write a C++ program to compute the gross pay for an employee. An employee is paid at hourly rate for the first 40 hours worked in a week. Any hours worked in excess of 40 hours are paid at the overtime rate of one and half times that. Your program should print the pay sheets of all the employees.