New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Loops 1 lecture: COSC 236

by: Kelsey Voelker

Loops 1 lecture: COSC 236 COSC 236

Marketplace > Towson University > Computer science > COSC 236 > Loops 1 lecture COSC 236
Kelsey Voelker

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

lecture notes about loops
Intro to Computer Science I
Ziying Tang
Class Notes
25 ?




Popular in Intro to Computer Science I

Popular in Computer science

This 4 page Class Notes was uploaded by Kelsey Voelker on Tuesday October 11, 2016. The Class Notes belongs to COSC 236 at Towson University taught by Ziying Tang in Summer 2016. Since its upload, it has received 3 views. For similar materials see Intro to Computer Science I in Computer science at Towson University.

Similar to COSC 236 at Towson

Popular in Computer science


Reviews for Loops 1 lecture: COSC 236


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/11/16
Intro to Computer Science 10/10/16 Lecture notes Loops 1  Main topics o Goal of a loop  A structure that allows you to repeat instructions more than once  Count ++; count --; o The increment and Decrement operators o The while loop  Use this for input validation o The do-while loop o For loops o Nested for loops  The Increment and Decrement Operators o Numerous times where a variable must be incremented or decremented  Number++ = number + 1;  Number-- = number – 1; o Java provides shortened ways to increment and decrement a variable’s value o Using the ++ or – unary operators, this task can be completed quickly  Number ++; or ++number;  Number--: or –number;  Code ex.: Public class IncrementDecrement { public static void main(String[] args) { int number = 4; //number starts out with 4 //display the value in number system.out.println(“number is” + number); system.out.println(“I will decrement number.”); //decrement number number--; //display the value in number once more system.out.println(“Now, number is” + number); } }  Differences between prefix and postfix o When an increment or decrement are the only operations in a statement, there is no difference between prefix and postfix notation o When used in an expression:  Prefix notation indicates that the variable will be incremented or decremented prior to the rest of the equation being evaluated  Postfix notation indicates that the variable will be incremented or decremented after the rest of the equation has been evaluated.  WHILE loops o Form:  Initialization, condition, and update (goes in a circle/infinite loop)  While(condition)  { statements;}  while the condition is true, the statements will execute repeatedly o called pre-test loop  it will test the value of the condition prior to executing the loop o must take caution when setting the condition to false somewhere in the loop so the loop will end o loops that do not end are called infinite loops o a while loop executes 0 or more times—if condition is false it will not execute  if condition is true keeps repeating statement  if condition is false goes straight down until end  INFINITE loops o In order for a while loop to end, the condition must become false. o Ex. On PP  Block statements in Loops o Curly braces are required to enclose block statement while loops (like block if statements)  While (condition)  {  statement;  statement;  }  The WHILE loop for input validation o Input validation is the process of ensuring that user input is valid o *keeps asking the user for information until the input is correct with the information being asked o checks information FIRST and then executes statements  The DO-WHILE loop o A post-test loop  It will execute the loop prior to testing the condition o Executes statements THEN check the condition o Statementsboolean expression (true or false) true(go back to statements)/ false (other code) o Sometimes called a do loop. Takes form: Do { statements(); } while (condition);  Notes from examples o Input =  the input represents any user input o Input.charAt (); put a number in the parenthesis to tell the computer to check the FIRST character of the users input  Ex. Input: Yes, input.charAt(0); checks the first letter ‘Y’ o *make sure you know the scope of your variable (where it will be able to print out) LOOPS 2  The FOR loop o A pre-test loop o Allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code. o Combines initialization, condition, and statement all in ONE sentence o Form:  For(initialization; test; update)  { statements;}  ex.--> for (int num = 1; num <= 10; num++) { statements;} o executes code update checks condition o *good to use when you know how many times you want the loop to run  The Update expression o Usually used to increment or decrement the counter variable(s) declared in the initialization section for the loop o This section executes last in the loop o May update multiple variables o Each variable updated is executed as if it were on a line by itself  Modifying the control variable o Avoid updating the control variable of a for loop within the body of the loop o The update section should be used to update the control variable o Updating the control variable in the for loop body leads to hard to maintain code and difficult debugging  Multiple initializations and updates o The 3 elements of this are optional o The only mandatory parts are the ;;  Sentinel Values o Sometimes the end point of input data is not known o This can be used to notify the program to stop acquiring input o If it is a user input, the user could be prompted to input data that is not normally in the input data range (where normal input would be positive) o *A value that indicates the ‘flag’ to java to STOP the loop  Nested loops o If a loop is nested, the inner loop will execute all of its iterations for each time the outer loop executes once o Inner loop executes outer loops  Break statements o Can be used to abnormally terminate a loop o Exists entire loop o Considered to be bad form  Continue statements o Causes the currently executing iteration of a loop to terminate and the next iteration will begin o Bad form  differences: o while loop good for instances when you don’t know how many times you’re going to loop o For loop (indeterminate) might not know how many times the loop will be executed  Deciding which loops to use o The while loop:  Pretest loop  Use it where you do not want the statements to execute if the condition is false in the beginning o The do-while loop:  Post-test loop  Use it where you want the statements to execute at least one time o the for loop:  pretest loop  use it where there is some type of counting variable that can be evaluated


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Allison Fischer University of Alabama

"I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over $600 per month. I LOVE StudySoup!"

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.