DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.

Slides:



Advertisements
Similar presentations
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Advertisements

Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and 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.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
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.
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.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
Chapter 4: Control Structures II
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Java Fundamentals 4.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Outline Altering flow of control Boolean expressions
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Control Statements Loops.
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Presentation transcript:

DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement 2 Statement 3 True Condition Statement (s) False Statement (s)

Selectionstatement(if..)(if..)  The if… statement is used to test a condition. If defined condition is true the statements are executed otherwise they are ignored.  The condition may be simple or etc.) and must be enclosed in ( complex ). (using &&, ||(using &&, ||  Expression defined True or False. asasconditionconditionmustbeevaluatedevaluatedas Syntax In case ofsingle statement in if… the use of {} is optional. if ( ch>=‘0’ && ch<=‘9’ ) { jLable1.setText(“It is digit”); } if (condition) { statement 1 ; …………….. } if ( num>0) { jLable1.setText(“Number is positive”); }

Selection statement(if..else..)  The if…else..also tests condition. If defined condition is true else the statements in Trueblockareareexecutedotherwise blockisisexecuted. { } In case of single statement{}{}is optional. if ( age>=18) jLable1.setText(“Eligible to Vote”); else jLable1.setText(“Not eligible to Vote”); if ( num>0) { jLable1.setText(“Number is positive”); } else jLable1.setText(“Number is zero or negative”); if (condition) { statement 1 ; …………….. } else { statement 2; ……………. }

if(condition 2) { …….. } else { ……… } if(condition 3) { …….. } else { ……… } Nestedif...if...  An if… or if..else.. may have another if.. Statement in its true block or in false block. It is known as Nesting of if (placing an if inside another if). else if (condition1){ else{ } if ( num>0) { jLable1.setText(“Number is positive”); } else { if (num<0) jLable1.setText(“Number is negative”); jLable1.setText(“Number is zero”); } Nested if.. block

If…else…Ifladder  When a else block contains another if.. and this nested else block may have another if and so on. This nesting is often known as if..else..if ladder or staircase. jLable.setText(“Sunday”); jLable.setText(“Monday”); elseelse jLable.setText(“Wednesday ”); if(day==5) elseelse jLable.setText(“Friday”); jLable.setText(“Saturday”); if (day==1) else if (day==2) else if (day==3) jLable.setText(“Tuesday”); if(day==4) else jLable.setText(“Thrusday”); if(day==6) else if (condition1) statement 1; else if (condition 2) statement 2; else if (condition 3) statement 3; else if(condition 4) statement 4; ……… else statement n;

Conditional operator and if.. statement The ? : (conditional operator) may be used as alternative to if..else.. statement in Java. In contrast to if…, ?: is more concise and compact code it is less functional. ?: produces an expression so that a single value may be incorporated whereas if.. is more flexible, whereas you may use multiple statements, expressions and assignments. When ?: is used as nested form, it becomes more complex and difficult to understand.     Syntax C = a>b ? a : b ; if ( a>b) c=a; else c=b; Expression 1 ? Expression 2: expression 3;

TheTheswitchstatement  Multi-branching selection can be made in Java by using switch statement.  It test successively, the value of an expression (short, int, long or char type), when match is found, the statements associated with constant is executed. 1. No two identical constant can be used. 2. Default.. is optional may be anywhere in switch block. switch ( ) { case : statement (s); break; case : statement (s); break; case : statement (s); break; ……….. [default : statement (s);] } switch (day) { case 1 : Dow=“Sunday”; break; case 2 : Dow=“Monday”; break; case 3 : Dow=“Tuesday”; break; case 4 : Dow=“Wednesday”; break; case 5 : Dow=“Thursday”; break; case 6 : Dow=“Friday”; break; case 7 : Dow=“Saturday”; break; default : Dow=“Wrong Input”; } jLable.setText(“Weak day”+Dow);

Switch and if..else statement The switch and if..else both are used to make a selection construct in Java, but there some differences.  Switch can test only equality whereas if.. Can evaluate any type of relational or logical expression.  In switch a single value or constant can be tested but in if.. more versatile expression can be tested.  The switch statement can handle only byte, short, int or char variable but If.. can test more data type like float, double or string etc. { case :switch (exp2) } if ( condition 1) { switch (exp1) { ……. ……. } else { …….; } switch (exp 1) { ……. ……. } ………… Nesting of switch with switch or if.. may be used….

Iteration(looping) statements Iteration or looping allow a set of instructions to be executed repeatedly until a certain condition is true or false. As per placing of condition to be tested, a loop may be Entry-controlled or Exit-controlled loop. In Entry controlled loop, a condition is tested (pre test) before executing the statements. Whereas in Exit-controlled statements are executed first then condition is tested (post test), which ensures at least on time execution of statements. As per number of execution, a loop may be Counter- controlled or Sentinel loop. Counter controlled loops are executed fixed number of times, but sentinel loop may be    stopped any time by giving its sentinel value. i.e. number execution can not be forecasted. A body of loop contains a block, having statements to be executed. ofof 

TheThefor..for..loop InInsimple use, afor.. Loop is Entry-controlled and counter controlled loop having fixed number of iteration. for (int i=1; i<=10 ; i++) {) { } //loop to get sum of first 10 nos. int sum=0; sum=sum+ i; System.out.println (“”+sum); //loop to find factorial int fact=1,num=5; for (int i=1; i<=num ; i++) fact=fact * i; System.out.println (“factorial=”+fact); for (int i=1; i<=10 ; i++ ) { System.out.println (“”+i); } //loop to find even nos. up to 50 for (int i=0; i<=50 ; i=i+2) System.out.println (“”+i); for (initialization exp (s) ; condition ; update exp (s) ) { ……….. ……….. Looping statements }

Variationinfor..looploop Multiple initialization and update expression A for.. Loop may contain multiple initialization and/or expressions separated by (,) for (i=0,sum=0;i<=n; sum++, i++) Optional Expressions In for loop initialization, condition and update section omitted. Note that (;) must be present. for ( ; i<=n ; ) Infinite loop  update  may be  For.. Loop may be endless (infinite) when defined condition is always true or it is omitted. for ( ; ; ) Empty loop for.. Loop may not contain any statement in looping body i.e. Empty loop. If (;) placed after for.. Loop then empty loop is created. for (int i=1; i<=300 ; i++) ; Variable declared inside for.. Loop can’t accessed outside the loop. Since it is out of scope.  

TheThewhile..looploop In simple use, awhile.. Loop is Entry-controlled and counter controlled or sentinel looping statement, which executes statements until defined condition is true. } A while loop also may be empty or infinite int i=1; while ( i<=10) { i=i+1; System.out.println (“”+i); //while loop to find factorial int fact=1,num=5,i=1; while (i<=num) { fact=fact * i; i++; } System.out.println (“factorial=”+fact); while (condition) { ……….. ……….. Looping statements }

TheThedo..while loop Unlike for.. and while.. Loop, do..while loop is Exit-controlled and counter controlled or sentinel looping statement, which executes statements until defined condition is true. It always executes at least once. A do…while loop also may be empty or infinite //do.. loop fixed time int i=1; do { System.out.println (“”+i); i++; } while (i<=10); // do.. Loop to print A – Z letters char ch =‘A’; do { System.out.println (“”+i); ch++; } while (ch<=‘Z’); do { ……….. ……….. Looping statements } while (condition);

Whichloop is better?  Java offers three looping statements i.e. for.., while.. and do..while. There are some situation where one loop is more appropriate than others.  The for loop is best suited where number of execution is known in advance. (fixed execution)  The while and do.. Are more suitable in the situations where it is not known that when loop will terminate. (unpredictable times of execution).  The do.. Loop ensures at least one time of execution since it is Exit-controlled loop.

Jump statementinJavaJava Java offers three jump statements (return, break and continue), which transfers the control else where unconditionally.  The return statement can be used any where in the program. It transfers the control to calling module or Operating System. However Java provides System.exit() method to stop the execution of program.  The break is used with for.., while, do.. and switch statements which enables the program to skip over some part of the code and places control just after the nearest closing of block. It is used to terminate the loop.  The continue statement is used within looping statement (not with switch) and works like break i.e. it also skips the statements. Unlike break, it forces the next iteration of loop by skipping the in between code and continues the loop. 

BreakandandContinuethetheloop Do { statement 1; if (condition) continue; ……. statement 2; } While (test condition) Statement 3; While (condition 1) { statement 1; if (condition 2) continue; ……. statement 2; } Statement 3; for (ini;cond;update) { statement 1; if (condition) continue; ……. statement 2; } Statement 3; Do { statement 1; if (condition) break; ……. statement 2; } While (test condition) Statement 3; for (ini;cond;update) { statement 1; if (condition) break; ……. statement 2; } Statement 3; While (condition 1) { statement 1; if (condition 2) break; ……. statement 2; } Statement 3;

Using Labels with Break and Continue statement Java also provides a mechanism for exiting nested loops. We can define a Label (flag). Label is an identifier followed by (:), which identifies a place where control is to be transferred. In such case we may use Label with break or continue statement. Outer: for (int i=0; i<=n; i++) { ……… Inner: for(int j=1;j<=m;j++) { if (condition) break Outer; else break Inner; ……. } statement 2; } Statement 3; break [label name]; continue [label name];

{ int y=5; ………… } { int z=3; ……. } ScopeScopeofofa variable  In Java, a variable can be declared any where in the program but before using them.  The area of program within which a variable is accessible, known as its scope.  A A variablevariablecancanbebeaccessedwithin the blockwhere itwhere itisisdeclared. Scope ofx ScopeScopeof yof y ScopeScopeof zof z Yand zand zare not accessible here { int x=10; if (a>b) else ………. }