ADELPHI CSC 160 - Loops N Decisions If/Else Counting Loops

Unformatted text preview:

Introduction to Computer ProgrammingYour old Average friendAverage Program - did you pass?The if statementRelational expressionsif statement flow diagramThe if/else statementif/else flow diagramif/else questionNested if/else statementsNested if/else flow diagramNested if/else/if diagramSequential if diagramWhy loops?Example : Average of three numbersExample : Average of three numbers (continued)LoopsCounting LoopsExample: Hello AgainSlide 20Counting Loops (continued)Example: Rewriting HelloAgainRefining HelloAgainSlide 24The New HelloAgainGeneralizing HelloAgainGeneralizing HelloAgain (continued)Slide 28Slide 29The Revised HelloAgainExample: Averaging n NumbersRefining AvgnSlide 33Slide 34Slide 35Slide 36Slide 37The AverageN ProgramSlide 39Example: Interest ProgramSlide 41Refining The Interest AlgorithmSlide 43Slide 44Slide 45Slide 46Slide 47The Interest ProgramSlide 49Output from the Compound Interest ProgramSystem.out.printf()System.out.printf(): Some Simple ExamplesSpecial Characters%d and %fChanging the widthChanging the width (continued)Changing The PrecisionThe revised Compound programSlide 59The output from the Revised Compound ProgramA program to calculate Grade Point AverageA Program To Calculate Test AverageDesigning the Grade ProgramRefining the Grade ProgramSlide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Our programSlide 74Slide 75One Last RefinementThe Final Grade ProgramSlide 78Slide 79Slide 80Introduction to Computer ProgrammingLoops N DecisionsIf/ElseCounting LoopsYour old Average friendimport java.util.Scanner;public class AverageN { //AverageN - Find the average of N values public static void main(String[] args) { Scanner keyb = new Scanner(System.in); double sum, average, value1, value2, value3; //get values System.out.println (“What is value 1?”); value1 = keyb.nextDouble(); System.out.println (“What is value 2?”); value2 = keyb.nextDouble(); System.out.println (“What is value 3?”); value3 = keyb.nextDouble();sum = value1 + value2 + value3;average = sum/3; System.out.println(“The average is “ + average);}}Average Program - did you pass?•You have the average program averaging 3 grades, but did you pass? If the grade is over 65, you passed. •How can you code this?4The if statement•if statement: Do this only if true–General syntax:if (<condition>) { <statement> ; ... <statement> ;}•Example:double average = sum/3;if (average >= 65) { System.out.println(“ You passed.");}5Relational expressions Operator Meaning Example Value==equals1 + 1 == 2 true!=does not equal3.2 != 2.5 true<less than10 < 5 false>greater than10 > 5 true<=less than or equal to126 <= 100 false>=greater than or equal to5.0 >= 5.0 true6if statement flow diagram7The if/else statement•if/else statement: A Java statement that executes one block of statements if a certain condition is true, and a second block of statements if it is false.–General syntax:if (<condition>) { <statement(s)> ;} else { <statement(s)> ;}•Example: double average = sum/3;if (average >= 65) { System.out.println(“ You passed.");} else{ System.out.println(“You failed.”);}8if/else flow diagram9if/else question•Write code to read a number from the user and print whether it is even or odd using an if/else statement.–Example executions:Type a number: 42Your number is evenType a number: 17Your number is odd10Nested if/else statements•nested if/else statement: A chain of if/else that chooses between outcomes using many conditions.–General syntax:if (<condition>) { <statement(s)> ;} else if (<condition>) { <statement(s)> ;} else { <statement(s)> ;} •Example:if (number > 0) { System.out.println("Positive");} else if (number < 0) { System.out.println("Negative");} else { System.out.println("Zero");}11Nested if/else flow diagramif (<condition>) { <statement(s)> ;} else if (<condition>) { <statement(s)> ;} else { <statement(s)> ;}12Nested if/else/if diagramif (<condition>) { <statement(s)> ;} else if (<condition>) { <statement(s)> ;} else if (<condition>) { <statement(s)> ;}13Sequential if diagramif (<condition>) { <statement(s)> ;}if (<condition>) { <statement(s)> ;}if (<condition>) { <statement(s)> ;}Why loops?•Computers offer several advantages over calculators. •If it is necessary, they can perform the same steps over and over again, simply by rerunning the program.•But is this the only way to get a computer to perform the same action repeatedly? And is this the only reason for getting a computer to repeat itself?Example : Average of three numbers•Let’s take another look at our program that finds the average of three numbers:import java.util.Scanner;public class AverageThree { public static void main(String[] args) { Scanner keyb = new Scanner(System.in); int value1, value2, value3; int sum, average; System.out.println ("What is the first value?"); value1 = keyb.nextInt(); System.out.println ("What is the second value?"); value2 = keyb.nextInt();Example : Average of three numbers (continued) System.out.println ("What is the third value?"); value3 = keyb.nextInt(); sum = value1 + value2 + value3; average = sum / 3; System.out.println("Average = " + average); if (average >= 65) { System.out.println(“ You passed.");} else{ System.out.println(“You failed.”);} }}•What would we do if we wanted the program to average 5 values instead of 3? or 10? or 100?•This is clearly not the best way to write this!Loops•We need the ability to perform the same set of instructions repeatedly so we don’t have to write them over and over again.•This is why Java includes several ways of using repetition in a program.•Each case where we repeat a set of statement is called a loop.Counting Loops•The first type of loop is a counting loop.•Counting loops are repeated a specific number of times.•If you read the loop, you can easily figure out how many times its statements will be performed.Example: Hello Again•Example - Write a program that greets the user with "Hi there!" five times.•We could write the program like this:import java.util.Scanner;public class HelloAgain { // Hello again - this program writes "Hello, again" five times public static void main(String[] args) { System.out.println("Hello, again"); System.out.println("Hello, again"); System.out.println("Hello, again"); System.out.println("Hello, again"); System.out.println("Hello,


View Full Document

ADELPHI CSC 160 - Loops N Decisions If/Else Counting Loops

Documents in this Course
Classes

Classes

73 pages

Methods

Methods

64 pages

Load more
Download Loops N Decisions If/Else Counting Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Loops N Decisions If/Else Counting Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Loops N Decisions If/Else Counting Loops 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?