Loops CGS3416 Spring 2019 Lecture 7.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Control Structures Corresponds with Chapters 3 and 4.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
DAT602 Database Application Development Lecture 5 JAVA Review.
Fundamentals of C and C++ Programming Control Structures and Functions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Control Structures if else do while continue break switch case return for.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
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.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Core Java Statements in Java.
Lecture 4b Repeating With Loops
Chapter 4: Control Structures I
The switch Statement, and Introduction to Looping
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements I
Agenda Control Flow Statements Purpose test statement
Chapter 5: Control Structures II
Control Structures.
Control Structures - Repetition
Decision statements. - They can use logic to arrive at desired results
Looping and Repetition
Chapter 4: Control Structures I
Java I.
Starting JavaProgramming
The University of Texas – Pan American
LRobot Game.
Additional Control Structures
In this class, we will cover:
Chapter 7 Additional Control Structures
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Computer Science Core Concepts
Control Structure Chapter 3.
In this class, we will cover:
Chap 7. Advanced Control Statements in Java
Methods/Functions.
Control Structure.
Looping and Repetition
Control Statements:.
Presentation transcript:

Loops CGS3416 Spring 2019 Lecture 7

The if-else statement public class UnderstandingIfElseStatements { public static void main(String[] args) { // TODO Auto-generated method stub boolean isLightGreen = false; if(isLightGreen) { //traffic light is green System.out.println("Drive!");} else{ System.out.println("stop!");} } if ( Boolean expression) { // codes Statements; When; True; } else { // some other code False; Boolean expression is tested. If this expression is true. A series of codes is executed. These codes underneath the if statements. If the Boolean expression is false. The statements under if are skipped. And the statements in the else block are executed. In this particular statements, we will do one of two things based on the Boolean expression is true or false. And never both.

Types of Control Flow Sequential: Default mode. Statements are executed line by line. Selection: Used for decisions, branching – choosing between 2 or more alternative paths. if if - else switch Repetition: Used for looping – repeating a piece of code multiple times in a row. while do - while for There are 3 types of loop in java, the first type of loop is while, the second type is do while

Control Flow

Incrementing and Decrementing Some short-cut assignment operators (numbers) v += e; means v = v + e; // v -= e; means v = v - e; v *= e; means v = v * e; v /= e; means v = v / e; v %= e; means v = v % e; Increment and Decrement Operators ++x; // pre-increment (returns reference to new x) x++; // post-increment (returns value of old x) // shortcuts for x = x + 1 --x; // pre-decrement x--; // post-decrement // shortcuts for x = x - 1 This example is simple but it gives you a very important concept what we will use in our loop structure. Increment by 1 let’s trace through the program and see what is going on.

While Loop Declare a loop control variable public class DifferenceBetweenSelectionandLoop { public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; while (i < 10) { System.out.println("While Loop, iteration " + i); i++; } System.out.println("The While Loop Ended"); do while vs while loop The only time you should use do while loop is when you want to execute the statements inside loop at least once, even though condition expression returns false. Otherwise it’s always better to use while loop. Java while loop is used to execute a block of statements for a specific number of times while the if statement is a conditional statement which only execute once. Increment or decrement

Do while Loop public class UnderstandingDoWhileLoop { public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; do { System.out.println("Do-While Loop, iteration " + i); i++; }while(i<10); System.out.println("The Do-While Loop Ended"); } Increment or decrement

for Loop public class UnderstandingForLoop { public static void main(String[] args) { // TODO Auto-generated method stub /* for(loop control variable initialization; termination/exit condition; increment or decrement statement) */ for(int i = 0; i <10; i++) { System.out.println("For Loop, iteration " + i); } System.out.println("The For Loop Ended"); Let’s me create a simple example to show the working of for loop. Semi colons

Nested Loops Demo

Break and continue These statements can be used to alter the flow of control in loops, although they are not specifically needed. (Any loop can be made to exit by writing an appropriate test expression). break: This causes immediate exit from any loop (as well as from switch blocks). continue: When used in a loop, this statement causes the current loop iteration to end, but the loop then moves on to the next step. In a while or do-while loop, the rest of the loop body is skipped, and execution moves on to the test condition. In a for loop, the rest of the loop body is skipped, and execution moves on to the iterative statement.

For Loops with Strings For loops run a fixed number of times strings have a fixed length. Printing each of the characters in a string using a for loop

The file is located at c:\myFiles\myDoc.doc Task Use the java standard print out function to print the following sequences on screen: He said "Hello, World!“ The file is located at c:\myFiles\myDoc.doc Bachslash (\) vs forward slash:: (/) An escape sequence will always start with a backslash

Escape Sequences: \” \\ \n \t \r A way to use symbols that are already being used in Java (“ and \) Stop code and perform a task (newline, tab, and carriage return) Must be contained in quotes Will always start with a backslash \ Backslash escape sequences are very important in java, they are especially important when you are trying to print out a quote or backslash. If you are not aware of them , you will get unexpected errors. And not get the result that you desire.