Module5 Looping Techniques: Part 2

Slides:



Advertisements
Similar presentations
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Advertisements

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 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.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
C#: Statements and Methods Based on slides by Joe Hummel.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
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
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Repetition Statements while and do while loops
Iterations Very Useful: Ability to repeat a block of code Example:
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
CIS 3301 C# Lesson 3 Control Statements - Selection.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
COMP Loop Statements Yi Hong May 21, 2015.
1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
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#
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
UCT Department of Computer Science Computer Science 1015F Iteration
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Repeating Code Multiple Times
REPETITION CONTROL STRUCTURE
ECE Application Programming
Repetition (While-Loop) version]
Thanachat Thanomkulabut
Chapter 2.2 Control Structures (Iteration)
Objectives Explain selection constructs Describe loop constructs
The University of Texas Rio Grande Valley
The University of Texas – Pan American
.Net Programming with C#
The University of Texas – Pan American
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.
Chapter 2.2 Control Structures (Iteration)
Module5 Looping Techniques: Part 2
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Module8 Multi-dimensional Array
Module 8 & 9 Method :Part I & Array :Part II
Var Name =Console . ReadLine();
Iteration (Loop) partII
Control Statements Loops.
Thanachat Thanomkulabut
Iteration (Loop) part II
Presentation transcript:

Module5 Looping Techniques: Part 2 Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

Switch-case example <expression> must be int, char, string review Switch-case example <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 Switch-case example (continue) <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 review Flowchart of Switch-case 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"); } label (day_num) label=1 statement1 (day_num=1) label=2 statement2 (day_num=2) label=3 statement3 (day_num=3) : default statement n

Looping or Iteration in C# while Iteration for foreach do…while

Outline do…while statement for statement

while statement while (condition) statement;

do…while statement For Single Statement do { statement; } while (condition); For Multiple Statements do { statement-1; statement-2; . statement-N; } while (condition);

Sentinel Loops: example static void Main() { int N, SUM; SUM = 0; N = 0; Console.Write(”Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); while (N != -1) { SUM = SUM+N; } Console.WriteLine(“The sum is {0}.”, SUM); 1st Time (redundant part)

Sentinel Loops: do…while Version static void Main() { int N, SUM; SUM = 0; N = 0; do { Console.Write(”Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); if (N!= -1) SUM = SUM+N; } while (N != -1); Console.WriteLine(”The sum is {0}.”, SUM); }

Factorial Number: C# Program review Factorial Number: C# Program QUIZ1 N! = N * (N-1) * (N-2) * (N-3) *...* 1 Example 5! = 5*4*3*2*1 static void Main() { int N, FACT=1; FACT = 1; Console.Write(”Please input number: ”); N = int.Parse(Console.ReadLine()); while ( ?????? ) { FACT = ?????? ; ???? ; }; Console.WriteLine(”The factorial is {0}.”,FACT); } Find N! N > 0 FACT * N N--

Outline do…while statement for statement

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

for statement k = ? for ( [initializers]; [expression]; [iterators] ) START END expression true false Statement iterators initializers for ( [initializers]; [expression]; [iterators] ) statement; static void Main() { int k; for (k = 0; k <= 3; k++) Console.Write("A "); Console.Write(k); } Example of for statement k = ? A A A A 4

Display 1 to 5 on screen while version for version static void Main() { int i; i = 1; while (i <= 5) { Console.WriteLine(“{0}”, i); i++; } while version static void Main() { int i; for (i=1; i<=5; i++) { Console.WriteLine(“{0}”, i); } for version

Problem #1 – Display 10 stars START END expression true false Statement iterators initializers ********** static void Main() { int i=0; for ( ; ; ) Console.Write(”*”); } i=0 i = 0 i < 10 i++ i<10 write(“*”) i++ How to display N stars?

Problem #1: Find QUIZ2 i2 static void Main() { int N=0, SUM=0, i=0; 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); } i = 1 i <= N i++ i*i

Problem #3 – Display 4x3 stars **** static void Main() { int N=0, M=0; for ( ; ; ) { for ( ; ; ) { Console.Write(”*”); } Console.WriteLine(); M = 0 M < 3 M++ N = 0 N < 4 N++ How to display NxN stars?

Problem #4 – Triangle shape#1 *** ** * static void Main() { int N=0, M=0; for ( ; ; ) { for ( ; ; ) { Console.Write(”*”); } Console.WriteLine(); M = 3 M > 0 M-- N = 0 N < M N++

Problem #5: Find 2n QUIZ3 static void Main() { int N=0, SUM=1, i=1; Console.Write(”Please input number: ”); N = int.Parse(Console.ReadLine()); while (i<=N){ SUM = SUM*2; i++; } Console.WriteLine(”SUM = {0}”, SUM); for ( ; ; ) SUM = SUM * 2; i = 1 i <= N i++