1 CSC103: Introduction to Computer and Programming Lecture No 11.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Control Structures Corresponds with Chapters 3 and 4.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
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.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Computer Science Department Relational Operators And Decisions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 05 (Part III) Control Statements: Part II.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Java Programming Fifth Edition Chapter 5 Making Decisions.
1 CS161 Introduction to Computer Science Topic #8.
Week 4 Program Control Structure
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
CPS120: Introduction to Computer Science Decision Making in Programs.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
Introduction to Control Statements IT108 George Mason University.
Java Programming Fifth Edition
More on the Selection Structure
Selections Java.
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.
Decisions Chapter 4.
Flow of Control.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Selection CSCE 121 J. Michael Moore.
Control Structures: Selection Statement
Program Flow.
Control Structures: Selection Statement
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 11

2 Previous lecture for for loop break break statement continue continue statement do while do while loop

3 Today’s lecture outline Example program – nested loop breakcontinue break and continue statement case case control structure

4 Example program Write a program to generate all combinations of 1, 2 and 3 using for loop i j k

5 Write a program

6 continue continue statement

7 break break statement

8 Case control structure The control statement that allows us to make a decision from the number of choices is called a switch

9 Sequence of execution switch First, the integer expression following the keyword switch is evaluated case The value it gives is then matched, one by one, against the constant values that follow the case statements case When a match is found, the program executes the statements following that case casedefault and all subsequent case and default statements as well If no match is found with any of the case statements, only the statements following the default are executed

10 case Flowchart – case control structure

11 C code – case control structure switch(2)

12 Cont.

13 Flow chart vs C code

14 Tips and traps case can be used in any order

15 Cont. char caseswitch You are also allowed to use char values in case and switch

16 Cont. At times we may want to execute a common set of statements for multiple cases Go to program

17 Cont. case Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces switch default It is possible to have a switch without a default case float float is not allowed in case statement case 5.5: – e.g. case 5.5:

18 Cont. break The break statement in a switch takes the control outside the switch continue However, use of continue will not take the control to the beginning of switch Switch can be nested switch The switch statement is very useful while writing menu driven programs

19 switchif-else switch Versus if-else Ladder switch A float expression cannot be tested using a switch case a +3 : Cases can never have variable expressions (for example it is wrong to say case a +3 : ) Multiple cases cannot use same expressions. Thus the following switch is illegal:

20 Example program Write a program to determine that the input integer is 1, 2, 3 or other than 1, 2 or 3 using switch case statement Write a program

21