Slide 4 - COP3502 Programming Fundamentals for CIS Majors 1...

Info iconThis preview shows pages 1–23. Sign up to view the full content.

View Full Document Right Arrow Icon
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sakai In case of any problems, contact TA before due date! Agenda
Background image of page 2
Chapter 3 Conditional statements if if. .else else if switch Logical operators &&, ||, ^, ! Previously . ..
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 4 Loops while do-while for Input from file Objectives
Background image of page 4
Loops
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Suppose we want to show “Java is fun!” 100 times. How do we do that? Repeat the following statement 100 times!? Motivation System.out.println("Welcome to Java!");
Background image of page 6
Naïve solution Naïve Solution System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); 100 times!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Better solution Using loop Better Solution int count = 0; while (count < 100) { System.out.println("Welcome to Java"); count++; }
Background image of page 8
1. To automate the repetition of calculations E.g. compute the profit for “a number of different months” of a company's sale 2. To iterate through data and test for certain condition E.g. Checking input data, until user wants to “quit” 3. To keep attempting for some operation E.g obtaining data from a remote computer over a network) until we succeed Why Loops?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
while Loop
Background image of page 10
1. If the condition is true, the statement is executed; then the condition is evaluated again … 2. The statement is executed over and over until the condition becomes false. 3. When the loop finishes, control passes to the next instruction in the program, following the closing curly brace of the loop. while Loop while (condition) { statement; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
while while (loop-condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; } Loop Continuation Statement(s) (loop body) false (count < 100)? System.out.println("Welcome to Java!"); count++; false ( A ) ( B ) count = 0;
Background image of page 12
Trace Program while (count < 2) { System.out.println("Welcome to Java!"); count++; } Initialize count
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Trace Program int count = 0; System.out.println("Welcome to Java!"); count++; } (count < 2) is true
Background image of page 14
Trace Program int count = 0; while (count < 2) { count++; } Print “Welcome to Java!”
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Trace Program int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); } count is now 1.
Background image of page 16
Trace Program int count = 0; System.out.println("Welcome to Java!"); count++; } (count < 2) is still true.
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Trace Program int count = 0; while (count < 2) { count++; } Print “Welcome to Java!”
Background image of page 18
Trace Program int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); } count is now 2.
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Trace Program int count = 0; System.out.println("Welcome to Java!"); count++; } (count < 2) is now false.
Background image of page 20
Trace Program int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Loop exit
Background image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
No ; at the end of while Caution!
Background image of page 22
Image of page 23
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 73

Slide 4 - COP3502 Programming Fundamentals for CIS Majors 1...

This preview shows document pages 1 - 23. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online