Popular in Course
Popular in Electrical Engineering
This 5 page Class Notes was uploaded by Dorris Borer on Monday September 28, 2015. The Class Notes belongs to ESE112 at University of Pennsylvania taught by Staff in Fall. Since its upload, it has received 5 views. For similar materials see /class/215450/ese112-university-of-pennsylvania in Electrical Engineering at University of Pennsylvania.
Reviews for INTROTOESE
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 09/28/15
Introduction to Programming WI39fz Java for Beginners Control Structures Sequential Control Flow I Consists ofjust a list of commands to be done in order Welcome to Dr Java gt int num 2 gt int sqNum gt sqNum num num gt SystemoutprintlnsqNum Orjust type1sqNum 2 EsE112 1I17 What are control Structures I Limitations of sequential control ow I Cannot choose whether or not to perform a commandinstruction I Cannot perform the same command more than once I Such programs are extremely limited I Control structures allow a program to base its behavior on certain conditions EsE112 2I17 Recap Boolean Boolean is one of the eight primitive types I Only 2 value true orfalse I Booleans are used to make yes or no decisions I All control structures use Booleans The following expression each give a Boolean result 25 gt 24 ampamp 12 13 resuts to false 25 gt 24 12 13 resuts to true I Thus based on certain conditions we can alter the outcome or flow of the program EsE112 3M7 Conditionals if statements if statement if condition statement 5 If the condition is true then the statements ie instructions Will be executed Othenvise itthey won t An if statement is a flow control statement It is also called a conditional or a branch We ll see several flavors An if all by itself An if with an else part An if with an else it part Assume X is an integer ifX 2 0 Systemoutprintlnx is even EsE112 m7 EsE112 5M7 Example ifelsequot statement ifelsequot statement Example Syntax amp if condition Explanation statement 5 Determine smaller of two integers if condition numbers amp put it variable min statement5 else t statements n mm 5155 int X statementss int y 10 A 39 39 t H ssume X ls an in agar Ifthe condition is true ifX 2 0 if x lt y then the statements Systemoutprintlnx is evenquot min x in the i c are execu Else Else 0th 39 39fth 39 Systemoutprintlnx is oddquot min 3quot anxxeTAgil era s statements in it are 55112 El 7 ESE112 ex ed if with elseif statement if condition I No limit on else ifquot statement 5 statements I The else condition is 59 if condition usually a default statements condition I For completeness else EsE112 8M7 Cascading ifelse char userChoice Ask user for input and store it in userChoice if userChoice q Systemoutprintln quitting else if userChoice a Systemoutprintln adding else if userChoice 5 Systemoutprintln saving else Sys tem out println unrecogni zed choice Note You can avoid the curly brace after condition if only one statement is to be performed EsE112 9M7 Nested ifstatements An if within an if Truth Table if condition1 if condition2 What Values must the statement 5 A conditions have in order for block A to run B C else statements B A B C condition1 T else statementss C cond39t39on2 EsE112 1on7 The infamous dangling elsequot if x gt y When is statementB if y lt Z executed statementA else In other words which stat ntB if is the else paired with An else is paired with the last if X gt Y elseless if regardless of spacing if Y lt 2quot unless dictate otherwise statementquot else statementB 555112 m A Loop I A simple but powerful mechanism for making lots of things happen I Performs a statement or block over amp over I Usually set upto repeat an action until some condition is atisfied I Computing Scenarios Examples I Run an application until user hits quit button I Read characters until end of file reached I Deal card hands until game over EsE112 12I17 Syntax of the whie statement While condition statement s l I condition is a truefalse boolean expression I If condition is initially false the statement is never executed I If condition is true statanent is executed and condition is reevaluated I The statement should eventually make the loop stop EsE112 13m A whie Loop to Print Numbers Print the numbers 1 thru 10 int x 1 while x lt 10 Systemoutprintlnx x x 1 l I What happens if you forget the statement x x 1 I We print value 1 forever I Known as in nite loop EsE112 14m More InfHire Loops Some infinte loops are intentional while true statement 5 Others are not int x 5 while x lt 10 statement 5 which don t change x EsE112 15m Compute Square of first 10 numbers In Squarejava int num 1 int sqNum 0 while num lt 10 sqNum num num Systemoutprintlnnum quot quot sqNum num num 1 ESE112 16I17 For Loop for init endtest reinit statement I Executes loop body as long as endtest evaluates to TRUE I Initialization and reinitialization code included in loop statement I Note Test is evaluated before executing loop body EsE112 17I17 While vs For39 Explanation int x 1 An example of while x lt 10 a while loop Systemoutprintlnx that has this patte n x x 1 for int x 1 x lt 10 x x 1 A for loop Systemoutprintlnx that 51055 the same thing Note For loops are used generally for bounded iteration EsE112 18I17 Summary of Loops Type of Loop Syntax while while condition statement5 for for exprl condition expr3 statement5 EsE112 19m