DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
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.
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.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Chapter 4 Program Control Statements
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
CPS120 Introduction to Computer Science Iteration (Looping)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Controlling Execution Dong Shao, Nanjing Unviersity.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
CSI 3125, Preliminaries, page 1 Control Statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Loops in Java.
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.
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
JavaScript: Control Statements I
Web Programming– UFCFB Lecture 16
Programming Fundamentals Lecture #6 Program Control
IF if (condition) { Process… }
Outline Altering flow of control Boolean expressions
Lab5 PROGRAMMING 1 Loop chapter4.
Computer Science Core Concepts
ICT Programming Lesson 3:
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Presentation transcript:

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures

06/26/10 Control Structures A computer program is a set of statements, which are normally executed sequentially in the order in which they appear. This happens when options or repetitions of certain calculations are not necessary. However, there are situations where it is necessary to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met. These conditions are achieved by using the control structures.

06/26/10 Control Structures Control statements can be put into the following categories 1.Selection 2.Iteration 3.Jump Selection statements allow the program to choose different paths of execution based upon the outcome of an expression or the state of a variable Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops) Jump statements allow the program to execute in a nonlinear fashion

06/26/10 Selection Statements Two selection statements: if statements switch statement These statements allow to control the flow of the program's execution based upon conditions known only during the run time. if Syntax: if(condition) statement1; else statement2; The condition is any expression that returns a boolean value. The else clause is optional.

06/26/10 Selection Statements… Nested if A nested if is an if statement that is the target of another if or else. E.g.: if(i = = 10) { if(j < 20) a = b; if(k > 100) c = d; else a = c; } else a=d;

06/26/10 Selection Statements… If-Else-If Ladder A common programming construct that is based upon a sequence of nested ifs is the if else-if ladder. Syntax: if(condition) statement; else if(condition) statement; else if(condition) statement; : else statement.;

06/26/10 Selection Statements… switch The switch statement is Java's multi way branch statement. often provides a better alternative than a large series of if-else-if statements. Syntax: switch(expression) { case value1: //statement sequence break; case value2: //statement sequence break; :: default: //statement sequence break; }

06/26/10 Selection Statements… Nested switch It is also possible to use a switch as a part of the statement sequence of an outer switch statement. This is called a nested switch. Ex: switch(count) { case 1: switch(target) { // nested switch case 0: System.out.println("target is zero"); break; case 1: // no conflicts with outer switch System.out.println("target is one"); break; } break; case 2:

06/26/10 Iteration Statements The iteration statements in OOP are, while do-while for These statements creates what is commonly known as loops. a loop repeatedly executes the same set of instructions until a termination condition is met. While The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its controlling expression is true. Syntax: while(condition) { //body of loop }

06/26/10 Iteration Statements… do-while If the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all.But the do- while-loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Syntax: do { //body of the loop } while (condition);

06/26/10 Iteration Statements… For Syntax: for(initialization;condition;iteration) { //body } When the loop First starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable..

06/26/10 Iteration Statements… Nested Loops In OOP one loop can include inside another loop Ex: for() { for() { statements… }.. Exercise

06/26/10 Jump Statements.. Java allows the use following jump statements: The jump statements in OOP are, break continue Break By using break, can force an immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop.

06/26/10 Jump Statements.. Continue Sometimes it is useful to force an early iteration of a loop. It may necessary to continue running the loop, but stop processing the remainder of the code in its body for the current iteration.