Control statements Mostafa Abdallah

Slides:



Advertisements
Similar presentations
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Advertisements

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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
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.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
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.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Switch Statements Comparing Exact Values
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Programming Fundamentals
Boolean Expressions & the ‘if’ Statement
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,
Control Structures.
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Chapter 3: Program Statements
CS139 October 11, 2004.
Logic of an if statement
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)
Chap 7. Advanced Control Statements in Java
Conditionals and Loops
CprE 185: Intro to Problem Solving (using C)
Boolean Expressions & the ‘if’ Statement
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Control statements Mostafa Abdallah

Agenda Conditional Statements  If.  If else.  Switch. 3-2

Conditional Statements 3-3

3-4 The order of statement execution through a method is linear: one statement after another in sequence. Some programming statements allow us to:  Decide whether or not to execute a particular statement.  Execute a statement over and over, repetitively. Conditional Statements

Continued. The Java conditional statements are the:  if statement.  if-else statement.  switch statement. 3-5

The if Statement 3-6

if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If condition is true: statement is executed. If condition is false: statement is skipped. The if Statement The if statement has the following syntax: 3-7

condition evaluated statement true false Logic of an if statement 3-8

A condition often uses one of Java's equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between ( == ) and ( = ). Boolean Expressions 3-9

An example of an if statement: Example of if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum);  First the condition is evaluated.  If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped.  Either way, the call to println is executed next. 3-10

Continued. What do the following statements do? if (top >= MAXIMUM) top = 0; Sets top to zero if the current value of top is greater than or equal to the value of MAXIMUM 3-11 Example of if Statement

Continued. What do the following statements do? if (total != stock + warehouse) inventoryError = true; Sets a flag to true if the value of total is not equal to the sum of stock and warehouse  Arithmetic operators is higher in precedence than the equality and relational operators Example of if Statement

Logical Operators Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results 3-13

Logical Not This operation is also called logical negation or logical complement. If some boolean condition:  if a is true, then !a is false;  if a is false, then !a is true; Logical expressions shown using a truth table 3-14 a!a truefalse true

Logical AND The logical AND expression: a && b Is true if both a and b are true, and false otherwise 3-15 aba && b true false truefalse

Logical OR The logical OR expression: a || b is true if a or b or both are true, and false otherwise aba || b true falsetrue falsetrue false

Logical Operators Expressions that use logical operators can form complex conditions. if (total < MAX + 5 && !found) System.out.println ("Processing…");  All logical operators have lower precedence than the relational operators  Logical NOT has higher precedence than logical AND and logical OR. 3-17

Short-Circuited Operators The processing of logical AND and logical OR is short-circuited. If the left operand is sufficient to determine the result, the right operand is not evaluated.  This type of processing must be used carefully 3-18 if (count != 0 && total/count > MAX) System.out.println ("Testing…");

The if-else Statement 3-19

The if-else Statement An else clause can be added to an if statement to make an if-else statement.  If the condition is true, statement1 is executed; if the condition is false, statement2 is executed  One or the other will be executed, but not both 3-20 if ( condition ) statement1; else statement2;

Logic of an if-else statement 3-21 condition evaluated statement1 true false statement2

If-else Statement Example if (radius >= 0) { area = radius * radius * ; System.out.println("The area for the “ + “circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); } 3-22

Nested if Statements 3-23

The switch Statement 3-24

The switch Statement The switch statement provides another way to decide which statement to execute next. The switch statement evaluates an expression, then attempts to match the result to one of several possible cases. 3-25

The switch Statement Continued. Each case contains one value (a constant) and a list of statements. The flow of control transfers to statement associated with the first case value that matches. 3-26

Switch Syntax The general syntax of a switch statement is: 3-27 switch and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } If expression matches value2, control jumps to here

Switch example An example of a switch statement: 3-28 switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; }

Switch rules A break statement is used as the last statement in each case's statement list. A break statement causes control to transfer to the end of the switch statement. If a break statement is not used, the flow of control will continue into the next case. 3-29

Switch rules Continued. A switch statement can have an optional default case. The default case has no associated value and simply uses the reserved word default. If the default case is present, control will transfer to it if no other case value matches. 3-30

Switch rules Continued. If there is no default case, and no other value matches, control falls through to the statement after the switch. The implicit boolean condition in a switch statement is equality 3-31

Switch rules Continued. The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int, long) or a char. It cannot be a boolean value or a floating point value (float or double). You cannot perform relational checks with a switch statement. 3-32

Questions

Thanks