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

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

COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

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

View Full Document
Sakai In case of any problems, contact TA before due date! Agenda
Chapter 3 Conditional statements if if. .else else if switch Logical operators &&, ||, ^, ! Previously . ..

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

View Full Document
Chapter 4 Loops while do-while for Input from file Objectives
Loops

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

View Full Document
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!");
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!

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

View Full Document
Better solution Using loop Better Solution int count = 0; while (count < 100) { System.out.println("Welcome to Java"); count++; }
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?

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

View Full Document
while Loop
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; }

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

View Full Document
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;
Trace Program while (count < 2) { System.out.println("Welcome to Java!"); count++; } Initialize count

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

View Full Document
Trace Program int count = 0; System.out.println("Welcome to Java!"); count++; } (count < 2) is true
Trace Program int count = 0; while (count < 2) { count++; } Print “Welcome to Java!”

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

View Full Document
Trace Program int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); } count is now 1.
Trace Program int count = 0; System.out.println("Welcome to Java!"); count++; } (count < 2) is still true.

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

View Full Document
Trace Program int count = 0; while (count < 2) { count++; } Print “Welcome to Java!”
Trace Program int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); } count is now 2.

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

View Full Document
Trace Program int count = 0; System.out.println("Welcome to Java!"); count++; } (count < 2) is now false.
Trace Program int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Loop exit

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

View Full Document
No ; at the end of while Caution!
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online