Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

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

Chapter 2: Functions and Control Structures

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Introduction. C provides two styles of flow control:

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Chapter 4 C Program Control

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

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

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops / Repetition Statements

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

C++ Programming: From Problem Analysis to Program Design, Third Edition

LECTURE 5 Control Structures Part 2

OER on Loops in C Programming

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

REPETITION CONTROL STRUCTURE LOGO

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Lecture 7 Tao Wang 1

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

In this chapter you will learn:

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Repetition Structures

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

Structured Programming. Dr. Mohamed Khedr Lecture 9

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

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

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

Quiz 1: Functions and Procedures

Problem Solving and 'C' Programming

Introduction to C/C++ Lecture 3 - Program Flow Control

Repetition and Loop Statements Chapter 5

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

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

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

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

Structure of Programming Languages Lecture 5

DELHI PUBLIC SCHOOL TAPI

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Loops. CSE 114, Computer Science 1 Stony Brook University

Information Science 1

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Information Science 1

Programming for Engineers Iteration

Iteration statements - Loops

Module 4: Decision-making and forming loops

Structured Program Development in C

Decision Making and Loops

Computer Science III WEEK4 Dr.Elgin KILIÇ

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Chapter 3 Structured Program Development


Chapter 5: Control Structures II (Repetition)

Object-Oriented Programming in Java


Chapter 3. Iteration

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Chapter 4 C Program Control

Unit 3 Decision making, Looping and Arrays

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

Why Is Repetition Needed?

Control Structures II. Repetition (Loops)

Computer Programming: C++

Chapter 4 Introduction to Control Statements

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

204111: Computer and Programming

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Introduction to Java & Fundamental Data Types

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

Control Structures (Deitel chapter 4,5)

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Control Statements. Musa M. Ameen Computer Engineering Dept.

Control Statements: Part Pearson Education, Inc. All rights reserved.

EP241 Computing Programming

Loops / Repetition Statements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 5: Loops and Files

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

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Programming Fundamentals

3 The L oop Control Structure

Control Statements: Part Pearson Education, Inc. All rights reserved.

Chapter 2 - Control Structures

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

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

Unit 1 Lesson 4. Introduction to Control Statements

Fundamentals of Programming Session 9

Computer Programming: C++

Lecture 7: General Loops (Chapter 7)

Le L c e t c ur u e e 3 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 Control Statements

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Repetition, Looping CS101

Transcription:

COMPUTER PROGRAMMING SKILLS (4800153-3) CHAPTER 5: REPETITION STRUCTURE Second Term (1437-1438) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

Table of Contents Objectives & Outline Objectives 1. Use basic problem-solving of selection. 2. Develop algorithms of loop. 3. Use the repetition statements in C. 4. Write a C program using repetition statements: while, for, do...while, and nested loop. Umm al-qura University Chapter 5 (Medical & Administrative) 1/22

Table of Contents Objectives & Outline Outline 1. Repetition structures in C 2. while statement 3. for statement 4. do..while statement 5. Nested loop statement Umm al-qura University Chapter 5 (Medical & Administrative) 2/22

Repetition structures in C Overview Introduction Loops are used to execute a block of code several number of times. A loop statement allows you to specify that an action is to be repeated while some condition remains true. There are three types of loops: for, while, and do..while. Each of them has their specific uses. Umm al-qura University Chapter 5 (Medical & Administrative) 3/22

Repetition structures in C Overview Loop Type while loop do...while loop for loop nested loops Description Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. Like a while statement, except that it tests the condition at the end of the loop body Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. You can use one or more loop inside any another while, for or do... while loop. Umm al-qura University Chapter 5 (Medical & Administrative) 4/22

Repetition structures in C Overview Counter-Controlled Repetition Counter-controlled repetition requires: 1. The name of a control variable (or loop counter). 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop. 4. The condition that tests for the final value of the control variable (i.e., whether looping should continue). Umm al-qura University Chapter 5 (Medical & Administrative) 5/22

while statement Overview Description 1. Initialization of the control variable, which will be used to test the condition, is applied only once. 2. If the condition is true, the statement and the increment are executed, then the whole thing is done again. 3. The statement and the increment are executed repeatedly until the condition becomes false. 4. If the condition starts out false, the while-body will never be executed. 5. Increment is the statement that makes change on the condition, Otherwise, the loop will continue running (Infinite loop). Umm al-qura University Chapter 5 (Medical & Administrative) 6/22

while statement Syntax 1 while ( condition ) { 3 statement ( s ) ; } Listing 1: Syntax of a while loop in C programming language Figure 1: The flowcart of while Statement Umm al-qura University Chapter 5 (Medical & Administrative) 7/22

while statement Example # include < s t d i o. h> 2 i n t main ( void ) { 4 i n t counter = 1 ; // i n i t i a l i z a t i o n while ( counter <= 10 ) { // r e p e t i t i o n condition 6 p r i n t f ( "%d\ t ", counter ) ; // display counter ++counter ; // increment 8 } return 0 ; 10 } Listing 2: A program to print numbers from 1 to 10. Output of Listing.2 1 2 3 4 5 6 7 8 9 10 Umm al-qura University Chapter 5 (Medical & Administrative) 8/22

for statement Syntax f o r ( I n i t i a l i z e ; condition ; increment ) 2 { statement ( s ) ; 4 } Listing 3: Syntax of a for loop in C programming language Umm al-qura University Chapter 5 (Medical & Administrative) 9/22

for statement Overview Definition The Initialize step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). If the condition becomes false, the for loop terminates. Umm al-qura University Chapter 5 (Medical & Administrative) 10/22

for statement Overview Figure 2: The flowcart of for Statement Umm al-qura University Chapter 5 (Medical & Administrative) 11/22

for statement Example # include < s t d i o. h> 2 i n t main ( void ) { 4 i n t counter ; // define counter // i n i t i a l i z a t i o n, r e p e t i t i o n condition, and increment are 6 // a l l included in the f o r statement header. f o r ( counter = 1 ; counter <= 1 0 ; ++counter ) { 8 p r i n t f ( "%d\ t ", counter ) ; } // end f o r 10 return 0 ; } Listing 4: A program to print the numbers from 1 to 10 Output of Listing.4 1 2 3 4 5 6 7 8 9 10 Umm al-qura University Chapter 5 (Medical & Administrative) 12/22

for statement Example Figure 3: for statement header components Umm al-qura University Chapter 5 (Medical & Administrative) 13/22

for statement Example Figure 4: Flowcharting a typical for repetition statement Umm al-qura University Chapter 5 (Medical & Administrative) 14/22

for statement Equivalence of for and while statements 1 f o r ( expression1 ; expression2 ; expression3 ) { statement } Listing 5: General Format of a for Statement expression1 initializes the loop-control variable, expression2 is the loop-continuation condition, expression3 increments the control variable. In most cases, the for statement can be represented with an equivalent while statement as follows: expression1 ; 2 while ( expression2 ) { statement 4 expression3 ; } Listing 6: General Format of a while Statement Umm al-qura University Chapter 5 (Medical & Administrative) 15/22

do...while statement Overview Definition The do... while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition is tested at the beginning of the loop before the body of the loop is performed. The do... while statement tests the loop-continuation condition after the loop body is performed. Therefore, the loop body will be executed at least once. When a do... while terminates, execution continues with the statement after the while clause. do { 2 statement } while ( condition ) ; Listing 7: Syntax of do...while statement Umm al-qura University Chapter 5 (Medical & Administrative) 16/22

do...while statement Overview Figure 5: The flowcart of do...while Statement Umm al-qura University Chapter 5 (Medical & Administrative) 17/22

do...while statement Example 1 # include < s t d i o. h> i n t main ( void ) 3 { i n t counter = 1 ; // i n i t i a l i z e counter 5 do { p r i n t f ( "%d \ t ", counter ) ; // display counter 7 } while ( ++counter <= 10 ) ; // end do... while return 0 ; 9 } Listing 8: Using the do... while repetition statement. Output of Listing.8 1 2 3 4 5 6 7 8 9 10 Umm al-qura University Chapter 5 (Medical & Administrative) 18/22

Nested loop statement Overview Definition C programming allows to use one loop inside another loop. In a nested loop, we can put any type of loop inside any other type of loop. For example, a for loop can be inside a while loop or vice versa. 1 f o r ( i n i t ; condition ; increment ) { f o r ( i n i t ; condition ; increment ) { 3 statement ( s ) ; } 5 statement ( s ) ; } Listing 9: Example of nested loop: A nested for loop statement Umm al-qura University Chapter 5 (Medical & Administrative) 19/22

Nested loop statement Example # include < s t d i o. h> 2 i n t main ( ) { i n t i, j ; 4 f o r ( i = 2 ; i <30; i ++) { f o r ( j = 2 ; j <= ( i / j ) ; j ++) 6 i f (! ( i%j ) ) break ; // i f f a c t o r found, not prime i f ( j > ( i / j ) ) p r i n t f ( "%d i s prime\n", i ) ; 8 } return 0 ; 10 } Listing 10: Use a nested for loop to find the prime numbers from 2 to 30 Umm al-qura University Chapter 5 (Medical & Administrative) 20/22

Nested loop statement Example Output of Listing.10: 2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime Umm al-qura University Chapter 5 (Medical & Administrative) 21/22

References C How to Program, 7th Edition, Paul Deitel, Deitel & Associates, Inc. Harvey Deitel. 2013 Pearson. C Language Tutorials: an easy way of learning C, Ghulam Murtaza Dahar, 2013. Umm al-qura University Chapter 5 (Medical & Administrative) 22/22