Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Switch Statement. Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) {
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
If/else and switch. Assignments Due – Lab 3 No reading – study for your quiz!
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Java Programming: From The ground Up  Chapter 4 Selection and Decision: if Statements.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Copyright 2003 Scott/Jones Publishing Making Decisions.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements.
CSI 3125, Preliminaries, page 1 Control Statements.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 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.
A First Book of C++ Chapter 4 Selection.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
SELECTION STATEMENTS (1)
الكلية الجامعية للعلوم التطبيقية
Structured Program Development in C
Control Structures: Selection Statement
Program Flow.
Control Structures: Selection Statement
Presentation transcript:

Nested conditional statements

A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement or an if-else-statement in the then- part (and in the else-part) of a conditional statement !!! Nested conditional statement = a conditional statement where the then-part and/or the else-part contains another conditional statement

Programming example: determine the price for a hair cut (1)

Programming example: determine the price for a hair cut (2)

Programming example: determine the price for a hair cut (3)

Programming example: assign a letter grade to a number grade (1)

Programming example: assign a letter grade to a number grade (2)

Programming example: assign a letter grade to a number grade (3)

Programming example: assign a letter grade to a number grade (4)

Programming example: assign a letter grade to a number grade (5)

Programming example: assign a letter grade to a number grade (6)

A three-way selection construct (1) A two-way selection is a choice between 2 mutually exclusive cases:

A three-way selection construct (2) A three-way selection is a choice between 3 mutually exclusive cases:

A three-way selection construct (3) A common way to ensure that the 3 choices are mutually exclusive is as follows:

A three-way selection construct (4)

A three-way selection construct (5)

The N-way selection construct (1) A N-way selection construct looks like the following:

The N-way selection construct (2)

The switch statement: an N- way selection statement (1) an intrinsically N-way selection statement: the switch statement

The switch statement: an N- way selection statement (2) INT-EXPRESSION must be an integer valued expression The result of the INT-EXPRESSION is compared to the integer values given in the case clauses The statements following the case clause up to the break statement are executed If the result of the INT-EXPRESSION is not equal to any INT-VALUE in the case clauses, then the statements in the default case are executed

The switch statement: an N- way selection statement (3)

The switch statement: an N- way selection statement (4) The following types are automatically converted into an integer typed value in expressions:

The switch statement: an N- way selection statement (5)