Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Control Structures.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Introduction to Programming
3-1 Chapter 3 Flow of Control (part a - branching)
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
The if-else Statements
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Fundamental of C programming
Control Structures Selections Repetitions/iterations
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Decisions If statements in C.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Selections Java.
Flow of Control.
Chapter 3 Branching Statements
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Control Structures I
CMSC 202 Java Primer 2.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Controlling Program Flow
CSS 161: Fundamentals of Computing
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

As in most programming languages, flow of control in Java refers to its branching and looping mechanisms Flow of Control Branching Switch caseifIf… else Looping ForwhileDo.. while

د / احمد يوسف 3 Control Structures

4 Branching Statements – Simple if statement – if-else statement – Nested if statements – switch statement

5

Java Comparison Operators 3-6 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

د / احمد يوسف 7 Simple if Statement Syntax: if (expression) statement ; if (hour < 12) System.out.println("Good morning.");

د / احمد يوسف Example: Simple if Statement import java.util.Scanner; public class Result{ public static void main(String[] args){ Scanner keyboard=new Scanner(System.in); int grade= keyboard.nextInt(); if ( grade > = 60 ) System.out.println ( " passed " ); }

9 Example: Simple if Statement Write a Java program prints the absolute value of a number. import java.util.Scanner; class absolute { public static void main(String [] args) { Scanner kb = new Scanner(System.in); System.out.print(" Enter a number: "); int x = kb.nextInt(); int y = x; if( y < 0) y = -y; System.out.print(" The absolute value of " + x + " is " + y); }

د / احمد يوسف 10 Branching with an if-else Statement Two-Way Selection Syntax: if (expression) statement1; else statement2; The Expression must be enclosed in parentheses If the Expression is true, then the Statement1 is executed If the Expression is false, then the Statement2 is executed

Example: Even/Odd number import java.util.Scanner; class EvenOdd{ public static void main(String [] args) { Scanner kb = new Scanner(System.in); System.out.print(" Enter a number: "); int num= kb.nextInt(); if(num %2 = = 0) System.out.println ( " The Number is Even" ); else System.out.println ( "The Number is Odd" ); } 3-11

د / احمد يوسف 12 if ( grade > = 60 ) System.out.println ( " passed " ); else System.out.println ( " failed " );

13 Compound (Block of) Statements Syntax: { statement1 statement2. statementn }

Compound Statements You have to use braces if the or block has multiple statements. if only one statement is there braces are optional but it is advisable to always use them to enhance readability if (testScore < 60) { System.out.println("You did not pass"); System.out.println("Try harder next time"); } else { System.out.println("You did pass"); System.out.println("Keep up the good work"); } Then Block Else Block

Multiway if-else Statement if (Boolean_Expression) Statement_1 else if (Boolean_Expression) Statement_2 else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

if - else- if if (score >= 90) { System.out.println("Grade is A"); } else if (score >= 80) { System.out.println("Grade is B"); } else if (score >= 70) { System.out.println("Grade is C"); } else if (score >= 60) { System.out.println("Grade is D"); } else { System.out.println("Grade is F"); } Test ScoreGrade 90  score A 80  score  90 B 70  score  80 C 60  score  70 D score  60 F

The switch Statement The switch statement is multiway branching – When a switch statement is evaluated, one of a number of different branches is executed – The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch The controlling expression must evaluate to a char, int, short, or byte 3-17 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The switch Statement Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements – Each case label must be of the same type as the controlling expression – Each sequence of statements may be followed by a break statement ( break; ) 3-18 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

د / احمد يوسف 19 switch Structures

د / احمد يوسف 20 switch (expression) { case value 1: statements 1; break; case value 2: statements 2; break;... case value n: statements n ; break; default: statements }

د / احمد يوسف 21 switch With No break Statements x = 10; false true ch == 'a' ? x = 20; x = 30; ch == 'b' ? ch == 'c' ? false true public class SwitchNoBreak { public static void main( String s[] ) { char ch = 'a'; int x = 0; switch ( ch ) { case 'a': x = 10; case 'b': x = 20; case 'c': x = 30; } System.out.println( "x = " + x ); } x = 30

د / احمد يوسف 22 switch With break Statements x = 10; false true ch == 'a' ? x = 20; x = 30; ch == 'b' ? ch == 'c' ? false true break; public class SwitchWithBreak { public static void main(String s[]) { char ch = 'a'; int x = 0; switch ( ch ) { case 'a': x = 10; break; case 'b': x = 20; break; case 'c': x = 30; break; } System.out.println( "x = " + x ); } x = 10

د / احمد يوسف 23 import java.util.Scanner; class Arithmatic{ public static void main(String [ ] args){ Scanner keyboard=new Scanner(System.in); System.out.print("Enter First number"); int x= keyboard.nextInt(); System.out.print("Enter Second number“); int y= keyboard.nextInt(); System.out.print("Enter 1:Add\n 2:Sub\n 3:Mul.\n 4: Div.“); int menu = keyboard.nextInt(); switch (menu) { case 1 : System.out.println(“x+y = " + (x+y) ); break ; case 2 : System.out.println(“ x – y =" + (x - y)) ; break ; case 3 : System.out.println("x * y ="+ (x * y)) ; break ; case 4 : System.out.println("x / y ="+ (x / y)) ; break ; }

The Conditional Operator The conditional operator is a notational variant on certain forms of the if-else statement – Also called the ternary operator or arithmetic if – The following examples are equivalent: if (n1 > n2) max = n1; else max = n2; vs. max = (n1 > n2) ? n1 : n2; – If the Boolean expression is true, then the expression evaluates to the value of the first expression ( n1 ), otherwise it evaluates to the value of the second expression ( n2 ) 3-24 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Pitfall: Using == with Strings The equality comparison operator ( == ) can correctly test two values of a primitive type In order to test two strings to see if they have equal values, use the method equals, or equalsIgnoreCase string1.equals(string2) string1.equalsIgnoreCase(string2) 3-25 Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Confusion between =, ==, and equals method is one of the most frequent programming errors

Building Boolean Expressions When two Boolean expressions are combined using the "and" ( && ) operator, the entire expression is true provided both expressions are true – Otherwise the expression is false When two Boolean expressions are combined using the "or" ( || ) operator, the entire expression is true as long as one of the expressions is true – The expression is false only if both expressions are false Any Boolean expression can be negated using the ! operator – Place the expression in parentheses and place the ! operator in front of it Unlike mathematical notation, strings of inequalities must be joined by && – Use (min < result) && (result < max) rather than min < result < max 3-26 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Truth Tables 3-27 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.