Your SlideShare is downloading. ×
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
control structure
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

control structure

165

Published on

ppt about control structure-part 1

ppt about control structure-part 1

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
165
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java I--Copyright © 2000 Tom Hunter
  • 2. Java I--Copyright © 2000 Tom HunterChapter 4Control Structures: Part I
  • 3. Java I--Copyright © 2000 Tom Hunter“ There is No goto in Java ”• Structured programming: the building blocks• There are 3 different kinds of operations in a program:perform a sequence of actions,perform a selection between alternative actions, orperform a repetition or iteration of the same action.Sequence, Selection, IterationStructured Programming
  • 4. Java I--Copyright © 2000 Tom Hunter• Sequence: one thing after anotherStructured Programmingtask1task2task3
  • 5. Java I--Copyright © 2000 Tom Hunter• Selection: making choicesStructured ProgrammingtaskAtaskB?YESNOStructuredprogramming,only one entrance,only one exit.
  • 6. Java I--Copyright © 2000 Tom Hunter• Repetition, Part I: doing the same thing again untilthere’s a reason to stop.Structured ProgrammingtaskATRUEFALSEDo while: maybe won’t ever do taskA even once.“A while loop repeats as long as a condition is true.”expression?
  • 7. Java I--Copyright © 2000 Tom Hunter• Repetition, Part II: doing the same thing again untilthere’s a reason to stop.Structured ProgrammingtaskA?TRUEFALSEDo until: will always do taskA at least once.“A Do Until loop repeats as long as a condition is false.”
  • 8. Java I--Copyright © 2000 Tom HunterStructured ProgrammingProcedural Structured Programming• Begin at the top, move to the bottom.• Each program unit has only one entrance andonly one exit.
  • 9. Java I--Copyright © 2000 Tom HunterSelection in JavaObject Oriented Programming• Within a method, procedural code.• Simple ‘if’ with or without brackets.if( expression )statement;if( expression ){statement;}
  • 10. Java I--Copyright © 2000 Tom HunterObject Oriented Programming• Simple ‘if’ with or without brackets.if( expression )statement;if( expression ){statement;}• Within brackets,a “block.”Selection in Java
  • 11. Java I--Copyright © 2000 Tom HunterObject Oriented Programming• Simple ‘if’ / ‘else’ without brackets.if( expression )statement;elsestatement;• Without brackets, limit of only one statement per branch.Selection in Java
  • 12. Java I--Copyright © 2000 Tom HunterwObject Oriented Programming• Simple ‘if’ / ‘else’ with brackets.if( expression ){statement;statement;}else{statement;statement;}Selection in Java
  • 13. Java I--Copyright © 2000 Tom Hunter• Compound ‘if’ / ‘else if’ / ‘else if’ / ‘else’.if( expression ){statement;}else if( expression ){statement;}else{statement;}Selection in Java
  • 14. Java I--Copyright © 2000 Tom Hunter• Special “Ternary” ? : Operator—shorthand ‘if’ / ‘else’1.) expression mustevaluate to True or False.2.) If expression is True,execute the command beforethe colon.3.) If expression is False,execute the command after thecolon.Do thisif expressionis TrueDo this ifexpressionis FalseSelection in JavaSystem.out.print(expression ? “True” : “False”)
  • 15. Java I--Copyright © 2000 Tom Hunterwhile—“the Do While”• The Test is FirstRepetition: while Part Iwhile( expression )statement;
  • 16. Java I--Copyright © 2000 Tom Hunterwhile—“the Do While”• The Test is FirstRepetition: while Part Iwhile( expression ){statement;statement;}The while { “Do While” } is used when you can’t predictexactly how many times your loop will be executed.The while may not be executed even once.It executes the loop while the expression is still true.w
  • 17. Java I--Copyright © 2000 Tom Hunter// DoUntil.java// Even though "c" begins the loop false,// it still executes at least once.public class DoUntil{public static void main( String args[] ){boolean c = false;do{System.out.println( ”Execute DoUntil at least once " );}while( c );System.exit( 0 );}}
  • 18. Java I--Copyright © 2000 Tom Hunterwhile—“the Do Until”• The Test is LastRepetition: while Part IIdo{statement;statement;}while( expression );wThis do/while {“Do Until”} is also used when you can’tpredict exactly how many times your loop will be executed.It executes at least once.It executes Until the expression becomes false.
  • 19. Java I--Copyright © 2000 Tom Hunter// WhileTest.java// Since "c" is already false when it reaches the// test, the loop never executes.public class DoWhile{public static void main( String args[] ){boolean c = falsewhile( c ){System.out.println( ”Execute DoWhile while c is true" );}System.exit( 0 );}}
  • 20. Java I--Copyright © 2000 Tom Hunter// SelectionTest.javaimport javax.swing.*;public class SelectionTest{public static void main( String args[] ){int b, s;String big, small, out;big = JOptionPane.showInputDialog( "Big Number" );small = JOptionPane.showInputDialog( "Small Number" );b = Integer.parseInt( big );s = Integer.parseInt( small );out = ( b > s ? "Big was larger" : "Small was larger" );JOptionPane.showMessageDialog( null, out, "Results",JOptionPane.INFORMATION_MESSAGE);System.exit( 0 );}}• Test Trinary Operator
  • 21. Java I--Copyright © 2000 Tom Hunter// DoWhileTest.javaimport javax.swing.*;public class DoWhileTest{public static void main( String args[] ){int b = 2, s = 1;String big, small, out = “Big is still Bigger”;while( b > s ){JOptionPane.showMessageDialog( null,out,"Results",JOptionPane.INFORMATION_MESSAGE);big = JOptionPane.showInputDialog( "Big Number" );small = JOptionPane.showInputDialog( "Small Number" );b = Integer.parseInt( big );s = Integer.parseInt( small );}System.exit( 0 );}}• Test DoWhile
  • 22. Java I--Copyright © 2000 Tom Hunter// DoUntilTest.javaimport javax.swing.*;public class DoUntilTest{public static void main( String args[] ){int b = 2, s = 1; // preload variables.String big, small, out = “Big is still Bigger”;do{JOptionPane.showMessageDialog( null,out,"Results",JOptionPane.INFORMATION_MESSAGE);big = JOptionPane.showInputDialog( "Big Number" );small = JOptionPane.showInputDialog( "Small Number" );b = Integer.parseInt( big );s = Integer.parseInt( small );}while( b > s );System.exit( 0 );}}• Test DoUntil
  • 23. Java I--Copyright © 2000 Tom HunterRepetition: while Loops• The majority of applications use the plain while loop.• Choose either while loop when you can’t know inadvance how many times the loop will be executed.• The loop is repeated until it encounters a sentinel value,that announces that the loop has finished.
  • 24. Java I--Copyright © 2000 Tom HunterAssignment Operators• We are already familiar with this statement:int x;x = 15;• This means the value 15 is placed into the variable x.• We say, “15 is assigned to x.”• In Java, a single equals sign is the assignment operator.
  • 25. Java I--Copyright © 2000 Tom HunterAssignment Operators• Another common bit of code is this:int x; Declares x as an int.x = 15;After this assignment,x contains 15.x = x + 5;After this assignment,x contains 20.First the addition on theright is done. Then, theresult is assigned to the thevariable on the left.
  • 26. Java I--Copyright © 2000 Tom HunterAssignment Operators• Java offers a shortcut to the statement below:int x = 15;x = x + 5;After this assignment,x contains 20.x += 5;After this assignment,x contains 20.+=
  • 27. Java I--Copyright © 2000 Tom HunterAssignment Operators• Java offers a shortcut to the statement below:int x = 15;x = x - 5;After this assignment,x contains 10.x -= 5;After this assignment,x contains 10.-=
  • 28. Java I--Copyright © 2000 Tom HunterAssignment Operators• Java offers a shortcut to the statement below:int x = 15;x = x * 5;After this assignment,x contains 75.x *= 5;After this assignment,x contains 75.*=
  • 29. Java I--Copyright © 2000 Tom HunterAssignment Operators• Java offers a shortcut to the statement below:int x = 15;x = x / 5;After this assignment,x contains 3.x /= 5;After this assignment,x contains 3./=
  • 30. Java I--Copyright © 2000 Tom HunterAssignment Operators• Java offers a shortcut to the statement below:int x = 15;x = x % 5;After this assignment,x contains 0.x %= 5;After this assignment,x contains 0.%=
  • 31. Java I--Copyright © 2000 Tom HunterIncrement/Decrement Operators• There is one addition statement in Java that is socommon, it gets its own operator.• It means, simply, “Add one to the variable.”x++ x--
  • 32. Java I--Copyright © 2000 Tom HunterIncrement/Decrement Operators• Java offers a shortcut to the statement below:int x = 2;x = x + 1;After this assignment,x contains 3.x++;After this assignment,x contains 3.x++
  • 33. Java I--Copyright © 2000 Tom HunterIncrement/Decrement Operators• Java offers a shortcut to the statement below:int x = 2;x = x - 1;After this assignment,x contains 1.x--;After this assignment,x contains 1.x--
  • 34. Java I--Copyright © 2000 Tom HunterIncrement/Decrement Operators• You can do a pre-increment, or a post-increment.Pre Post++x; x++;--x; x--;• If each of these statements is on a line by itself, there isno difference in the effect of doing a pre- or post-increment.
  • 35. Java I--Copyright © 2000 Tom HunterIncrement/Decrement Operators• If each of these statements is on a line by itself, there isno difference in the effect of doing a pre- or post-increment.• However, if the variable—which is having the pre- orpost-increment applied to it—is used within anotherstatement, your choice of pre- or post-increment can alteryour results.
  • 36. Java I--Copyright © 2000 Tom Hunter// PrePostIncrement.javapublic class PrePostIncrement{public static void main( String args[] ){int x=0;System.out.println( " Baseline, x = " + x );System.out.println( "n Pre-increment = ++x = " + ++x );System.out.println( "n After increment, x = " + x );x = 0;System.out.println( "n 2nd Baseline, x = " + x );System.out.println( "n Post-increment = x++ = " + x++ );System.out.println( "n After increment, x = " + x );System.exit( 0 );}}Baseline, x = 0Pre-increment = ++x = 1After increment, x = 12nd Baseline, x = 0Post-increment = x++ = 0After increment, x = 1w
  • 37. Java I--Copyright © 2000 Tom Hunter

Ă—