Introduction to Programming with Java, for Beginners Control Structures.

Slides:



Advertisements
Similar presentations
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Introduction to Programming with Java, for Beginners Conditionals (if statements)
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Flow of Control Part 1: Selection
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
CPS120 Introduction to Computer Science Iteration (Looping)
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
19-Feb-16 Simple Control Structures boolean s, the if statement, and the while loop.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Learning Javascript From Mr Saem
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Discussion 4 eecs 183 Hannah Westra.
Control Flow (Python) Dr. José M. Reyes Álamo.
REPETITION CONTROL STRUCTURE
Chapter 3: Decisions and Loops
Lecture 6 Repetition Richard Gesick.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Logical Operators and While Loops
Lecture 4A Repetition Richard Gesick.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Repetition Control Structure
Selection Statements.
ICT Programming Lesson 3:
Chapter 2 Programming Basics.
Simple Control Structures
Logical Operators and While Loops
Simple Control Structures
PROGRAM FLOWCHART Iteration Statements.
Just Enough Java 17-May-19.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Introduction to Programming with Java, for Beginners Control Structures

1 Sequential Control Flow Consists of just a list of commands to be done in order Welcome to Dr. Java > int x = 2; > int squareOfX = x * x; > System.out.println(x); //Or just type: squareOfX 2

2 What are control Structures ? You can’t do very much if your program consists of sequential control flow The program cannot choose whether or not to perform a command The program cannot perform the same command more than once Such programs are extremely limited! Control structures allow a program to base its behavior on the values of variables

3 Recap: Boolean Boolean is one of the eight primitive types Booleans are used to make yes or no decisions All control structures use Booleans There are exactly two Boolean values, true (“yes”) and false (“no”) Boolean, true, and false are all lowercase Boolean variables are declared like any other kind of variable: Boolean hungry; boolean passingGrade; boolean taskCompleted = false;

4 Numeric & Boolean Comparisons The following comparisons each give a Boolean result: (25 > 24) && (12 == 13) //results to false (25 > 24) || (12 == 13) //results to true

5 Conditionals (“if” statements)  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 if” part

6 //Assume x is an integer if((x % 2) == 0){ System.out.println(x + “ is divisible by 2”); } “if” statement If the condition is true, then the statement(s) (i.e. instructions) will be executed. Otherwise, it/they won’t. if (condition){ statement(s) }

7 ExampleSyntax & Explanation //Determine smaller of two integers numbers & put it variable “min” int min; int x = 5; int y = 10; if (x <= y){ min = x; } else { min = y; } if (condition){ statement(s) } else { statements(s) } If the condition is true, then the statement(s) in the “if block” are executed. Otherwise, if there is an “else” part, the statement(s) in it are executed. “if-else” statement

8 Example char userChoice; // Ask user for input and store it in userChoice if (userChoice == ‘q’) System.out.println(“quitting.”); else if (userChoice == ‘a’) System.out.println(“adding.”); else if (userChoice == ‘s’) System.out.println(“saving.”); else System.out.println(“unrecognized choice.”); Cascading “if-else”

9 An if within an ifTruth Table if (condition1){ if (condition2){ statement(s) A } else{ statement(s) B } else { statements(s) C } What values must the conditions have in order for block A to run? B? C? Nested if-statements ABC condition1 T condition2

10 CodeNotes if (x > y) if (y < z) statementA; else statementB; When is statementB executed? In other words, which if is the else paired with? The infamous “dangling else” An else is paired with the last else-less if, regardless of spacing, unless { } dictate otherwise. if (x > y){ if (y < z){ statementA; } else{ statementB; }

11 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over & over Usually set up to repeat an action until some condition is satisfied, e.g. Run an application until user hits “quit” button Read characters until end of file reached Deal card hands until game over

12 Syntax of the while statement condition is a true/false (boolean) expression If condition is initially false, the statement is never executed If condition is true, statement is executed and condition is re-evaluated The statement should eventually make the loop stop while (condition){ statement(s) }

13 A while Loop to Print Numbers // Print the numbers 1 thru 10 int x = 1; while (x <= 10){ System.out.println(x); x = x + 1; }

14 An Infinite Loop // Faulty attempt to print 1 thru 10 int x = 1; while (x <= 10){ System.out.println(x); }

15 More Infinite Loops // Some infinte loops are intentional while (true){ statement(s) } // Others are not int x = 5; while (x < 10){ statement(s) which don’t change x }

16 Pseudocode and Loops initialize game while( the game isn’t over){ process player input update game state determine if game should end } if game should be saved save game quit Pseudocode is an informal, english-like, code-like way Of sketching out what you want your code to do.

17 Compute Square of first 10 numbers //In Square.java int number = 1; int squareOfNumber = 0; while (number <= 10) { squareOfNumber = number * number; System.out.println(number + " " + squareOfNumber); number = number + 1; }

18 For Loop for (init; end-test; re-init){ statement } Executes loop body as long as end-test evaluates to TRUE Initialization and re-initialization code included in loop statement Note: Test is evaluated before executing loop body

19 While vs. For CodeExplanation int x = 1; while (x <= 10){ System.out.println(x); x = x + 1; } An example of a while loop that has this pattern for (int x = 1; x <= 10; x = x + 1){ System.out.println(x); } A for loop that does the same thing Note: For loops are used generally for bounded iteration

20 Type of LoopSyntax whilewhile (condition){ statement(s) } forfor (expr1; condition; expr3){ statement(s) } Summary of Loops

21 Indentation if(x<10){ ___ x=x+1; } else{ ___ x=x-1; } while(x>0){ ___ System.out.println (x); ___ x=x-1; } Recommended indentation is from 2 to 4 spaces, but must be consistent throughout the program Most important style rule: If you are modifying or adding to an existing program, keep to the original style (whatever it may be)