Lecture 4 Control Structures MIT – AITI 2003. What are Control Structures? Control structures are a way to alter the natural sequence of execution in.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Control Structures.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CONTROLLING PROGRAM FLOW
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
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,
Flow of Control Chapter 3 Flow of control Branching Loops
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
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.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
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
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
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computer Programming -1-
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
Chapter 4 – C Program Control
The switch Statement, and Introduction to Looping
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
The Java switch Statement
Program Flow.
PROGRAM FLOWCHART Iteration Statements.
CSC215 Lecture Control Flow.
Control Statements:.
Presentation transcript:

Lecture 4 Control Structures MIT – AITI 2003

What are Control Structures? Control structures are a way to alter the natural sequence of execution in a Java program In other words, they act as “direction signals” to control the path a program takes Control structures include: block statements decision statements loops

Block Statements A block statement groups zero or more statements between curly brackets: { */Normally, two or more statements are used inside block statements/* } From the compiler’s perspective, a block statement is equivalent to a single statement Block statements can be nested inside other block statements

Decision Statements

If Statements The “if” decision statement executes a statement conditionally if (expression) { statement; } next_statement; The (expression) must produce a boolean value, either true or false If (expression) returns true, statement is executed and then next_statement If (expression) returns false, statement is not executed and the program continues at next_statement

If-Else Statements The basic “if” statement can be extended by adding the “else” clause if (expression) { statement1; } else{ statement2; } next_statement; Again, the (expression) must produce a boolean value If (expression) returns true, statement1 is executed and then next_statement is executed. If (expression) returns false, statement2 is executed and then next_statement is executed.

Here is an example of chained if-else statements: if (grade == ‘A’) System.out.println(“You got an A.”); else if (grade == ‘B’) System.out.println(“You got a B.”); else if (grade == ‘C’) System.out.println(“You got a C.”); else System.out.println(“You got an F.”);

Switch Statements The switch statement enables you to test several cases generated by a given expression. The expression must produce a result of type char, byte, short or int, but not long, float, or double.

For example: switch (expression) { case value1: statement1; case value2: statement2; default: default_statement; } Every statement after the true case is executed The flow diagram of this control structure looks something like the following:

Break Statements in Switch Statements The break; statement tells the computer to exit the switch statement For example: switch (expression) { case value1: statement1; break; case value2: statement2; break; default: default_statement; break; }

Remember the Example… Here is an example of chained if-else statements: if (grade == ‘A’) System.out.println(“You got an A.”); else if (grade == ‘B’) System.out.println(“You got a B.”); else if (grade == ‘C’) System.out.println(“You got a C.”); else System.out.println(“You got an F.”);

Another way to do this same example is to use the switch statement Complicated if-else chains can be rewritten with the switch statement switch (grade) { case ‘A’: System.out.println(“You got an A.”) break; case ‘B’: System.out.println(“You got a B.”) break; case ‘C’: System.out.println(“You got a C.”) break; default: System.out.println(“You got an F.”) }

Loops

A loop allows you to execute a statement or block of statements repeatedly. There are three types of loops in Java: 1.for loops 2.while loops 3.Do-while loops

The for Loop for (initialization_expression; loop_condition; increment_expression){ //statement } The control of the for loop appear in parentheses and is made up of three parts. 1.The first part, the initialization_expression, sets the initial conditions for the loop, particularly the loop counter. Is executed before the loop starts. 2.Execution of the loop continues as long as the condition specified in the second part, the loop_condition (which must be an expression having the value true or false ), is true. This expression is checked at the beginning of each loop iteration, and when it is false, execution continues the statement following the loop block. 3.The third part of the control information, the increment_expression, is usually used to increment the loop counter. This is executed at the end of each loop iteration.

For example: int limit = 20; int sum = 0; for(int i = 1; i<=limit; i++){ // initialization_expression loop_condition increment_expression sum += 2; } What is the value of sum ?

Another example: for(int div = 0; div<1000; div++){ if(div % 12 == 0){ System.out.println(div+”is divisible by 12”); } } This loop will display every number from 0 to 999 that is evenly divisible by 12.

A for loop can have more than one variable set up during the initialization section (part one) and more than one statement in the change section (part three). The different variables and statement will be separated by a coma. Here is an example: For(i=0, j=0; i*j<1000; i++, j+=2){ System.out.println(i+”*”+j+“=“+i*j); } You do not have to fill every part of the control of the for loop. If you leave one field back sure to keep three semicolon. For example: for(int i=0; i>100; ){ sum+=i++ }

The while Loop while (expression){ statement } This while loop executes as long as the given logical expression between parentheses is true. When expression is false, execution continues with the statement following the loop block. The expression is tested at the beginning of the loop, so if it is initially false, the loop will not be executed at all.

For example: int limit = 20; int sum = 0; int i = 1; while(i <= limit){ sum += i++; } What is the value of sum ?

The do while Loop do{ statement; }while(expression); This loop is similar to the while loop, except that the expression controlling the loop is tested at the end of the loop block. This mean the loop block is executed at least once, even if the expression is always false.

For example: int limit = 20; int sum = 0; int i = 1; do{ sum += i; i++; }while(i <= limit);

The continue Statement The continue statement causes the loop to exit its current “trip” through the loop and start over at the first statement of the loop. Here are two examples: Example 1: While(index<=1000){ index+=5; if(index==400){ continue; System.out.println(“The index is”+index); }

Example 2: For(int i=1; i<=100; i++){ if(i%3==0){ continue; } sum += i }

Using the break Statement in Loops We have seen the use of the break statement in the switch statement. In loops, you can use the break statement to exit the current loop you are in. Here is an example: while (index <= 1000){ index += 5; if(index==400) break; Stystem.out.println(“The index is”+index); }

Nested Loops You can nest loops of any kind one inside another to any depth. Here is a example: int totalCount = 70 while(totalCount<600){ for(int i = 0; i < 10; i++){ totalCount += i; if(totalCount>400) break; }

Final example: for (int i = 1; i <= 10; i++) { if (i % 2 == 0 ) { System.out.println(“The number ” + i + “ is even.”) } else { System.out.println(“The number ” + i + “ is odd.”) }

POP QUIZ 1.In the switch statement expression must produce a result e of what type? 2.What must be used to separate each section of a for statement. 3.Which statement causes a program to go back to the statement that began a loop and then keep going from there. 4.Write a for loop that outputs in reverse sequence. 5.Write a for loop that outputs all numbers that are divisible by 3 between 0-50.