Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York 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.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 6 Introduction to classes and objects.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Decision Making Selection structures (if....else and switch/case)
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Statements: Part1  if, if…else, switch 1.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 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.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Branching statements.
Presented By: Mahmoud Rafeek Alfarra
Selection (also known as Branching) Jumail Bin Taliba by
Selections Java.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
JavaScript: Control Statements.
SELECTION STATEMENTS (1)
Control Statement Examples
Introduction To Programming Information Technology , 1’st Semester
Lecture 2: Logical Problems with Choices
Selection (if-then-else)
Introduction To Programming Information Technology , 1’st Semester
Introduction To Programming Information Technology , 1’st Semester
Structured Program Development in C++
Introduction To Programming Information Technology , 1’st Semester
PROGRAM FLOWCHART Selection Statements.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Statements Paritosh Srivastava.
Comparing Data & the ‘switch’ Statement
Presentation transcript:

Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements (cont.)

 if...else Double-Selection Statement  Nested if...else Statements  Blocks  Switch  Be care  Practices  Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra

 The if...else double-selection statement allows the programmer to specify an action to perform when the condition is true and a different action when the condition is false. 3 Presented & Prepared by: Mahmoud R. Alfarra if...else Double-Selection Statement If student's grade is greater than or equal to 60 Print "Passed" Else Print "Failed" if ( grade >= 60 ) System.out.println( "Passed" ); else System.out.println( "Failed" );

4 Presented & Prepared by: Mahmoud R. Alfarra if...else Double-Selection Statement X <= 0 yes F(x) = X F(x) =- X NO Write a program to code the above flowchart HW 7.1

 Write a program to receive an integer from user and then print if it is an even or an odd 5 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Even or Odd Write the pseudo code and flowchart of the above example HW 7.2

6 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Even or Odd Write a program to receive two an integer from user and then print if it is an even or an odd HW 7.3

If student's grade is greater than or equal to 90 Print "A" else If student's grade is greater than or equal to 80 Print "B" else If student's grade is greater than or equal to 70 Print "C" else If student's grade is greater than or equal to 60 Print "D" else Print "F"  A program can test multiple cases by placing if...else statements inside other if...else statements to create nested if...else statements. 7 Presented & Prepared by: Mahmoud R. Alfarra Nested if...else Statements

8 Presented & Prepared by: Mahmoud R. Alfarra if...else Double-Selection Statement X <= 50 yes return “Success” return fail NO X <= 80 yesNO return “pass”

 Write a program to receive the user name and then password from user, and if the user name matches “onway2allah” and the password matches “muslim4ever”, the program print “open”.  Else the program print “pass is mismatched” or & “user is mismatched”. 9 Presented & Prepared by: Mahmoud R. Alfarra Example: Privacy login Write the pseudo code and flowchart of the above example HW 7.4

10 Presented & Prepared by: Mahmoud R. Alfarra Write a program to receive two an integer from user and then print if it is an even or an odd HW 7.5 Example: Privacy login

11 Presented & Prepared by: Mahmoud R. Alfarra Comparing strings : Be care Comparing strings is doing by using (ComparTo) method, not by ==. Using the following formula: String1.ComparTo(string2), this comparison results: 1 : means the string1 is larger than string2 -1: means the string2 is larger than string1 0: means the two strings are equal Using the following formula: String1.ComparTo(string2), this comparison results: 1 : means the string1 is larger than string2 -1: means the string2 is larger than string1 0: means the two strings are equal Using the following formula: “Ali”.ComparTo(“zain”) = 1 “Ali”.ComparTo(“Ali”) = 0 “yahia”.ComparTo(“ahmad”) = -1 Using the following formula: “Ali”.ComparTo(“zain”) = 1 “Ali”.ComparTo(“Ali”) = 0 “yahia”.ComparTo(“ahmad”) = -1

 The if statement normally expects only one statement in its body.  To include several statements in the body of an if (or the body of an else), enclose the statements in braces ({ and }).  A set of statements contained within a pair of braces is called a block. 12 Presented & Prepared by: Mahmoud R. Alfarra Blocks

 A block can be placed anywhere in a program that a single statement can be placed. 13 Presented & Prepared by: Mahmoud R. Alfarra Blocks Forgetting both of the braces that delimit a block will lead to logic error in a program. Forgetting one of the braces that delimits a block will lead to syntax error in a program.

14 Presented & Prepared by: Mahmoud R. Alfarra Example: Blocks if ( grade >= 60 ) System.out.println( "Passed" ); else { System.out.println( "Failed" ); System.out.println( "You must take this course again." ); }  All the instructions into the block are related to the if and else …

 Java provides the switch multiple-selection statement to perform different actions based on the possible values of an integer variable or expression.  Each action is associated with the value of a constant integral expression. 15 Presented & Prepared by: Mahmoud R. Alfarra Switch A constant value in expression of type byte, short, int or char, but not long.

16 Presented & Prepared by: Mahmoud R. Alfarra Formula: Switch switch (expr.) { case x: // instructions break; case y: // instructions break;. default : // instructions } switch (expr.) { case x: // instructions break; case y: // instructions break;. default : // instructions }

 Write a program to receive an integer from user and then print the day of this number as ▪ 1 : Saturday ▪ 2 : Sunday ▪ …. 17 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Days Write the pseudo code and flowchart of the above example HW 7.6

18 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Days Re-Write this program using one of if statements HW 7.7

قال الله تعالى‏:‏ ‏ (‏يوم تكون الجبال كالعهن المنفوش) 19 Presented & Prepared by: Mahmoud R. Alfarra

Practices 20 Presented & Prepared by: Mahmoud R. Alfarra