1. 2 Revision To understand the term Nesting –To be able to write programs that contain nested loops. –To be able to dry run programs containing nested.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

The if-else Statements
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Control Structures Corresponds with Chapters 3 and 4.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Some revision.  Today, we will do some revision on: - ◦ booleans, and ◦ if statements.
Mock test review Revision of Activity Diagrams for Loops,
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
C++ for Engineers and Scientists Third Edition
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CPS120 Introduction to Computer Science Iteration (Looping)
Flow of Control Part 1: Selection
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Decision Making Selection structures (if....else and switch/case)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
CSC 212 Object-Oriented Programming and Java Part 2.
CPS120 Introduction to Computer Science Iteration (Looping)
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
‘C’ Programming Khalid Jamal.
Decision Making in C.
More on the Selection Structure
Lecture 3- Decision Structures
Programming Fundamentals
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Control Structures – Selection
Ruth Anderson UW CSE 160 Winter 2017
Lesson #5 Repetition and Loops.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Selection CSCE 121 J. Michael Moore.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Lab5 PROGRAMMING 1 Loop chapter4.
Homework Any Questions?.
CISC124 Labs start this week in JEFF 155. Fall 2018
CMPE212 – Reminders The other four assignments are now posted.
Objectives You should be able to describe: The while Statement
CSC215 Lecture Control Flow.
Michael Ernst UW CSE 190p Summer 2012
Programming Language  C Control Flow
Chapter 13 Control Structures
Presentation transcript:

1

2 Revision To understand the term Nesting –To be able to write programs that contain nested loops. –To be able to dry run programs containing nested loops. To introduce the concepts of Iteration –To understand how to use the while loop construct –To understand how to use the for loop construct –To understand how to use the do … while loop construct To understand a different kind of selection statement: switch

Nested if … else int a = 15; int b = 10; if(10 <= a) { if(b <= 10) { a = a * b; } else { a = a + b; } } else { a = a - b; } 3 Very poor layout. You should indent every time you have : - then clause else clause if inside another if (which is really then/else clause) Put } at the start of the line. Put { at the end of a line

Nested if … else Value of a ? int a = 15; int b = 10; if(10 <= a) { if(b <= 10) { a = a * b; } else { a = a + b; } // end inner if } else { a = a - b; } // end outer if Better layout? You should indent every time you have : - then clause else clause if inside another if (which is really then/else clause) Put } at the start of the line. Put { at the end of a line

What is the value of a ? int a = 15; int b = 10; if(25 > a) { if(10 > b) { a = a * b; } else { a = a + b; } // end inner if } else { a = a - b; } // end if 5 25

Selection - revision Have a look at this youtube video on selection In your own time watch this one 6

Additional Information A BIG problem you may encounter when using nesting of if … else code is the matching of “else” with the appropriate “if”.

String calculateGrade(int marks) { String result = "Error"; if(marks >= 70) result = "A"; else if(marks >= 60) result = "B"; else if(marks >= 50) result = "C"; else if(marks >= 40) result = "D"; result = "Fail"; else return result; } If you want to spend hours debugging your code don’t follow indenting and bracketing standards. Why won't this program compile? The return statement may not necessarily be reached. The else on line 11 should be on line 10. You can see that there is no “if” just before line 11 statement. Conform to the Coding Conventions

String calculateGrade(int marks) { String result = "Error"; if(marks >= 70){ result = "A"; } else { if(marks >= 60){ result = "B"; } else { if(marks >= 50){ result = "C"; } else { if(marks >= 40){ result = "D"; } else { result = "Fail"; } return result; } Brackets can help reduce errors. Brackets and indents can help debugging.

When will message 2 be printed? if(x>18 && x<30){ if (q > 200) { System.out.println("message 1"); } else { System.out.println("message 2"); } if(x>18 && x<30){ if (q > 200) { System.out.println("message 1"); } } else { System.out.println("message 2"); } if(x>18 && x<30) if (q > 200) System.out.println("message 1"); else System.out.println("message 2"); else is matched with most recently defined if. Here we have the same behaviour as the bracket free version Different behaviour can be achieved with different positioning of braces Indents should make reading easier The “Dangling else” Problem Answer: when x>18 and x 200

Do loop. What is the value of s ? int t = 12; int s = 2; do { t = t - 2; s = s + 3; } while(t > 8); // end do loop 11 8 t s t> True 8 8 False

While Loop. What is the value of x ? int y = 10; int x = 9; while(y > 7) { y = y - 1; x = x + 3; } // end while 12 y x y> True 9 12 True 8 15 True 7 18 False 18

For loop. What is the value of s ? int s = 15; int r; for(r=4; r>=0; r=r-2) { s = s + 5; } // end for s r r>= True 20 2 True 25 0 True False

For loop. What is the value of h ? int h = 14; int i, j, k; for(i=1; i<3; i=i+1) { for(j=2; j<3; j=j+1) { for (k=2; k<=3; k=k+1) { h = h + 1; } // end inner for } // end middle for } // end outer for 14

int h = 14; int i, j, k; for(i=1; i<3; i=i+1) { for(j=2; j<3; j=j+1) { for (k=2; k<=3; k=k+1) { h = h + 1; } // end inner for } // end middle for } // end outer for 15 h i j k i<3 j<3 k<= T 2 T 15 3 T 16 4 F 3 F 2 T 17 3 T 18 4 F 3 F 18 Value of h ?

Switch. What is the value of a ? int a = 12; int b = 2; switch(b) { case 1: a = a - b; case 2: a = b * b; case 3: a = a + b; default: a = a - 8; } a b

Switch. What is the value of b ? int a = 3; int b = 17; switch(a) { case 3: b = a + b; break; case 6: b = a * b; break; case 9: b = a * b * 2; break; default: b = a * b * 3; break; } 17 20

Switch with data type char char a = 'B'; int b = 7; Int c = 10; switch(a) { case 'A': b = c + b; break; case 'B': b = c * b; break; case 'C': b = c * b * 2; break; default: b = c * b * 3; break; } 18 70

Can we deal with upper or lower case? char a = 'a'; int b = 7; Int c = 10; switch(a) { case 'A': case 'a': b = c + b; break; case 'B': case 'b': b = c * b; break; case 'c': case 'C': b = c * b * 2; break; default: b = c * b * 3; break; } 19 17

Any Requests for Thursday? 20