Iteration (Loop) partII

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CS201 - Repetition loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Do-while loop Syntax do statement while (loop repetition condition)
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Repetition Statements while and do while loops
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Statements
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Lecture 4b Repeating With Loops
Repeating Code Multiple Times
Control Structures.
Thanachat Thanomkulabut
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
INC 161 , CPE 100 Computer Programming
Looping.
Objectives Explain selection constructs Describe loop constructs
The University of Texas – Pan American
The University of Texas – Pan American
Chapter 6 Decision Making and Looping
Loops in C.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Control Statements Loops.
Thanachat Thanomkulabut
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Control Statements Loops.
Thanachat Thanomkulabut
Iteration (Loop) part II
Programming Fundamental
break & continue Statements
Presentation transcript:

Iteration (Loop) partII Thanachat Thanomkulabut

Switch-case example <expression> must be int, char, string review 2 <expression> must be int, char, string char version int version char op; Console.Write("Select + - / * :"); op=char.Parse(Console.ReadLine()); switch(op) { case '+': Console.Write("{0}+{1}={2}", x,y,x+y); break; case '-': Console.Write("{0}-{1}={2}", x,y,x-y); : default: Console.Write("Try again"); } int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); : default : Console.Write(“Try again"); } <expression>

Switch-case example (continue) review <expression> must be int, char, string string version string op; Console.Write("Select + - / * :"); op=Console.ReadLine(); switch(op) { case “+”: Console.Write("{0}+{1}={2}", x,y,x+y); break; case “-”: Console.Write("{0}-{1}={2}", x,y,x-y); : default: Console.Write("Try again"); } <expression>

Flowchart of Switch-case (1/2) review int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); : default : Console.Write(“Try again"); } statement1 label (day_num) label=1 label=2 default statement2 statement n (day_num=3) label=3 statement3 : (day_num=2) (day_num=1)

Flowchart of Switch-case (2/2) review Flowchart of Switch-case (2/2) 5 int month; Console.Write("Input Month"); month = int.Parse(Console.ReadLine()); switch(month) {case 1: case 3: case 5: : Console.Write("This month = 31days"); break; case 4: case 6: Console.Write(" This month = 30days"); default : Console.Write ("Input again"); } label (month) label=1,3,5,… default : statement1 (month=1,3,5,…) label=4,6,… Statement2 (month=4,6,…) statement n

Outline For statements Nested loops Break statements Continue statements

Looping or Iteration in C# while Iteration (repeat) foreach do…while for

Kinds of Iteration statements Conditional iteration while do...while Definite iteration For

for statement For Single Statement For Multiple Statements for ( [initializers]; [condition]; [runner] ) statement; For Multiple Statements for ( [initializers]; [condition]; [runner] ) { statement-1; statement-2; . statement-N; }

for statement Example of for statement 1 2 3 4 START END condition true false Statement runner initializers for ( [initializers]; [condition]; [runner] ) statement; Example of for statement static void Main() { int i; for(i = 1; i <= 4; i++) Console.WriteLine(i); } FALSE TRUE Output 1 2 i 3 2 5 4 1 3 4

for statement example static void Main() Output { int k; A A A A A 5 for (k = 0; k <= 4; k++) Console.Write("A "); Console.Write(k); } Output A A A A A 5

for statement example Write the program to show even number 2,4,6,...,n static void Main() { int k,n; Console.Write(“Input n :”); n = int.Parse(Console.ReadLine()); for ( ; ; ) } k = 1 k <= n k++ if(k%2==0) Console.WriteLine(k);

for statement example Write the program to show even number 2,4,6,...,n static void Main() { int k,n; Console.Write(“Input n :”); n = int.Parse(Console.ReadLine()); for ( ; ; ) } k = 2 k <= n k = k+2 Console.WriteLine(k);

Test I : Find n factorial int n,x,fact; Console.Write(“Enter value of n : “); n = int.Parse(Console.ReadLine()); fact = n; x = n-1; while (x >= 1) { fact = fact*x; x = x-1; } Console.WriteLine(“{0}! = {1}”,n,fact);

Test II : Problem #1: Find 5 i2 = 1*1 + 2*2 + 3*3 + 4*4 + 5*5 static void Main() { int N=0, SUM=0, i=0; Console.Write(”Please input number: ”); N = int.Parse(Console.ReadLine()); for ( ??? ; ??? ; ??? ) SUM = SUM + ??? ; Console.WriteLine(”SUM = {0}”, SUM); }

Outline For statements Nested loops Break statements Continue statements

Nested Loops : Example Nested for loops Do not use the same runner variable in the outer loop and the inner loop for (int outer = 1; outer <= 4; outer++) { for (int inner = 1; inner <=3; inner++) { Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); }

Nested Loops : Example Output Outer = 1 & Inner = 1 for (int outer = 1; outer <= 4; outer++) { for (int inner = 1; inner <=3; inner++) { Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); } Outer = 1 & Inner = 2 Outer = 1 & Inner = 3 Outer = 2 & Inner = 1 Outer = 2 & Inner = 2 Outer = 2 & Inner = 3 Outer = 3 & Inner = 1 Outer = 3 & Inner = 2 Outer = 3 & Inner = 3 Outer = 4 & Inner = 1 Outer = 4 & Inner = 2 Outer = 4 & Inner = 3

Nested Loops : Example Nested while loops int outer = 1; while (outer <= 4) { int inner = 1 ; while (inner <= 3) { Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); inner := inner +1; } outer := outer +1;

Nested Loops : Example Nested do...while loops int outer = 1; do { int inner = 1; Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); inner := inner +1 } while (inner < 6); outer := outer + 1; inner := 1 }while (outer < 5);

Test III : Nested Loops : Example Write the program to Show output 1 12 123 1234 12345 int i,j; for( ; ; ) { for( , , ) { }

Break statement The break statement terminates the closest enclosing loop or switch statement in which it appears

Break statement example int i,j; for(i=1;i<=10;i++) { Console.WriteLine(i); if(i==5) break; } Console.Write(“final i = {0}”,i); i 4 2 1 5 3 Output 1 2 3 4 5 final i = 5

Continue Statement i sum 1 3 2 +1 +3 +5 +7 +9 Output final sum = 25 3 2 +1 +3 +5 +7 +9 final sum = 25 int i=0,sum=0; do{ i++; if(i%2==0) continue; sum = sum+i; }while(i<10); Console.WriteLine("final sum={0}",sum);

Continue statement example int i=0,sum=0; for(i=1;i<=10;i++ ) { if(i%2==0) continue; sum = sum+i; } Console.WriteLine("final sum={0}",sum);

Any question?