CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
Chapter 4 Making Decisions
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
August 6, 2009 Data Types, Variables, and Arrays.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Control Flow Statements
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Introduction to Control Statements IT108 George Mason University.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CNG 140 C Programming (Lecture set 3)
Decision Making in C.
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
Control Structures.
Lecture 3- Decision Structures
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
PROGRAM FLOWCHART Selection Statements.
Comparing Data & the ‘switch’ Statement
Presentation transcript:

CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway

Short Circuit Logical Operators  Normal & (AND) and | (OR) operands will always evaluate each operand.  Short circuit && (AND) || (OR) will evaluate the second operand only when necessary (more efficient).  In an AND operation, if the 1st operand is false, the outcome is false, no matter what value the 2nd operand.  In an OR operation, if the 1st operand is true, the outcome of the operation is true no matter what the value of the second operand.

Short Circuit Logical Operators public class SCops { public static void main (String args[]) { int n, d, q; n = 10; d = 2; if (d != 0 && (n % d) == 0) System.out.println(d + “ is a factor of “ + n); d = 0; // now, set d to zero // Since d is zero, the 2 nd operand is not evaluated if (d != 0 && (n % d) == 0) System.out.println(d + “is a factor of ” + n); // Now, try same thing without short circuit operator // This will cause a divide by zero error. if (d != 0 & (n % d) == 0) System.out.println(d + “is a factor of ” + n); } Now both expressions are evaluated allowing a division by 0 Short circuit operator prevents division by 0

Selection – switch statements  It provides a multi way branch.  It enables a program to select among several alternatives.  Switch is generally more efficient than nested ifs  The value of an expression is successfully tested against a list of constants. When a match is found, the statement sequence associated with that match is executed.

Selection – switch statements  General form of a switch statement: switch(expression) { case constant1: statement sequence break; case constant2: statement sequence break; case constant3: statement sequence break;. default: statement sequence }

Selection – switch statements  The switch expression must be of type char, byte, short, or int. (floating-point expressions are not allowed).  Often the expression controlling the switch is simply a variable.  The case constants must be literals of a type compatible with the expression.  No two case constants in the same switch can have identical values.

Selection – switch statements  The default statement sequence is executed if no case constant matches the expression.  The default is optional; if not present, no action takes place if all matches fail.  When a match is found, the statements associated with that case are executed until the break is encountered or, in the case of default or the last case, until the end of the switch is reached.

Selection – switch statements  Sample program: public class SwitchDemo { public static void main(String args []){ int i; for (i=0;i<10;i++) switch(i) { case 0: System.out.println(“i is zero”); break; case 1: System.out.println(“i is one”); break; case 2: System.out.println(“i is two”); break; case 3: System.out.println(“i is three”); break; case 4: System.out.println(“i is four”); break; default: System.out.println(“i is five or more”); }

Selection – switch statements  Program output: i is zero i is 0ne i is two i is three i is four i is five or more

The break statement  It is “technically” optional (most applications of the switch will use it).  When encountered within a statement sequence of a case, the break statement causes program flow to exit from the entire switch.  If a break statement does not end the statement sequence associated with a case, then all statements at and following the matching case will be executed until a break (or the end of the switch) is encountered.

No break sample program class Nobreak { public static void main(String args[]) { int i; for(i=0;i<=5;i++) { switch(i) { case 0: System.out.println(“i is less than one”); case 1: System.out.println(“i is less than two”); case 2: System.out.println(“i is less than three”); case 3: System.out.println(“i is less than four”); case 4: System.out.println(“i is less than five”); } System.out.println(); } The case statements fall through here

No break sample program  Program output i is less than one i is less than two i is less than three i is less than four i is less than five i is less than two i is less than three i is less than four i is less than five

No break sample program  Program output i is less than three i is less than four i is less than five i is less than four i is less than five  Execution continues into the next case if no break statement is present

Empty cases  You can have empty cases: switch(i) { case 1: case 2: case 3: System.out.println(“i is 1, 2, or 3”); break; case 4: System.out.println(“i is 4”); break; }  If i has the value 1,2, or 3, the 1 st println() statement executes. If it is 4, the 2 nd println() statement executes.

Nested switch statements  It is possible to have a switch as part of the sequence of an outer switch.  This is called a nested switch.  Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

Nested switch statements  Program sample: switch(ch1) { case ‘A’ : System.out.println(“This is part of an outer switch”); switch(ch2) { case ‘A’ : System.out.println(“This is part of an inner switch”); break; case ‘B’ : //... } // end of inner case break; case ‘B’ : //... }