Lec 6 Loop Statements Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
CS150 Introduction to Computer Science 1
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Chapter 4 – C Program Control
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Control Statements: Part 2
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Control Statements Kingdom of Saudi Arabia
Control Structures Lecture 7.
Arrays, For loop While loop Do while loop
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
- Additional C Statements
Structured Program Development in C
Lecture Notes – Week 3 Lecture-2
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 4 - Program Control
for, do-while and switch statments
Program Control Topics While loop For loop Switch statement
Structured Program Development in C
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Lec 6 Loop Statements Introduction to Computer Programming KIC 5/22/2019 Department of Information Technology. Introduction to Computer Programming Lec 6 Loop Statements KIC/Introduction to Computer Programming 5/22/2019 Introduction to Computer Programming

KIC/Introduction to Computer Programming Outlines Repetition Statements for repetition statements while repetition statements do…while repetition statements 5/22/2019 KIC/Introduction to Computer Programming

KIC/Introduction to Computer Programming All programs can be broken down into 3 controls: Sequence–handled automatically by compiler Selection – if, if…else , switch Repetition – for, while, do…while 5/22/2019 KIC/Introduction to Computer Programming

Repetition Statements Computer executes group of instructions repeatedly while some condition remains true Counter-controlled repetition: Definite repetition: know how many times loop will execute Control variable used to count repetitions Counter-controlled repetition requires: The name of a control variable (or loop counter) The initial value of the control variable An increment (or decrement) by which the control variable is modified each time through the loop A condition that tests for the final value of the control variable (i.e., whether looping should continue) 5/22/2019 KIC/Introduction to Computer Programming

for Repetition Statements {body for the loop (statements) } 12345678910 for (int count = 1; count < = 10; count++) { printf (“%d ”, count); } No semicolon (;) after last expression 5/22/2019 KIC/Introduction to Computer Programming

KIC/Introduction to Computer Programming 5/22/2019 KIC/Introduction to Computer Programming

7 Example Program Output 1 2 3 4 5 6 7 8 9 10

What does the following program print? #include <stdio.h> int main(){ int i,j=1; for (i=0;i<10;i+=2) { j++; printf("%d\n", j); } getchar(); j For Loop Start 1 i = 0 2 i = 2 3 i = 4 4 i = 6 5 i = 8 6

while Repetition Statements The format Initialization (counter) while (<testing> or <condition>) { body for the loop (statements) increment for the counter } Example: int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition printf( "%d\n", counter ); ++counter; // increment } The statement int counter = 1; Names counter Defines it to be an integer Reserves space for it in memory Sets it to an initial value of 1 5/22/2019 KIC/Introduction to Computer Programming

Example Program Output 1 2 3 4 5 6 7 8 9 10

Comparison (While Loop Vs. For Loop) If we want to display the numbers from 0 to 10 then both loop statements will be as below: int count = 0; while ( count < = 10 ) { printf (“ \n %i ”, count); count++; } Initialization Increment int count; for (count = 0; count < = 10; count++) { printf (“ \n %i ”, count); } 5/22/2019 KIC/Introduction to Computer Programming

Comparison (While Loop Vs. For Loop) Example 5/22/2019 KIC/Introduction to Computer Programming

do…while Repetition Statements The format Initialization (counter) do { body for the loop (statements) increment for the counter } while (<testing> or <condition>); 1 2 3 4 5 6 7 8 9 10 Example int count = 0; do { printf (“ \n %d ”, count); count++; or count=count+1; } while (count<=10); 5/22/2019 KIC/Introduction to Computer Programming

Example

Comparison (While Loop Vs. do.. While Loop) 5/22/2019 KIC/Introduction to Computer Programming

KIC/Introduction to Computer Programming Questions ( (6 Describe what happens in the following loop: int d=7 int c=2 while(c!=d){ c=c+2;} 5/22/2019 KIC/Introduction to Computer Programming