Lecture Notes – Week 2 Lecture-2

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
Introduction to Control Statements IT108 George Mason University.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Elementary Programming
Making Choices with if Statements
Chapter 3: Decisions and Loops
Bill Tucker Austin Community College COSC 1315
Chapter 4: Making Decisions.
Selections Java.
Decisions Chapter 4.
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
Introduction to programming in java
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
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, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Lecture Notes – Week 3 Lecture-2
Computers & Programming Languages
SELECTION STATEMENTS (2)
Chapter 7 Conditional Statements
Repetition Control Structure
Chapter 8: More on the Repetition Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Lecture Notes – Week 4 Chapter 5 (Loops).
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Lecture Notes – Week 2 Lecture-2
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Chapter 2 Sets Active Learning Lecture Slides
statement. Another decision statement, , creates branches for multi-
Presentation transcript:

Lecture Notes – Week 2 Lecture-2 Chapters 3 & 4

One-way if Statements /* This a program fragment from the previous slide … //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } A one-way if statement starts with keyword if, followed by a Boolean expression for the condition, then followed by a statement body. 2

One-way if Statements } if (Boolean-expression) { statement(s); if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } if (Boolean-expression) { statement(s); } 3

Relational Operators The examples are Boolean expressions. A Boolean expression consists of a variable or value on each side of a relational operator. The expression is evaluated as true if the relation operator is satisfied; otherwise it is evaluated as false. 4 4

Two-way if-else Statements /* This a program fragment from the previous slide … //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } else { System.out.println("Negative input"); If there are two alternative actions to be taken, we use a two-way if-else statement: one action is taken when the Boolean expression is evaluated as true while another is taken otherwise. For each alternative action, we would need a block of statements. 5

Two-way if-else Statements if (boolean-expression) { statement(s); \\first block of statement } else { statement(s); \\second block 6 6

Multi-way if-else Statements /* This a program fragment for a multi-way if-else statement … if (score >= 90.0) System.out.print(”A”); \\ if score >= 90.0 else if (score >= 80.0) System.out.print(”B”); \\ if score < 90.0 and >= 80.0 else if (score >= 70.0) System.out.print(”C”); \\ if score < 80.0 and >=70.0 else if (score >= 60.0) System.out.print(”D”); \\ if score < 70.0 and >= 60.0 else System.out.println(”F");\\ score < 60.0 This statement has 4 nested if-else statements and 5 alternative actions (5 ways). 7 7

Multi-way if-else Statements else if else if else if else 8 8 8

Question: Write a Program that will take students marks, based on marks, his/her grade will be assigned using grade variable. At the end of IF statements. You are suppose to print the grade. Note: You need to use print statement only once and grade will contain char data type.

End