CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

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:
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).
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.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Computer Science Department Relational Operators And Decisions.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Previously Repetition Structures While, Do-While, For.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
TK 1914 : C++ Programming Control Structures I (Selection)
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 CS161 Introduction to Computer Science Topic #8.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming Control Structures I (Selection).
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
The if…else Selection Statement
More on the Selection Structure
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.
Week 3 C Program Structures (Selection Structures)
EGR 2261 Unit 4 Control Structures I: Selection
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Week 3 – Selection Structures
DKT121: Fundamental of Computer Programming
Control Structures.
Chapter 4: Control Structures I (Selection)
Chapter#3 Structured Program Development in C++
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Presentation transcript:

CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

2 C ONTENTS CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING The switch statement Nested selection

L EARNING O UTCOME At the end of : Identify the concept and usage of selection control structure: able to write a program using

4 THE switch STATEMENT switch structure: alternate to if..else break; }

5 THE switch STATEMENT Semester Jan – Apr 2010 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING switch expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector Expression value can be only integral Its value determines which statement is selected for execution A particular case value should appear only once

6 THE switch STATEMENT Semester Jan – Apr 2010 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING One or more statements may follow a case label Braces are not needed to turn multiple statements into a single compound statement The break statement may or may not appear after each statement switch, case, break, and default are reserved words

7 THE switch STATEMENT Rules : When value of the expression is matched against a case value, Statements execute until break statement is found or the end of switch structure is reached If value of the expression does not match any of the case values Statements following the default label execute If no default label, and if no match, the entire switch statement is skipped A break statement causes an immediate exit from the switch structure

8 THE switch STATEMENT Example

9 THE switch STATEMENT Example (cont’d)

Example 2: For more than one options in every cases (example: capital and small letters), the following example can be done. switch statement #include void main() { char grade; cout<<”Enter your grade:”; cin>>grade; switch (grade) { case ‘A’: case ‘a’: cout<<”Excellent”; break; case ‘B’: case ‘b’: cout<<”Average”; break; case ‘C’: case ‘c’: cout<<”Poor”; break; case ‘F’: case ‘f’: cout<<”Try Again”; break; } cout<<”Hope you are satisfied with your results!”; }

11 THE nested STATEMENT Multiple selection When one control statement is located within another (nested) The rule : - Pairing and else with an if - An else is associated with the most recent if that has not been paired with an else ( an else is always belongs to the closest if)

12 THE nested if STATEMENT Semester Jan – Apr 2010 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Consider the following statements : else

13 THE nested if STATEMENT CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Exercise 2 Consider the following case : Nation’s Air force has asked you to write a program to label supersonic aircraft as military or civilian. Your program is to be given the plane’s observed speed in km/h and its estimated length in meters. For planes traveling in excess of 1100km/h, you will label those longer than 52 meters “civilian” and shorter aircraft as “military”. For planes traveling at slower speeds, you will issue an “aircraft type unknown” message.

Exercise 3 Write the C++ program based from the flowchart shown below: