Structured Programming. Dr. Mohamed Khedr Lecture 9

Similar documents
Chapter 4 C Program Control

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

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Loops / Repetition Statements

Dept. of CSE, IIT KGP

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

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

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

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.

Chapter 4: Control structures. Repetition

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

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

Chapter 4: Control structures

Repetition and Loop Statements Chapter 5

In this chapter you will learn:

Chapter 4 C Program Control

Loops / Repetition Statements

Looping. Arizona State University 1

Chapter 3 Structured Program Development

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

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Control structures in C. Going beyond sequential

Structured Program Development

Programming for Engineers Iteration

Chapter 2, Part III Arithmetic Operators and Decision Making

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

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

Lecture 10. Daily Puzzle

A Look Back at Arithmetic Operators: the Increment and Decrement

Structured Program Development in C

Flow Control. CSC215 Lecture

Control Structure: Loop

Programming for Engineers Introduction to C

C Functions. 5.2 Program Modules in C

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?

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 5: Loops and Files

3 The L oop Control Structure

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

Chapter 7. Additional Control Structures

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

Problem Solving and 'C' Programming

Fundamental of Programming (C)

Module 4: Decision-making and forming loops

Chapter 2 - Control Structures

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

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

C: How to Program. Week /Mar/05

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Repetition Structures

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

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

Lab 2: Structured Program Development in C

Introduction. C provides two styles of flow control:

CS110D: PROGRAMMING LANGUAGE I

Chapter 2 - Introduction to C Programming

REPETITION CONTROL STRUCTURE LOGO

Lecture 7 Tao Wang 1

Control Statements. Musa M. Ameen Computer Engineering Dept.

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

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

Why Is Repetition Needed?

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

Chapter 5: Control Structures

C: How to Program. Week /Apr/16

Unit 3 Decision making, Looping and Arrays

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

Lecture 6. Statements

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

204111: Computer and Programming

EK131 E5 Introduction to Engineering

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

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

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

C: How to Program. Week /Apr/23

Chapter 4. Flow of Control

Lecture 04 FUNCTIONS AND ARRAYS

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

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

Repetition, Looping CS101

Decision Making and Loops

Control Structures II. Repetition (Loops)

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

Chapter 2, Part I Introduction to C Programming

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.

LAB 5: REPETITION STRUCTURE(LOOP)

Full file at C How to Program, 6/e Multiple Choice Test Bank

Fundamentals of Programming Session 8


Repetition Structures II

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

Transcription:

Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1

Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop body changes and this causes the repeating to stop

The Repetition Statement Establish initial value of control variable counter = 1 true Determine if final value of control variable has been reached false Body of loop (this may be many statements) Increment the control variable 3

Repetition Structure: for for loops syntax for ( initialization ; loopcontinuationtest ; increment ) statement Example: Prints the integers from one to ten for ( counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter ); For loops can usually be rewritten as while loops: initialization; while ( loopcontinuationtest ) { statement; increment; } Initialization and increment Can be comma-separated list of statements Example: for ( i = 0, j = 0; j + i <= 10; j++, i++) printf( "%d\n", j + i ); No semicolon (;) after last expression 4

Arithmetic expressions The for Structure (cont.) Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) Notes about the for structure: "Increment" may be negative (decrement) If the loop continuation condition is initially false The body of the for structure is not performed (i.e. pre-test) Control proceeds with the next statement after the for structure Control variable Often printed or used inside for body, but not necessarily 5

The for Structure (cont.) 1 /* Fig. 4.5: fig04_05.c 2 Summation with for */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int sum = 0, number; 8 9 for ( number = 2; number <= 100; number += 2 ) 10 sum += number; 11 12 printf( "Sum is %d\n", sum ); 13 14 return 0; 15 } 1. Initialize variables 2. for repetition structure Program Output: Sum is 2550 2 + 4 + 8 + +100 = 2550 6

for = = while for(expr1;expr2;expr3) statement; Is equivalent to: expr1; while(expr2){ } statement; expr3; This will create an infinite loop: for(;;){...} 7

Comma in For Loops Can put commas in for loops Evaluated left to right #include <stdio.h> int main() { int a=0,b=0; for(a=0, b=10; a<b; a++, b--) printf("a=%d b=%d\n",a,b); return 0; 8

Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { printf( % d Potato \n, num ); } 9

num? Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); OUTPUT 10

num 1 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); OUTPUT 11

num 1 Example of Repetition int num; true for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); OUTPUT 12

num 1 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato OUTPUT 13

num 2 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato OUTPUT 14

num 2 Example of Repetition int num; true for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato OUTPUT 15

num 2 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato 2Potato OUTPUT 16

num 3 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato 2Potato OUTPUT 17

num 3 Example of Repetition int num; true for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato 2Potato OUTPUT 18

num 3 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato 2Potato OUTPUT 3Potato 19

num 4 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato 2Potato OUTPUT 3Potato 20

num 4 Example of Repetition int num; false for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); 1Potato 2Potato OUTPUT 3Potato 21

num 4 Example of Repetition int num; false for ( num = 1 ; num <= 3 ; num++ ) printf( % d Potato \n, num ); When the loop control condition is evaluated and has value false, the loop is said to be satisfied and control passes to the statement following the For statement. 22

The output was: 1Potato 2Potato 3Potato 23

int count ; Count-controlled Loop for ( count = 4 ; count > 0 ; count-- ) { printf( %d \n, count ) ; } printf ( Done ) ; OUTPUT: 4 3 2 1 Done 24

What is the output? int count; for ( count = 0 ; count < 10 ; count++ ) { printf( * ); } 25

OUTPUT ********** NOTE: the 10 asterisks are all on one line. Why? 26

For Loop Variations any expression may be more complex: for (i=100*y; i>=1; i--) for (i=100; i>=y/2; i--) for (i=100; i>=1; i-=4*y) 27

For Loop Variations Increment may be negative: for (i=100; i>=1; i--) This counts from 100 to 1. Increment may be greater than 1: for (i=100; i>=5; i-=5) This counts from 100 to 5 in steps of 5 28

What output from this loop? int count; for (count = 0; count < 10; count++) ; { printf ( * ); } 29

One *! Why? OUTPUT the ; right after the ( ) means that the body statement is a null statement in general, the Body of the for loop is whatever statement immediately follows the ( ) that statement can be a single statement, a block, or a null statement actually, the code outputs one * after the loop completes its counting to 10 30

Infinite Loop You can still end up with an infinite loop when using for loops for (counter=0; counter<=10; counter--) 31

1 2!"##" $ % 3 &# %'( 4 &# $"''( 5 additional header 6 $" )% "$ * 7 $" + 8, 9 )# "$ "$ % 10 )# "# %" "# 11 )# " - ""# % " 12."." 13 14 ")# #$ '" 15 %/%0 1" 2$ % 16 17 "##" "$ % "'."% 18.".".", 19 20 "##" 3 "$ %." 21 "$ "# 3 "." 22 23 ")# 3 24 //0." "$ 25 4 26 27 " "$ %%%##. 28 29 4 $" Outline pow function calculates the value of the first argument raised to the power of the second argument 32

1" 2$ % - / /- 5-65 /-- - /6/7 5 6 6 7 66 8 --55-55 /778 Outline 33

Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) { printf ("*"); } printf ("\n"); } 34

Nested For Loops, Example #2 #include <stdio.h> main () { int rows, columns; } for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) { printf ("*"); } } printf ("\n"); 35

Nested Loops initialize outer loop while ( outer loop condition ) {... } initialize inner loop while ( inner loop condition ) { inner loop processing and update }...

Example Write a program that displays the multiplication tables ( 1-12 ). 1 x 1 = 1 1 x 2 = 2. 1 x 12 = 12 2 x 1 = 2. 12 x 12 = 144 37