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

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

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

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

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

Introduction. C provides two styles of flow control:

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

Flow Control. CSC215 Lecture

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

Module 4: Decision-making and forming loops

Subject: PIC Chapter 2.

Programming for Electrical and Computer Engineers. Loops

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

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

SELECTION STATEMENTS:

Programming for Engineers Iteration

Structured Programming. Dr. Mohamed Khedr Lecture 9

Lecture 10. Daily Puzzle

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

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

Decision Making and Loops

Government Polytechnic Muzaffarpur.

Loops / Repetition Statements

Chapter 13 Control Structures

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Chapter 13. Control Structures

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

DECISION CONTROL AND LOOPING STATEMENTS

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

Prepared by: Shraddha Modi

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

CSE 130 Introduction to Programming in C Control Flow Revisited

Lecture 7 Tao Wang 1

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

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

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?

Loops / Repetition Statements

Problem Solving and 'C' Programming

Iterative Languages. Scoping

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

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

Chapter 4: Control structures. Repetition

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

REPETITION CONTROL STRUCTURE LOGO

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

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

Dept. of CSE, IIT KGP

Chapter 4: Control structures

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

Control structures in C. Going beyond sequential

Conditional Statement

Quiz Determine the output of the following program:

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

16.216: ECE Application Programming Fall 2011

More examples for Control statements

Computer Programming Unit 3

Chapter 13 Control Structures

Computers Programming Course 7. Iulian Năstac

Chapter 4. Flow of Control

Overview. CS 201 Repetition. Endfile-controlled loops. What is the Output? Nested Loops. Loops. Debzani Deb

C: How to Program. Week /Mar/05

Repetition Structures

Repetition Structures II

MODULE 2: Branching and Looping

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

Repetition and Loop Statements Chapter 5

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

Why Is Repetition Needed?

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

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

CS1100 Introduction to Programming

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;

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

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

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

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

ITEC114 Structured Programming Midterm Exam, Spring 2015/ April Instructor: Akile Oday Research Assistant: Mohammed Abubakar SOLUTIONS

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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

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

Crude Video Game Simulator Algorithm

'C' Programming Language

Iteration. Side effects

YOLOP Language Reference Manual

C Programming Language

Functions. (transfer of parameters, returned values, recursion, function pointers).

LESSON 6 FLOW OF CONTROL

Chapter 4 C Program Control

Chapter 4 C Program Control

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

C - Basics, Bitwise Operator. Zhaoguo Wang

Lab 09: Advanced SQL

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

do, while and for Constructs

Unit 3 Decision making, Looping and Arrays

Loops (while and for)

C Program Reviews 2-12

For Loop. Variations on Format & Specific Examples

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Transcription:

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

LOOPS WHILE AND FOR while syntax while (expression) statement The expression is evaluated. If it is true (non-zero), statement is executed and expression is re-evaluated. This cycle continues until expression becomes zero, at which point execution resumes after statement. 1

LOOPS WHILE AND FOR for syntax 1 4 for (expr1; expr2; expr3) statement 2 3 Grammatically, the three components of a for loop are expressions. Most commonly, expr1 and expr3 are assignments or function calls and expr2 is a relational expression. Any of the three parts can be omitted, although the semicolons must remain. If the test, expr2, is not present, it is taken as permanently true, so is an infinite loop, presumably to be broken by other means, such as a break or return. 2

LOOPS WHILE AND FOR for (expr1; expr2; expr3) statement equivalent int i, sum = 0; const int MAX = 10; for(i = 1; i <= MAX; i++) sum += i; printf("sum from 1 to %d is : %d\n", MAX, sum); expr1; while (expr2) statement expr3; int i = 1, sum = 0; const int MAX = 10; while (i <= MAX) sum += i; i++; printf("sum from 1 to %d is : %d\n", MAX, sum); 3

LOOPS WHILE AND FOR Whether to use while or for is largely a matter of personal preference. For example, in while ((c = getchar()) == ' ' c == '\n' c = '\t')... there is no initialization or re-initialization, so the while is most natural. The for is preferable when there is a simple initialization and increment since it keeps the loop control statements close together and visible at the top of the loop. This is most obvious in for (i = 0; i < n; i++)... 4

BREAK The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately. 5

BREAK int input, i, sum; while(1) sum = 0; printf("enter a positive integer or 0 for END :"); scanf("%d", &input); if(input <= 0) break; for(i=1;i <= input; i++) sum += i; printf("sum from 1 to %d : %d\n", input, sum); printf("end the Program.\n"); 6

CONTINUE The continue statement causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately. In the for loop, control passes to the increment step. The continue statement applies only to loops, not to switch. 7

CONTINUE int input, i; printf("enter a positive integer greater than 3: \n"); scanf("%d", &input); printf("multiples of 3 from 1 to %d are : ", input); for(i = 1; i<=input; i++) if(i % 3!= 0) continue; printf("%d%s", i, (input-i)<3? \n :, ); 8

INFINITE LOOP An infinite loop (endless loop) is a sequence of instructions in a computer program which loops endlessly. If it is not intentional, the loop can be terminated by ctrl+c If you want to make an infinite loop, use non-zero as the conditional expression while and do while without conditional expression causes an error infinite loop error 9

NESTED LOOP Nested loop a loop within a loop outer loop inner loop for (m = 1; m <= 4; m++) for (n = 1; n <= 3; n++) statements outer loop control variable m; iterates 1 to 4 inner loop control variable n; iterates 1 to 3 Loop start Loop end 10

NESTED LOOP int i, j; for (i = 1; i <= 5; i++) for (j = 1; j <= i; j++) printf("*"); printf("\n"); Loop start Loop end 11