Chapter 4: Control Structures I

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 4: Control Structures I (Selection)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
C++ Programming Control Structures I (Selection).
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Java Fundamentals 4.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Selections Java.
Selection—Making Decisions
DKT121: Fundamental of Computer Programming
Chapter 4: Control Structures I
SELECTION STATEMENTS (2)
Chapter#3 Structured Program Development in C++
Self study.
Visual Basic – Decision Statements
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Structured Program Development in C++
Chapter 3: Selection Structures: Making Decisions
Control Structure.
Presentation transcript:

Chapter 4: Control Structures I

Java Programming: From Problem Analysis to Program Design, D.S. Malik Chapter Objectives Learn how to use the selection control structures if, if…else, and switch in a program. Learn about loop statements Java Programming: From Problem Analysis to Program Design, D.S. Malik

Java Programming: From Problem Analysis to Program Design, D.S. Malik Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik

Java Programming: From Problem Analysis to Program Design, D.S. Malik Selection One-way selection Two-way selection Compound (block of) statements Multiple selections (nested if) Conditional operator switch structures Java Programming: From Problem Analysis to Program Design, D.S. Malik

Java Programming: From Problem Analysis to Program Design, D.S. Malik One-Way Selection Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement. Java Programming: From Problem Analysis to Program Design, D.S. Malik

Short-Circuit Evaluation A process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. Example: (x>y) || (x==5) // if (x>y) is true, (x==5) is not evaluated (a==b) && (x>=7) // if (a==b) is false, (x>=7) is not evaluated (x>0) && ( (y = z*2) > 5) // if (x>0) is false, ((y = z*2) > 5) is not evaluated and the value of y will not change Java Programming: From Problem Analysis to Program Design, D.S. Malik

Java Programming: From Problem Analysis to Program Design, D.S. Malik Two-Way Selection Syntax: if (expression) statement1 else statement2 else statement must be paired with an if. Java Programming: From Problem Analysis to Program Design, D.S. Malik

Java Programming: From Problem Analysis to Program Design, D.S. Malik Two-Way Selection Example 4-13 if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate; Java Programming: From Problem Analysis to Program Design, D.S. Malik

Compound (Block of) Statements Syntax: { statement1 statement2 . statementn } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Compound (Block of) Statements if (age > 18) { System.out.println("Eligible to vote."); System.out.println("No longer a minor."); } else System.out.println("Not eligible to vote."); System.out.println("Still a minor."); Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if Multiple if statements can be used if there is more than two alternatives else is associated with the most recent if that does not have an else. Syntax: if (expression1) statement1 else if (expression2) statement2 statement3 Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if Example 4-19 // Assume that score is of type int. Based on the value of score, the following code determines the grade if (score >= 90) System.out.println (“Grade is A”); else if (score >=80 ) System.out.println (“Grade is B”); if (score >=70 ) System.out.println (“Grade is C”); if (score >=60 ) System.out.println (“Grade is D”); System.out.println (“Grade is F”); Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if Example 4-20 if( tempreture >= 50 ) if (tempreture >= 80) System.out.println (“Good swimming day”); else System.out.println (“Good golfing day”); System.out.println (“Good tennis day”); Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if Example 4-22 //The following code does not work as intended. For example if GPA=3.8 if ( GPA >= 2.0 ) if (GPA >= 3.9) System.out.println (“Deen Honor list”); else System.out.println (“GPA below graduation requirement”); -------------------------------------------------------------- //This is the correct way of writing the above code { } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Java Programming: From Problem Analysis to Program Design, D.S. Malik Switch Structures switch (expression) { case value1: statements1 break; case value2: statements2 ... case valuen: statementsn default: statements } Expression is also known as selector. Expression can be an identifier. Value can only be integral. Java Programming: From Problem Analysis to Program Design, D.S. Malik

Switch With break Statements x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? break; switch (N) { case 1: x = 10; break; case 2: x = 20; case 3: x = 30; } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Switch With No break Statements x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Switch With Break And Default Statements Example 4-23 switch (grade) { case 'A': System.out.println("The grade is A."); break; case 'B': System.out.println("The grade is B."); case 'C': System.out.println("The grade is C."); case 'D': System.out.println("The grade is D."); case 'F': System.out.println("The grade is F."); default: System.out.println("The grade is invalid."); } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Switch With Break And Default Statements Example 4-23 switch (grade) { case 'A': System.out.println("The grade is A."); break; case 'B': System.out.println("The grade is B."); case 'C': System.out.println("The grade is C."); case 'D': System.out.println("The grade is D."); case 'F': System.out.println("The grade is F."); default: System.out.println("The grade is invalid."); } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Example 4-23 With Nested If if (grade == 'A') System.out.println("The grade is A."); else if (grade == 'B') System.out.println("The grade is B."); else if (grade == 'C') System.out.println("The grade is C."); else if (grade == 'D') System.out.println("The grade is D."); else if (grade == 'F') System.out.println("The grade is F."); else System.out.println("The grade is invalid."); Java Programming: From Problem Analysis to Program Design, D.S. Malik