Module5 Looping Techniques: Part 2

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
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.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
BIM313 – Advanced Programming Techniques Flow Control 1.
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.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
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
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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.
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.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
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
ECE Application Programming
Repetition (While-Loop) version]
Thanachat Thanomkulabut
Chapter 2.2 Control Structures (Iteration)
CS149D Elements of Computer Science
The University of Texas Rio Grande Valley
The University of Texas – Pan American
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)
Chapter 6 Control Statements: Part 2
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
CHAPTER 21 LOOPS 1.
Var Name =Console . ReadLine();
Iteration (Loop) partII
CS149D Elements of Computer Science
Control Statements Loops.
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); }

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 #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++