Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

Syntax: for (statement1; condition; statement2) statement; or ... Syntax: switch (expression){ case literal-1: group of statements; break; case literal-2: ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 13
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • Additional Control Structures

2
Control Structures - revisited
  • Control structures have to do with the order in
    which the program statements are executed.
  • Sequential flow is when we execute statements one
    after the other in the order they are written
    this is more or less the default.

3
Control (cont.)
  • Conditional flow is when a statement or group of
    statements is executed, or not, based on the
    current value of some condition. This is also
    called selection. We have seen this with if and
    if-these statements

4
Control (cont.)
  • Iteration is having the execution of a statement
    or group of statements to be repeated over and
    over based on some condition. We have used the
    while statement for this.

5
The do-while statement
  • Syntax do statement(s) while
    (condition)
  • Style do statements while
    (condition)
  • Note that the loop body is always executed once,
    and then the condition determines whether or not
    to repeat. This is a good choice when you want to
    allow the user to execute once and then decide
    whether or not to continue.

6
do-while (cont.)
  • Typical scenario do process
    goAgain reader.readChar(Continue? )
    while ((goAgain y) (goAgain
    Y))

7
The for statement
  • Syntax for (statement1 condition
    statement2) statement or
  • for (statement1 condition statement2) stat
    ement statement

8
The for statement
  • for (statement1 condition statement2) statemen
    t
  • Statement1 is executed once
  • The condition is evaluated.
  • If condition is true, the loop body and
    statement2 are executed.

9
for loop example
  • Add up the first 100 positive integers sum
    0 for (i1 ilt100 i) sum i
  • Equivalent to sum 0 i1 while
    (ilt100) sum i i

10
The switch statement
  • This is another conditional statement somewhat
    like an extended if statement.
  • Syntax switch (expression) case
    literal-1 group of statements break case
    literal-2 group of statements break
    case literal-n group of statements break
    default // Optional part group of
    statements break

11
Switch Example
switch (score/10) case 10 case 9
writer.println("A") break case 8
writer.println("B") break case 7
writer.println("C") break case
6 writer.println(D") break
default writer.println(F")
12
Could we hear more from the string section,
please?
Write a Comment
User Comments (0)
About PowerShow.com