Iteration Statement for

Slides:



Advertisements
Similar presentations
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Advertisements

1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 TDBA66, VT-03, Lecture - Ch. 5 Repetition Loops are common in programming (computers can do boring things over and over again) Type of loopWhen usedC-structures.
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.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
INTRODUCTION TO C PROGRAMMING LANGUAGE Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
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)
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
1. Agenda for loop Short-handed notation How to use break and continue 2.
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,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
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.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
CSI 3125, Preliminaries, page 1 Control Statements.
COMP Loop Statements Yi Hong May 21, 2015.
Review : C Programming Language
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
PGT C Programming1 Week 4 – Repetition Structures / Loops.
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.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
ECE Application Programming
CSE 220 – C Programming Loops.
ECE Application Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
CiS 260: App Dev I Chapter 4: Control Structures II.
The nested repetition control structures & Continue Statements
Iteration statement while do-while
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Loop Control Structure.
- Additional C Statements
Loops in C.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Week 6 CPS125.
UMBC CMSC 104 – Section 01, Fall 2016
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Computer Programming Techniques Semester 1, 1998
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Statements in C Programming
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Lec 6 Loop Statements Introduction to Computer Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Iteration Statement for 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

Review: 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);

Review: while Statement 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; }

for Statement for Statement also control over the iteration flow of program just like while and do-while Statements. for Statement is more special than others. It is suitable in the case that we know exactly how many time, we want to repeat the statements in for block for(expr1; expr2; expr3) statement; for(expr1; expr2; expr3) { statement-1; statement-2; … statement-n; }

for Statement: Flow chart

while and for Statement Comparison

while Statement to for Statement Transformation while( i < 10) { printf(“%d\n”, i); i++; } for( ; ; ) { printf(“%d\n”, i); } i = 0 i < 10 i++

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

QUIZ 1 Find the output of this program #include <stdio.h> int main(int argc, char **argv) { int i; for( i = 0; i <= 50; i+=5) { printf(“%d “, i); }

QUIZ 2 Find the output of this program #include <stdio.h> int main(int argc, char **argv) { int i, sum; sum = 0; for( i = 1; i <= 5; i++) { sum += i; } printf(“summation of 1-5 = %d\n”, sum);

Nested loop i j #include <stdio.h> int main(int argc, char **argv) { int i, j; for( i = 1; i <= 2; i++ ) { for( j = 1; j <= 2; j++) { printf(“C is very easy\n”); } i 2 3 ? 1 j 3 ? 1 2 Output : C is very easy C is very easy C is very easy C is very easy

QUIZ 3 Find the output of this program #include <stdio.h> int main(int argc, char **argv) { int i, j; for( i = 1; i < 5; i++) { for( j = 0; j < i; j++) printf(“*”); printf(“\n”); }

break Statement

Example 1 i #include <stdio.h> int main(int argc, char **argv) { int i; for (i = 0; i < 5; i++) { printf("<"); if (i == 2) break; printf(“%d >“, i); } i ? 2 1 ผลการรัน : < 0> < 1> <

QUIZ 4 #include <stdio.h> int main(int argc, char **argv) { int i, j; for (i = 0; i < 5; i++) { printf(“i = %d\n”, i); for(j = 0; j < 5; j++) { if (j >= 2) break; printf(“j = %d\n”, j); } Find the output of this program

continue Statement

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