SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Control Flow Statements in Programming

jamal-taylor avatar
17 diapositives · 306 Views · Nov 18, 2014

This resource provides an in-depth overview of control flow statements, focusing on decision-making, looping, and branching mechanisms in programming. Key topics include the structure and function of if-then, switch, for, while, and do-while statements. The material discusses the importance of loops for automating repetition and iterating through data and offers practical examples of each loop type. Additionally, it touches on the concept of infinite loops and best practices for coding efficiency, clarity, and maintainability.

Understanding Control Flow Statements in Programming - slide 1
01 / 17Understanding Control Flow Statements in Programming
1 / 17

Télécharger la présentation

Understanding Control Flow Statements in Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Introduction to ProgrammingG50PROUniversity of NottinghamUnit 6 : Control Flow Statements 2 Paul Tennent http://paultennent.wordpress.com/G50PRO.html Paul.tennent@nottingham.ac.uk Room C7

  2. Control Flow Statements • The statements inside your source files are generally executed from top to bottom • Control flow statements, break up the flow of execution by employing decision making, looping, and branching

  3. Control Flow Statements • Decision-making statements • if-then • if-then-else • switch • Looping statements • for • While • do-while • Branching statements • break • continue • return

  4. Why Looping? • To automate the repetition of instructions. • To iterate through data and test for certain condition • To keep attempting for some operation (such as obtaining data from a remote computer over a network)

  5. Loops: for Statement • for statement provides a compact way to iterate over a range of values. • Repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: • keep in mind that: • initializationexpression initializes the loop; it's executed once, as the loop begins. • When the Condition is checked before each iteration through the loop. When it evaluates to false, the loop terminates. • increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value. for (initialization; Condition; increment) { statement(s) }

  6. Initialization ConditionEvaluation false true Statement (s) Increment Loops: for Statement 1 for (int i=0; i<2; i++){ Statement (s); } 8 2 5 3 6 i = 0 i = 2 i = 1 4 7

  7. for Statement • The output of this program is: • Count is: 1 • Count is: 2 • Count is: 3 • Count is: 4 class ForCount { public static void main(String[] args){ for(int i=1; i<5; i++){ System.out.println("Count is: " + i); } } }

  8. while Statement • The while statement continually executes a block of statements while a particular condition is true. • The expression must return a boolean value • The while statement continues testing the expression and executing its block until the expression evaluates to false while (expression) { statement(s) }

  9. ConditionEvaluation false true Statement (s) while Statement • The statement(s) is executed over and over until the condition becomes false • statement(s) is executed Zero or more times boolean found = false; while (!found) { //code to search for a value in list //set found = true to exit }

  10. while Statement class Count { public static void main(String[] args){ int count = 1; while (count < 5) { System.out.println("Count is: " + count); count++; } System.out.println(“Outside while loop"); }//main end }//class end • The output of this program is: • Count is: 1 • Count is: 2 • Count is: 3 • Count is: 4 • Outside while loop

  11. do-while Statement • The while statement continually executes a block of statements while a particular condition is true. • The expression must return a boolean value • do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once do { statement(s) }while (expression)

  12. do-while Statement Statement (s) • The statement(s) is executed over and over until the condition becomes false • statement(s) is executed at least once boolean exit = false; do { statement(s) //set exit = true to exit } while (!exit); ConditionEvaluation true false

  13. do-while Statement class Count { public static void main(String[] args){ int count = 10; do{ System.out.println("Count is: " + count); count++; } while (count < 5) System.out.println(“Outside while loop"); }//main end }//class end • The output of this program is: • Count is: 10 • Outside while loop

  14. Infinite Loops • infinite loop, will execute until the user interrupts the program. • This is a common type of logical error.always double check your loop condition. for(int i = 2; i > 1; i++){ System.out.println("Count is: " + i); }

  15. Notes • If what you really want is to execute the loop 10 times, write the condition • Number < 10 and not asNumber <= 9 • In general, specific values such as "10" should not appear within the body of your program. You should declare them as finals at the top of the program • static final int COUNT = 10; • In Java generally you would more likely want to loop notfrom 1 to 10, but from 0 to 9. All counting in Java tends to start at zero rather than one.

  16. Course Work 2.1 Notes • 11 marks for correct execution and meeting all requirements • 4 marks on the Quality of your code • Follow a logical Structure • Define local Variables at the start of main method • Use constants when needed • Correct indentation • Good use of comments

  17. Summary • Control Flow Statements • Looping statements • for • While • do-while