1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Slide 1 of 64 Lecture C Lesson 3: Conditions and Loops Unit 1: The if Statement.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection.
Logical Operators and Conditional statements
C++ for Engineers and Scientists Third Edition
1 Repetition structures Overview while statement for statement do while statement.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
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
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
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.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
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.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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)
CS0007: Introduction to Computer Programming
Information and Computer Sciences University of Hawaii, Manoa
Selections Java.
Chapter 4: Making Decisions.
Introduction to programming in java
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4: Making Decisions.
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.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
Control Structure.
Presentation transcript:

1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:

2 Basic control structures l Selection structures »if statement »switch statement l Repetition structures »while statement »for statement »do statement l Flow breaking statements »break »continue »return l Exception handling stuctures »try { } catch ( ) { }

3 Selection structures »if statement –Syntax –Interpretation –Flow diagram for if statement –Example »if else statement –Syntax –Interpretation –Flow diagram for if else statement –Example »Group of statements »Nested if statement »switch statement –Syntax –Interpretation –Example

4 Relational Operators l To find whether a student has passed ICS102, we compare the student mark with the pass mark score. l For comparing these 2 marks, we use relational operators such as > l Comparing two data items using relational operators is called relational operation. l Expressions containing operands operated with relational operators are called relational expressions. l A relational expression will result in either true or false, with regard to the operand values l Example : Given that stuMark = 80 passMark = 60 To check for pass, the relational expression is : stuMark >= passMark Here >= is a relational operator. This expression will result in true, for those values of stuMark which are greater than or equal to the value of passMark

5 Relational Operators OperatorUseReturn true if > op1 > op2 op1 is greater than op2 >= op1 >= op2op1 is not less than o p2 < op1 < op2 op1 is less than op2 <= op1 <= op2 op1 is less than or equal to op2 == op1 == op2 op1 and op2 are equal != op1 != op2 op1 and op2 are not equal Examples : if (a > b)statement1 If (x == y) statement2

6 Logical Operators In reality, to pass ICS102, A pass in both the lecture and lab. components is required. Pass mark in the lecture component is 60 out of 75 Pass mark in the Lab component is 20 out of 25 Now the student mark has 2 components: classMark = 64 labMark = 21 Here first we have to check whether pass in class mark and whether pass in lab separately classMark >= 60 labMark >= 20 After this, only if both expressions are true, the student has passed the course. So to combine two such conditions, we use Logical operators classMark >= 60&& labMark >= 20

7 Logical Operators Where && is called AND operator which results in true if both the sides of it are true. For Boolean operands / expressions OperatorUseReturns true if && op1 && op2op1 and op2 are both true || op1 || op2 either op1 or op2 is true ! ! op1 op1 is false For && operator : Opr 1Opr 2Result True False TrueFalse

8 Logical Operators For || operator : For ! Operator : Opr 1Opr 2Result True FalseTrue FalseTrue False ! OprResult TrueFalse True

9 Operator precedence l Operator precedence from higher to lower. Operators with higher precedence are evaluated before operators with a relatively lower precedence. Operators on the same line have equal precedence. postfix operators [ ]. (params) expr++ expr-- unary operators ++expr --expr +expr - expr ~ ! creation or cast new (type)expr Multiplicative * / % Additive + - relational = instanceof equality == != Logical AND && Logical OR || Conditional ?: assignment = += -= *= /= %= &= ^= |= >= >>>=

10 if statement syntax l Syntax if (condition) statement l Interpretation »If the condition is true, the statement is executed; if condition is false, the statement is skipped »This provides basic decision making capabilities

11 if statement Flow diagram Flow diagram for if statement statement condition false true

12 if statement Example import TextIO; class IfExample { static TextIO stdin = new TextIO(System.in); static final String message1 = " Illustrates the use of if statement.\n "; static final String message2 = "Enter a number to find whether it is an even number.\n "; static final String message3 = " The Number is an Even Number\n "; public static void main(String[]args) throws java.io.IOException { int number; System.out.println(message1); System.out.println(message2); number = stdin.readInt(); if (number % 2 == 0 ) System.out.println(message3); }

13 if statement Example In the previous example, what will happen if the given number is not an even number? The answer is, the message The Number is an Even Number will not be printed. But it would be nice to print, The Number is not an Even Number To have this alternate decision, Java has one more type of if statement with else clause.

14 if else statement syntax l Syntax »An else clause can be added to an if statement to make it an if-else statement: if (condition) statement1 else statement2 Interpretation »If the condition is true, statement1 is executed; if the condition is false, statement2 is executed »This provides basic decision making capabilities with alternate decision.

15 If else statement Flow diagram Flow diagram for if else statement statement1 condition false true statement2

16 If else statement Example import TextIO; class IfExample { static TextIO stdin = new TextIO(System.in); static final String message1 = " Illustrates the use of if statement.\n "; static final String message2 = "Enter a number to find whether it is even or not.\n "; static final String message3 = " The Number is an Even Number\n "; static final String message4 = " The Number is an Odd Number\n "; public static void main(String[]args) throws java.io.IOException { int number; System.out.println(message1); System.out.println(message2); number = stdin.readInt(); if (number % 2 == 0 ) System.out.println(message3); else System.out.println(message4); }

17 Grouping of statements Several statements can be grouped together into a block statement l Block of statements are surrounded/ enclosed in a pair of matching curly braces l A block of statements can be used wherever a statement is called for in the Java syntax

18 If else if statement This is used when we want to execute one block of statements out of more than two blocks, based on a sequence of conditions. Example : int testscore; char grade; if (testscore >= 90) grade = 'A'; else if (testscore >= 80) grade = 'B'; else if (testscore >= 70) grade = 'C'; else if (testscore >= 60) grade = 'D'; else grade = 'F';

19 Nested if statement The body of an if statement or else clause can be another if statement. These are called nested if statements An else clause is matched to the latest preceeding if (no matter what the indentation implies) l Example : import TextIO; class NestedIf { static TextIO stdin = new TextIO(System.in); public static void main(String[]args) throws java.io.IOException { int number; number = stdin.readInt(); if ( number > 0 ) System.out.println(”it is +ve\n"); else if ( number < 0 ) System.out.println(” it is -ve\n"); else System.out.println(”it is 0\n"); }

20 switch statement l Syntax switch (controllingExpression) { case value1 : Statements; break; case value2 : Statements; break;. default : Statements; } l Interpretation »Use the switch statement to conditionally perform statements based on resultant value of some expression.

21 switch statement Example import TextIO; Public class DayOfWeek { static TextIO stdin = new TextIO(System.in); public static void main(String[] args) { int dayNumber; dayNumber = stdin.readInt(); switch (dayNumber) { case 0: System.out.println("Saturday"); break; case 1:System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; case 4: System.out.println("Wednesday"); break; case 5: System.out.println("Thursday"); break; case 6: System.out.println("Friday"); break; }

22 switch statement Example ( with out break statement ) import TextIO; Public class DayOfWeek { static TextIO stdin = new TextIO(System.in); public static void main(String[] args) { dayNumber = stdin.readInt(); switch (dayNumber) { case 0: System.out.println("Saturday"); case 1: System.out.println("Sunday"); case 2: System.out.println("Monday"); case 3: System.out.println("Tuesday"); case 4: System.out.println("Wednesday"); case 5: System.out.println("Thursday"); case 6: System.out.println("Friday"); } What will be the output, if the input is 3 ?