© A+ Computer Science - www.apluscompsci.com if-else if-else if switch case © A+ Computer Science - www.apluscompsci.com.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Lecture Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where.
Control Structures.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 4 Computation Bjarne Stroustrup
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
3-1 Chapter 3 Flow of Control (part a - branching)
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Dependency Test in Loops By Amala Gandhi. Data Dependence Three types of data dependence: 1. Flow (True) dependence : read-after-write int a, b, c; a.
1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Selection Structures: if and switch Statements
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
01. Consider the following code segment. int x = int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n =
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
DECISION MAKING STRUCTURES Computer Programming 2.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
int num = 22; if (num > 0) if (num % 5 == 0) System.out.println(num); else System.out.println(num + “ is negative”);
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.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Chapter 3 Selection Statements
COMP 14 Introduction to Programming
Decisions Chapter 4.
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Chapter 4: Making Decisions.
CS149D Elements of Computer Science
IF if (condition) { Process… }
.Net Programming with C#
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Selection CSCE 121 J. Michael Moore.
C# Basics These slides are designed for Game Design Class
The switch statement: an alternative to writing a lot of conditionals
Relational, Logical, and Equality Operators
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
SELECTIONS STATEMENTS
JavaScript conditional
CPS120: Introduction to Computer Science
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com if-else if-else if switch case © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com String letter = "C"; int ascii=0; if(letter.equals("A")) { ascii=65; } else if(letter.equals("B")){ ascii=66; else if(letter.equals("C")){ ascii=67; else if(letter.equals("D")){ ascii=68; else{ ascii=69; out.println(ascii); if else if OUTPUT 67 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open ifelseif.java Complete the code © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com if else if int uilScore=200; if(uilScore>220) { out.println("state bound"); } else if(uilScore>200) { out.println("region bound"); else if(uilScore>180) { out.println("district bound"); else{ out.println("take more tests"); OUTPUT district bound Only one condition can be found true! © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open ifelseifuil.java Complete the code © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com switch case OUTPUT num == 30 int num = 30; switch (num) { case 11 : out.println("num == 11"); break; case 22 : out.println("num == 22"); break; case 30 : out.println("num == 30"); break; case 40 : out.println("num == 40"); break; default : out.println("does not equal"); } © A+ Computer Science - www.apluscompsci.com

What if there is no break? If you have no break, every statement after the first true condition is executed until a break is encountered or the bottom of the switch case is reached. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com switch case OUTPUT num == 30 num == 40 does not equal int num = 30; switch (num) { case 11 : out.println("num == 11"); case 22 : out.println("num == 22"); case 30 : out.println("num == 30"); case 40 : out.println("num == 40"); default : out.println("does not equal"); } © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open switchcaseone.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open switchcasetwo.java switchcasethree.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com logical operators © A+ Computer Science - www.apluscompsci.com

frequently used operators © A+ Computer Science - www.apluscompsci.com Logical frequently used operators Operator Use x||y either x or y must be true x&&y both x and y must be true !x true if x is false – false if x is true © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com logical operators int height=6; int weight=150; if(height>6||weight>150) { out.println("big un"); } else if(height<=6&&weight<=150) { out.println("little un"); OUTPUT little un © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open logical.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com nested ifs int num =75; if(num>50) { if(num>50&&num<100) if(num>50&&num<150) System.out.println(">50 && <150"); } OUTPUT >50 && <150 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open nestedifs.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Dangling Else int num=15; if(num>10){ if(num<25) out.println("jump"); }else out.println("run"); OUTPUT jump © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Dangling Else int num=35; if(num>10) if(num<25) out.println("jump"); else out.println("run"); OUTPUT run © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open danglingelse.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Start work on the labs © A+ Computer Science - www.apluscompsci.com