SELECTIONS STATEMENTS

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Computer Science Selection Structures.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Control Structures I
Chapter 3 Control Statements
CprE 185: Intro to Problem Solving (using C)
INC 161 , CPE 100 Computer Programming
2. Java language basics (2)
Decisions Chapter 4.
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Making Decisions.
C# and the .NET Framework
SELECTION STATEMENTS (1)
Java Programming: Guided Learning with Early Objects
IDENTIFIERS CSC 111.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Introduction to Java, and DrJava part 1
SELECTION STATEMENTS (2)
Numbers.
Lab5 PROGRAMMING 1 Loop chapter4.
Chapter 5: Control Structure
Introduction to Java, and DrJava
elementary programming
Single-Dimensional Arrays chapter6
Boolean Expressions to Make Comparisons
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Lab6 PROGRAMMING 1 metheds chapter5.
The Selection Structure
Introduction to Java, and DrJava part 1
Using C++ Arithmetic Operators and Control Structures
Lab4 PROGRAMMING 1 exercise chapter3.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

SELECTIONS STATEMENTS lab3 PROGRAMMING 1 SELECTIONS STATEMENTS

BEFORE WE START Your Cell Phone Silent Please Keep Quite Find Your Computer and Switch On Ins.Ebtesam AL-Etowi

content Boolean data types values. comparison operators . selection statement . conditional operator Formatting Output . Ins.Ebtesam AL-etowi

Boolean Data Type Boolean value: true or false. Example: boolean open = true; How do you compare two values, such as whether is greater than 0, equal to 0, or less than 0? The result of the comparison is a Boolean value. java provides six comparison operators (also known as relational operators), show in Table Ins.Ebtesam AL-etowi

comparison AND logic operators Ins.Ebtesam AL-etowi

Selection statements Selection statements use conditions. Conditions are Boolean expressions . Java has several types of selection statements: one-way if statements. two-way if statements. nested if statements. switch statements. Ins.Ebtesam AL-etowi

One-Way if Statements A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is shown below: if (boolean-expression) { statement(s); } Ins.Ebtesam AL-etowi

Two-Way if Statements The actions that a two-way if statement specifies differ based on whether the condition is true or false. The syntax for a two-way if statement is shown below: if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; Ins.Ebtesam AL-etowi

Two-Way if Statements(cont..) Example : Ins.Ebtesam AL-etowi

Nested if Statements The inner if statement is said to be nested inside the outer if statement. multiple alternative if statements. Ins.Ebtesam AL-etowi

Switch Statements Java provides a switch statement to handle multiple conditions efficiently. Ins.Ebtesam AL-etowi

Example :Switch statements int month = 8; switch (month) { case 1: System.out.print("January"); break; case 2: System.out.print("February"); break; case 3: System.out.print("March"); break; case 4: System.out.print("April"); break; case 5: System.out.print("May"); break; case 6: System.out.print("June"); break; case 7: System.out.print("July"); break; case 8: System.out.print("August"); break; case 9: System.out.print("September"); break; case 10: System.out.print("October"); break; case 11: System.out.print("November"); break; case 12: System.out.print("December"); break; default: } Ins.Ebtesam AL-etowi

conditional operator The syntax is shown below: boolean-expression ? expression1 : expression2; The result of this conditional expression is expression1 if boolean-expression is true; otherwise the result is expression2. For example: if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); System.out.println((num % 2 == 0)? num + “is even” :num + “is odd”); Ins.Ebtesam AL-etowi

Formatting Output Use the printf statement. System.out.printf(format, items); Where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, character, boolean value, or a string. Each specifier begins with a percent sign. Ins.Ebtesam AL-etowi

Specifier Output Example %b a boolean value true or false %c a character 'a' %d a decimal integer 200 %f a floating-point number 45.460000 %e a number in standard scientific notation 4.556000e+01 %s a string "Java is cool" Ins.Ebtesam AL-etowi

Exercise1 Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. 1 2 3 4 Ins.Ebtesam AL-etowi

OUTPUT Ins.Ebtesam AL-etowi

Exercise2 Write a program to make simple calculator , Let the user Enter the numbers then ,choose the Arithmetic operation. 1] Addition 2]Subtraction 3]Multiplication 4]Division 1 2 3 Ins.Ebtesam AL-etowi

Exercise2 Add Sub Mul Div Ins.Ebtesam AL-etowi

OUTPUT Ins.Ebtesam AL-etowi

Thank You !