Control Structures.

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

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.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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 (!)
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
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.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Control Structures if else do while continue break switch case return for.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Repetition Statements while and do while loops
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 STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution SPARCS JAVA Study.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Lecture 4b Repeating With Loops
The switch Statement, and Introduction to Looping
Loops in Java.
Chapter 5: Control Structures II
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Loop Control Structure.
Outline Altering flow of control Boolean expressions
Java - Data Types, Variables, and Arrays
In this class, we will cover:
Lab5 PROGRAMMING 1 Loop chapter4.
Control Structures Part 1
In this class, we will cover:
PROGRAM FLOWCHART Iteration Statements.
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.
Controlling Program Flow
Control Statements:.
Presentation transcript:

Control Structures

Control Structures There are three types of loops in java : For loop While loop Do while loop Here is the general form of traditional for loop, For(initialization; condition; iteration){ //body } www.prolearninghub.com

Control Structures Loop While loop Do-while loop For loop Loops in Java are used to execute the same block of code a specified number of times or while a specified condition is true. While loop Do-while loop For loop Break and Continue www.prolearninghub.com

Control Structures WhileLoop While loop Syntax: The while loop is used when one wants the loop to execute and continue executing while the specified condition is true. Syntax: While loop var intilization while (var<=endvalue) { code to be executed } www.prolearninghub.com

Control Structures WhileLoop While loop The while loop is the most basic loop in Java. while (boolean-expression) { statement1; [...] } While loop www.prolearninghub.com

Control Structures WhileLoop While loop The loop body will continue to execute as long as the looping condition is true. The looping condition is tested upon entry and when the loop body is completed. If the loop body consists of a single statement, the curly braces are not necessary. If the looping condition is false upon entry, the loop body will not be executed While loop www.prolearninghub.com

Control Structures WhileLoop Example While loop int x = 0; while(x<10) { System.out.println(x++); } While loop www.prolearninghub.com

Control Structures WhileLoop Example While loop int x = 0; while(x<10) { System.out.println(++x); } While loop www.prolearninghub.com

Control Structures WhileLoop Example While loop int x = 0; do { System.out.println(x); x = x+1; } while(x<10); While loop www.prolearninghub.com

Control Structures Loop Do-while loop The block of do-while will be executed at least once with any condition. For the next execution, it checks the condition. If it is true, then block will be executed. Do-while loop Syntax: do { statement } while(condition) www.prolearninghub.com

Control Structures Loop Do-while loop The do-while loop is identical to the while loop except that the test is evaluated at the end of the loop. Do-while loop do { statement1; [...] } while (boolean-expression); www.prolearninghub.com

Control Structures Do-while loop Because the looping condition is evaluated at the end of the loop body, the loop body is guaranteed to execute at least once. Do-while loop www.prolearninghub.com

Control Structures Loop Example Do-while loop int count = 0; do { System.out.println(count); } while(count<10); Do-while loop www.prolearninghub.com

Control Structures Loop Example Do-while loop int x = 0; do { System.out.println(x); x = x+1; } while(x<10); Do-while loop www.prolearninghub.com

Control Structures For Loop For loop For loop Is same as while loop. All the three statement are included in same line. Syntax: For loop for(var=startvalue;var<=endvalue;var=var+increment) { code to be executed } www.prolearninghub.com

Control Structures For Loop For loop When the loop first start the initialization portion of the loop is executed. Generally there is an expression that sets the value of the loop control variable. which act as a counter that control the loop .It is important to understand the initialization portion is executed just once. Next, condition is executed. This must be Boolean expression. It usually test the loop control variable against the target value. If it is true then the body of the loop is executed and if false then loop terminated. For loop www.prolearninghub.com

Control Structures For Loop Example For loop public static void main (String args[]) { int t=2,i; for(i=1; i<10; i++ ) { System.out.println(t+ ”*” +i+ “ = ” + (t*i)); } For loop www.prolearninghub.com

Control Structures For Loop Example For loop Output: I am so smart for (int i = 1; i <= 4; i++) { // repeat 4 times System.out.println("I am so smart"); } For loop Output: I am so smart www.prolearninghub.com

Control Structures For Loop Example For loop Output: int highTemp = 5; for (int i = -3; i <= highTemp / 2; i++) { System.out.println(i * 1.8 + 32); } For loop Output: 26.6 28.4 30.2 32.0 33.8 35.6 www.prolearninghub.com

Control Structures Loop Break and Continue Break: The current loop is forced to terminate immediately. Continue: It is used to force the flow of control back to the top of the loop. www.prolearninghub.com

Control Structures Work the same as in C / C++ if/else, for, while, do/while, switch i = 0; while(i < 10) { a += 1; i++; } do { a += i; } while(i < 10); if(a > 3) { a = 3; else { a = 0; for(i = 0; i < 10; i++) { switch(i) { case 1: string = “foo”; case 2: string = “bar”; default: string = “”; www.prolearninghub.com

Control Structures (Contd…) Java supports continue & break keywords also Again, works very similar to C / C++ Switch statements require the condition variable to be of char, byte, short or int type for(i = 0; i < 10: i++) { if(i == 5) continue; a += i; } for(i = 0; i < 10: i++) { a += i; if(a > 100) break; } www.prolearninghub.com

Sample Exercise What are the errors in these loops? Answer : int x = 0; while(x<10); { System.out.println(x++); } Answer : this is an endless loop it is valid if x>10 . www.prolearninghub.com

Sample Exercise What will be the output for this program ? Answer : long[] primes = new long[20]; primes[0] = 2; primes[1] = 3; long[] primes2=primes; System.out.println(primes2[0]); primes2[0]=5; System.out.println(primes[0]); Answer : 2,5 www.prolearninghub.com