Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Similar documents
공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Subject: PIC Chapter 2.

Programming for Electrical and Computer Engineers. Loops

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Introduction to Computing Lecture 05: Selection (continued)

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Introduction. Pretest and Posttest Loops. Basic Loop Structures ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Structured Programming. Dr. Mohamed Khedr Lecture 9

LAB 6: While and do-while Loops

Slides adopted from T. Ferguson Spring 2016

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Introduction. C provides two styles of flow control:

Fundamentals of Computer Science. Sentinel Based Repetition

Fundamental of Programming (C)

Fundamentals of Programming

Introduction to Computing Lecture 10 Arrays (Part 1)

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Module 4: Decision-making and forming loops

Computer Programming Lecture 14 Arrays (Part 2)

Fundamental of Programming (C)

Basic Assignment and Arithmetic Operators

Lecture 3. More About C

DECISION CONTROL AND LOOPING STATEMENTS

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

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

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

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

16.216: ECE Application Programming Fall 2011

Computer Programming Lecture 12 Pointers

Lecture 5: C programming

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Data types, variables, constants

C Programming Lecture V

Structured programming. Exercises 3

LAB 5: REPETITION STRUCTURE(LOOP)

CpSc 1111 Lab 4 Formatting and Flow Control

LAB 5: REPETITION STRUCTURE(LOOP)

Dept. of CSE, IIT KGP

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Comments. Comments: /* This is a comment */

'C' Programming Language

Computer Programming Unit 3

Engineering 12 - Spring, 1998

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Introduction to Programming

Programming for Engineers Iteration

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C Program Reviews 2-12

Chapter 13 Control Structures

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

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Introduction to Computing Lecture 03: Basic input / output operations

Introduction to C. Systems Programming Concepts

Chapter 11 Introduction to Programming in C

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

Chapter 13. Control Structures

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

CSCI 2132 Software Development. Lecture 8: Introduction to C

Structured programming

Worksheet 4 Basic Input functions and Mathematical Operators

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Chapter 11 Introduction to Programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

COMP 208 Computers in Engineering

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Decision Making -Branching. Class Incharge: S. Sasirekha

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Structured Program Development

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

Lecture 10: Recursive Functions. Computer System and programming in C 1

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Chapter 11 Introduction to Programming in C

Chapter 5: Control Structures

Flow Control. CSC215 Lecture

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Chapter 3 Structured Program Development

Repetition Structures

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

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

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

Programming Language A

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

16.216: ECE Application Programming Fall 2013

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

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

ECET 264 C Programming Language with Applications. C Program Control

More Programming Constructs -- Introduction

Chapter 13 Control Structures

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Introduction to Computing Lecture 08: Functions (Part I)

Loops / Repetition Statements

Lecture 10. Daily Puzzle

Fundamentals of Programming

Control Statements Loops

Transcription:

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

Topics break statement continue statement Infinite loops Nested loops while and for Macros Example Factorization

The break statement Implements the "exit loop" primitive Causes flow of control to leave a loop block (while or for) immediately

The continue statement Causes flow of control to start immediately the next iteration of a loop block (while or for)

Infinite Loops while ( 1 )...etc...etc...etc... for ( ; 1 ; )...etc...etc...etc... for ( ; ; )...etc...etc...etc... Use an: if ( condition ) break; statement to break the loop

Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum output sum Example: addpos.c

Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; output sum printf("sum = %f\n", sum); return 0;

Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1) scanf("%f", &num); sum += num; output sum printf("sum = %f\n", sum); return 0;

Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1) scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; add number to sum sum += num; output sum printf("sum = %f\n", sum); return 0;

Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum output sum include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1)) scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; sum += num; printf("sum = %f\n", sum); return 0;

Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1) scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; add number to sum sum += num; output sum printf("sum = %f\n", sum); return 0;

for and continue In the case of a for statement, control passes to the update expression. Example: for (a = 0; a < 100; a++) if (a%4) continue; printf( %d\n,a);

Nested Loops - Example: rect.c Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row

Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for each row for each column in the current row print an asterisk start next row return 0;

Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) return 0;

Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) return 0;

Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); return 0;

Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;

Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;

Example: rect.c (cont) Print an m by n rectangle of asterisks algorithm input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; program printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;

Variation: rect2.c Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); i = 0; while (i < n) for (j=0; j < m; j++) printf("*"); printf("\n"); i++; return 0;

Variation: rect3.c Print an m by n rectangle of asterisks input width and height #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for each row for each column in the current row print an asterisk start next row for (i=0; i < n; i++) j = 0; while (1) printf("*"); j++; if (j == m) break; printf("\n"); return 0;

Variation: rect3.c (cont) #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; The innermost enclosing loop for this break is the while-loop printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) j = 0; while (1) printf("*"); j++; if (j == m) break; printf("\n"); return 0;

while and for A for loop can always be rewritten as an equivalent while loop, and vice-versa The continue statement in a for loop passes control to the update expression

ASCII (American Standard Code for Information Interchange) Is a character encoding based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that work with text There are 128 characters (0-127) First 32 (0-31) are control characters and are not printable

Example: asciicheck.c while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) break; else printf("bad bounds. Try again.\n");

Example: asciicheck.c while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) break; else printf("bad bounds. Try again.\n");

Example: asciiprint Print out a section of the ASCII table for each character from the lower bound to higher bound print its ascii value and ascii character for ( ch = low; ch <= high; ch++ ) printf("%d: %c\n", ch, ch); asciiprint1.c ch = low; while ( ch <= high ) printf("%d: %c\n", ch, ch); ch++; asciiprint2.c

Example: asciiprint (cont) for ( ch = low; ch <= high; ch++ ) printf("%d: %c\n", ch, ch); asciiprint1.c ch = low; while (1) printf("%d: %c\n", ch, ch); if (ch < high) ch++; continue; else break; asciiprint3.c

Example: asciiprint (cont) for ( ch = low; ch <= high; ch++ ) printf("%d: %c\n", ch, ch); asciiprint1.c ch = low; for (;;) printf("%d: %c\n", ch, ch); ch++; if (ch > high) break; asciiprint4.c

Example: ascii1.c #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) break; else printf("bad bounds. Try again.\n"); for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); return 0;

Example: ascii1.c (cont) #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) break; else printf("bad bounds. Try again.\n"); for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); return 0;

Example: ascii1.c (cont) #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) break; else printf("bad bounds. Try again.\n"); Macro definition: for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); #define identifier tokens return 0; All subsequent instances of identifier are replaced with its token

Output of Ascii1.c Enter bounds (low high): 32 126 32: 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 51:3 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 61:= 62:> 63:? 64:@ 65:A 66:B 67:C 68:D 69:E 70:F 71:G 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 81:Q 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 91:[ 92:\ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 101:e 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 111:o 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 121:y 122:z 123: 124: 125: 126:~

Example : Factorization Write a program which prints out the prime factorization of a number (treat 2 as the first prime) For example, on input 6, desired output is: 2 3 " " 24, " " : 2 2 2 3 " " 14, " " : 2 7 " " 23, " " : 23 (23 is prime)

Algorithm input n set factor to 2

Algorithm (cont) input n set factor to 2 while(some factor yet to try)

Algorithm (cont) input n set factor to 2 while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor

Algorithm (cont) input n set factor to 2 while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor else increment factor

Algorithm (cont) input n Why not? set factor to 2 while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor else increment factor while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor increment factor

#include <stdio.h> Program /* Print out the prime factors of a number */ int main() int n, factor ; return 0;

#include <stdio.h> Program (cont) /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; return 0;

#include <stdio.h> Program (cont) /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; printf("\nthe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ printf("\n\n"); return 0;

/* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 )

/* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ;

/* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

/* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

#include <stdio.h> /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; printf("\nthe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ; printf("\n\n"); return 0; factor1.c

/* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) Change from /* n is a multiple while-loop of factor, to for- ** so print factor and divide n by factor */ loop? printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

/* Try each possible factor in turn */ for ( factor = 2; factor <= n && n > 1 ; ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

#include <stdio.h> /* Print out the prime factors of a number. */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; printf("\nthe prime factors of %d are: ", n) ; /* Try each possible factor in turn. */ for ( factor = 2; factor <= n && n > 1 ; ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor. */ factor++ ; printf("\n\n"); return 0; factor2.c