TK1114 Computer Programming

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computer Programming Decisions If/Else Booleans.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 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.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Computer Programming Lab(4).
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Computer Programming Lab(5).
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Repetition Statements while and do while loops
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Slides by Evan Gallagher
Slides by Evan Gallagher
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 4 Repetition Statements (loops)
John Hurley Cal State LA
Chapter 2 Elementary Programming
Chapter 6 More Conditionals and Loops
Computer Programming Methodology Input and While Loop
Introduction to Methods in java
Repetition-Counter control Loop
Repetition.
Something about Java Introduction to Problem Solving and Programming 1.
While Statement.
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Outline Altering flow of control Boolean expressions
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Java Language Basics.
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Week 4 Lecture-2 Chapter 6 (Methods).
if-else if (condition) { statements1 } else { statements2
CIS 110: Introduction to Computer Programming
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Computer Science Club 1st November 2019.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

TK1114 Computer Programming 7 - while loop TK1114 Computer Programming

Repetition Statements (Loop) Repetition statements allow us to execute a statement multiple times Java has three kinds of repetition statements: the for loop the while loop the do-while loop The programmer should choose the right kind of loop for the situation

for loop An example : for (initialization; condition; increment){ statement true condition evaluated false increment initialization for (initialization; condition; increment){ statements; } An example : for (int count=1; count <= 5; count++){ System.out.println (count); }

while Loop while ( condition ){ statement; } An example: true false condition evaluated while ( condition ){ statement; } An example: int count = 1; while (count <= 5) { System.out.println (count); count++; }

do Loop An example : do { statement; } while ( condition ) true condition evaluated statement false do { statement; } while ( condition ) An example : int count = 0; do { count++; System.out.println (count); } while (count < 5);

Keep Positive Problem Description Write a program that reads an unspecified number of integers, determines how many positive value have been read. Your program ends with the input 0. Input The data set consists of a list of positive integer, num (0 < num ≤ 100) that ends with 0. Output There is only one output for this problem, which is the number of integers positive. Sample Input Output Sample Input Sample Output 2 -3 4 -5 6 7 -8 9 10 0 6

Keep Positive import java.util.Scanner; public class KeepPositive{ 2 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num; int count = 0; do { num = sc.nextInt(); if (num > 0) count++; } while (num != 0); System.out.println(count); } 2 -3 4 -5 6 7 -8 9 10 6

Min Max Problem Description Write a program that reads 10 integers. Find the smallest and largest numbers. Input 10 integers num (0 < num ≤ 100). Output The smallest and largest integer. Sample Input Output Sample Input Sample Output 12 3 2 5 4 27 1 9 10 7 1 27

Min Max import java.util.Scanner; public class KeepPositive{ 12 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num; int max = 0, min = 100; for (int count = 0; count<10; count++) { num = sc.nextInt(); if (num > max) max = num; if (num < min) min = num; }; System.out.println(min + " " + max); } 12 3 2 5 4 27 1 9 10 7 1 27

Sum of digits of numbers Problem Description Write a program to ask user for a positive integer and calculating sum of its digits then. Input integer positive number (n1>0 ). Output Sum of the digits. Sample Input Output Sample Input Sample Output  123 6

Sum of digits of numbers Analysis Input: 123 Output: 1+2+3 = 6 We can use modulo operator to solve this problem. 123 % 10 = 3 123 / 10 = 12 12 % 10 = 2 12 / 10 = 1

Sum of digits of numbers public class KeepPositive{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int num, lastnum=0, sum=0; num = in.nextInt(); if (num>0){ while (num != 0){ lastnum = num % 10; sum += lastnum; num /= 10; } System.out.println(sum); 123 6

Area of a circle Problem Description Write a program to calculate the area of a circle. Input The first line of input is the number of input n, where 1 ≤ n ≤ 20. For each of the following n lines there are radius for the circle. Output For each input, print the area. Sample Input Output   Sample Input Sample Output 4 3 34 12 28.26 3629.84 452.16 50.24

Area of a circle import java.util.Scanner; public class Trial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double radius; double area; int n; n = sc.nextInt(); for (int i=0; i<n; i++){ radius = sc.nextDouble(); area = radius * radius * 3.14; System.out.println (area); }