Iteration statement while do-while

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Some loop programs 1: Read in 10 integers and output their sum
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
ECE Application Programming
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
Looping.
Arrays, For loop While loop Do while loop
Loop Control Structure.
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
CMPT 102 Introduction to Scientific Computer Programming
UMBC CMSC 104 – Section 01, Fall 2016
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Iteration Statement for
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Iteration statement while do-while 350142 - Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Repetition Control Structures Iteration Statements for(…) Statement while(…) Statement do…while (…) Statement Repetition Control Statement break Statement continue Statement

Repetition Control Statements Problem: If you need to print the message “Hello, world” 10 messages onto the monitor screen. How will you do? main() { repetition statement printf(“Hello, world\n”); } main() { printf(“Hello, world\n”); … } An easier way to do that is using a repetition control statement. Use 10 printf() functions What about 100 messages!

Repetition Control Statements (cont.) In some situation, programs require the ability to execute a set of statements repeatedly. There are three repetition statements in C language: for, while, do…while statements. The for, while, do…while statements each have a Logic expression determining whether the loop should continue or terminate. Main sequence Loop (or Repeated) Statements Loop

while Statement while Statement (Step-by-Step) Check the condition of while statement If condition is true Execute statements in while block And then jump back to check condition again If condition is false, ignore statements in while block while (condition) statement; while (condition) { statement-1; … statement-n; }

while Statement: Flow chart(1) false true

while Statement: Flow chart (2)

Example 1 4 2 1 3 #include <stdio.h> int main(int argc, char **argv) { int count = 0; printf(“Show number from zero to three.\n”); while (count <= 3) printf(“%d ”, count); count++; } 4 2 1 3 count What will be happened if we forget to write “count++” in this program ? Output : Show number from zero to three. 1 2 3

Example 2 2 3 1 ? 2 1 3 #include <stdio.h> int main(int argc, char **argv) { int begin = 0, sum = 0, end; printf(“Enter end number : ”); scanf(“%d”, &end); while(begin <= end) { sum = sum + begin; begin++; } printf(“Sum = %d”, sum); 2 3 1 begin ? 2 end 1 3 sum Output : Enter end number : 2 Sum = 3

QUIZ 1 Find the output of this program #include <stdio.h> int main(int argc, char **argv) { int count = 3; while (count >= 0) { printf(“%d ”, count); count--; } printf(“count = %d\n”, count);

do-while Statement do-while Statement is similar to while Statement The difference is do-while Statement will execute statements in do-while block before checking the condition If condition is true, program will jump back to do Statement and executes statement in do-while block again, otherwise program leaves do-while block do statement; while (condition); do { statement-1; statement-2; … statement-n; } while (condition);

do-while Statement: Flow chart

Example 3 2 -1 1 Output : 2 1 #include <stdio.h> num int main(int argc, char **argv) { int num = 2; do { printf(“%d “, num); num--; } while(num >= 0); } 2 -1 1 num Output : 2 1

QUIZ 2 Find the output of this program #include <stdio.h> int main(int argc, char **argv) { float sum = 0.0; int k = 1; do { sum = sum + k; k++; } while(k <= 10); printf(“Average = %.4f\n”, sum/10); }

The Difference between while and do-while Statement

break Statement This statement causes an immediate exit from the current loop structure #include <stdio.h> int main(int argc, char **argv) { int k = 1; while (k <= 10) { printf(“k = %d\n”, k); k++; break; } printf(“Bye Bye\n”); Output of this program ?

continue Statement Sometimes, we want to skip the rest of statements and back to the condition checking of iteration process again continue Statement is used to jump back immediately to condition checking statement #include <stdio.h> int main(int argc, char **argv) { int k = 1; while (k <= 10) { if (k < 9) { k++; continue; } printf(“k = %d\n”, k); } printf(“Bye Bye\n”); Output of this program ?

QUIZ 3 Find the output of this program. #include <stdio.h> int main(int argc, char **argv) { int i = 0; do { printf("< "); if (i == 2) break; printf(“%d >“, i); i++; } while (1); } Find the output of this program.

QUIZ 4 Find the output of this program #include <stdio.h> int main(int argc, char **argv) { int i = 0; do { i++; printf("<"); if (i == 2) continue; printf(“%d >“, i); } while (i <= 3); }