×

### Let's log you in.

or

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

×

or

## COSC 236 decision structures 2 lecture

by: Kelsey Voelker

4

0

3

# COSC 236 decision structures 2 lecture COSC 236

Marketplace > Towson University > Computer science > COSC 236 > COSC 236 decision structures 2 lecture
Kelsey Voelker
Towson

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

×
Unlock Preview

lecture on decision structures (p2)
COURSE
Intro to Computer Science 1
PROF.
Dr. Alkharouf
TYPE
Class Notes
PAGES
3
WORDS
KARMA
25 ?

## Popular in Computer science

This 3 page Class Notes was uploaded by Kelsey Voelker on Monday October 3, 2016. The Class Notes belongs to COSC 236 at Towson University taught by Dr. Alkharouf in Fall 2016. Since its upload, it has received 4 views. For similar materials see Intro to Computer Science 1 in Computer science at Towson University.

×

## Reviews for COSC 236 decision structures 2 lecture

×

×

### What is Karma?

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

Date Created: 10/03/16
Comp sci 236 9/26/16 lecture notes Decision Structures 2  Variable Scope o Where the variable exists (where it can be seen)  Locally, in main method, in between braces, etc. o In java, a local variable does not have to be declared at beginning of method o The Scope of a local variable = at the point it is declared and terminates at the end of the method o when program enters section of code where a variable has scope, that variable has come into scope the variable is visible to the program  The Switch statement o The if-else statement allows you to make true/false branches  (switch statement makes this easier/looks nicer) o The switch statement allows you to use an ordinal value to determine how a program will branch o The switch statement can evaluate and integer type or character type variable  Makes decisions based on the value o Takes the form:  Switch (SwitchExpression) { case CaseExpression: // place one or more statements here break; case CaseExpression: // case statements may be repeated //as many times as necessary default: // place one or more statements here }  Can put number or character in for switchexpression and then connect it to which case you want. they must match  Ex.--> switch (4) case 4; (will perform case 4)  The default is the “else” if they do not give you your casexpressions  Switch statement rules o Takes an ordinal value (byte, short, int, long, or char) o as SwitchExpression  switch (SwitchExpression)  {…} o evalutates the expression o if an associated case statement that matches that value, program execution will be transferred to that case statement  Case statement o Have a corresponding CaseExpression  Case CaseExpression: o If switchExpression matches CaseExpression, the Java statements between the colon and the break statement will be executed o The break statement ends the case statement o If a case does not contain a break, then program execution continues into the next case o The default section is optional and will be executed if no CaseExpression matches the SwitchExpression  The printf Method (for formatted printing) o Takes a format string, and an argument list o You can use the System.out.printf method to perform formatted console output o The general format of the method is:  System.out.printf(FormatString, ArgList);  FormatString a string that contains text and/or special formatting specifiers  ArgList is optional. It’s a list of additional arguments that will be formatted according to the format specifiers listed in the format string. o Ex.  Int hours = 40;  System.out.printf(“I worked %d hours.\n”, hours);  Output I worked 40 hours  %d format specifier, indicates that an integer will be printed  hours the contents of the hours variable will be printed in the location of the %d format specifier o Ex. 2  Int dogs = 2, cats = 4;  System.out.printf(“We have %d dogs and %d cats.\n”, dogs, cats);  Output We have 2 dogs and 4 cats o Ex. 3  Double grossPay = 874.12;  System.out.printf(“Your pay is %f.\n”, grossPay);  Output Your pay is 874.120000  %f format specifier indicates that a floating-point value will be printed  grossPay the contents of the grossPay variable will be printed in the location of the %f format specifier o Ex. 4  Double grossPay = 874.12;  System.out.printf(“Your pay is %.2f.\n”, grossPay);  Output your pay is 874.12  %.2f format specifier indicates that a floating-point value will be printed, rounded to two decimal places o %s.\n indicates that a string will be printed (letters) o %6d\n the integer will appear in a field that is 6 spaces wide (specifying a width)  9 o %6.2f indicates the number will appear in a field that is 6 spaces wide, AND will be rounded to 2 decimal places 9.77

×

×

### BOOM! Enjoy Your Free Notes!

×

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!"

Jennifer McGill UCSF Med School

#### "Selling my MCAT study guides and notes has been a great source of side revenue while I'm in school. Some months I'm making over \$500! Plus, it makes me happy knowing that I'm helping future med students with their MCAT."

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

#### STUDYSOUP CANCELLATION 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 support@studysoup.com

#### STUDYSOUP REFUND POLICY

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: support@studysoup.com

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 support@studysoup.com