Control Structures.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Introduction to C Programming
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Lecture 3 Selection Statements
CHAPTER 4 DECISIONS & LOOPS
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Control Structures.
Loop Structures.
Engineering Problem Solving with C++, Etter/Ingber
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Repetition-Counter control Loop
Engineering Problem Solving with C Fundamental Concepts
Loop Control Structure.
MSIS 655 Advanced Business Applications Programming
- Additional C Statements
How to develop a program?
More Loops.
More Loops.
Chapter 4: Loops and Files
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
LOOPS BY: LAUREN & ROMEO.
Three Special Structures – Case, Do While, and Do Until
Lab5 PROGRAMMING 1 Loop chapter4.
UMBC CMSC 104 – Section 01, Fall 2016
Computer programming Lecture 3.
A LESSON IN LOOPING What is a loop?
More Loops Topics Counter-Controlled (Definite) Repetition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Learning Plan 4 Looping.
Selections and Loops By Sarah, Melody, Teresa.
Looping Structures.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Control Structures

Sequential Execution x = keyBoard.nextInt(); statement statement y = 2*x - 3; statement System.out.println(x); … … System.out.println(y); statement

Selecting Execution Route ? true statement false if(?){ statement; }

Selecting Execution Route ? true statement _1 false statement_2 if(?){ statement_1; } else{ statement_2; }

for Loops Referred to as the “counting” loop Parts of a for-loop Initialize counter Referred to as the “counting” loop Parts of a for-loop Initialize and declare counter section Test counter section Update counter section Loop body section Test counter false true statement Update counter

for Loops for(initialize; test; update){ body } for(initialize_variable; test_variable; change_variable) { //body of loop goes here }

while loop

//display all divisors of a given positive integer //d is a divisor of N input N d = 1 while d less than or equal to N{ if(d divides evenly into N){ print d } increment d by one

while d less than or equal to N{ if(d divides evenly into N){ print d input N d = 1 while d less than or equal to N{ if(d divides evenly into N){ print d } increment d by one Bench Test: N d d <= N? d divdes N? output ============================================================== 10 1 yes yes 1 ------------------------------------------------------------- 2 yes yes 2 3 yes no - 4 yes no - 5 yes yes 5 6 yes no - 7 yes no - 8 yes no - 9 yes no - 10 yes yes 10 11 no

Contrasting Loops j = 1; while(j<=5){ System.out.println(“Hello”); j = j + 1; } Remember to update j for(int j=1; j<=5; j=j+1){ System.out.println(“Hello”); }

Arithmetic Sequences a = first term d = common difference n = number of terms Sequence: a, a+d, a+2d, …, a+(n-1)d

Code outCnt = 0; for(int k=0; k<n; k++){ if(outCnt != 5) System.out.printf("%5d", a + k*d); else{ System.out.printf("%n%5d", a + k*d); } outCnt++;

Alternate Code outCnt = 0; for(int k=a; k<a+n*d; k=k+d){ if(outCnt != 5) System.out.printf("%5d", k); else{ System.out.printf("%n%5d", k); } outCnt++;

do-while Loops Bottom-test loop Always executes the body at least once Loops until the Test? returns false statement Test? true false

do-while Loops do{ statement; }while(?); while(?){ statement; } Bottom-test loop while(?){ statement; } Top-test loop

Example

Premature Loop Exit break Used to exit an enclosing loop statement Used to exit a switch statement x = keyBoard.nextInt(); while(x<=100){ System.out.println(x); if(x % 4 == 0) break; x = x + 7; }

switch Statement Used in place of a series of if-else if-else ifs When the test produces an integer value if(month==1){ … } else if(month==2){ … } … switch(month){ case 1: … break; case 2: … case 3: … break; … default: }