1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
Introduction to Computers and Programming Lecture 5 New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CMT Programming Software Applications Week 3 Dr. Xiaohong Gao TP Room B107 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
CPS120: Introduction to Computer Science Decision Making in Programs.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
The if…else Selection Statement
Algorithm: procedure in terms of
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Program Control using Java - Theory
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Control Statements Paritosh Srivastava.
Structural Program Development: If, If-Else
Presentation transcript:

1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch

2 2.1 Introduction Before writing a program  Have a thorough understanding of problem  Carefully plan your approach for solving it While writing a program  Know what “building blocks” are available  Use good programming principles

3 2.2Algorithms Computing problems  Solved by executing a series of actions in a specific order Algorithm is a procedure determining  Actions to be executed  Order to be executed  Example: recipe Program control  Specifies the order in which statements are executed

4 2.3Pseudocode Pseudocode  Artificial, informal language used to develop algorithms  Similar to everyday English Not executed on computers  Used to think out program before coding Easy to convert into C++ program  Only executable statements No need to declare variables

5 2.4Control Structures Sequential execution  Statements executed in order Transfer of control  Next statement executed not next one in sequence 3 control structures (Bohm and Jacopini)  Sequence structure Programs executed sequentially by default  Selection structures if, if/else, switch  Repetition structures while, do/while, for

6 2.4Control Structures C++ keywords  Cannot be used as identifiers or variable names

7 2.4Control Structures Flowchart  Graphical representation of an algorithm  Special-purpose symbols connected by arrows (flowlines)  Rectangle symbol (action symbol) Any type of action  Oval symbol Beginning or end of a program, or a section of code (circles) Single-entry/single-exit control structures  Connect exit point of one to entry point of the next  Control structure stacking

8 2.5if Selection Structure Selection structure  Choose among alternative courses of action  Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed”  If the condition is true Print statement executed, program continues to next statement  If the condition is false Print statement ignored, program continues  Indenting makes programs easier to read C++ ignores whitespace characters (tabs, spaces, etc.)

9 2.5if Selection Structure Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; Diamond symbol (decision symbol)  Indicates decision is to be made  Contains an expression that can be true or false Test condition, follow path if structure  Single-entry/single-exit

10 2.5if Selection Structure Flowchart of pseudocode statement true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: is true

if/else Selection Structure if  Performs action if condition true if/else  Different actions if conditions true or false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

if/else Selection Structure Nested if/else structures  One inside another, test for multiple cases  Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

if/else Selection Structure Example if ( grade >= 90 ) // 90 and above cout = 80 ) // cout = 70 ) // cout = 60 ) // cout << "D"; else // less than 60 cout << "F";

14 Importance of Curly Braces Print “We have a problem” if examGrade < 60 Print “We have a real problem” if examGrade < 60 and quizGrade < 10 Print “Ok” if examGrade >= 60 int examGrade, quizGrade; if (examGrade < 60) System.out.println(“We have a problem”); if (quizGrade < 10) System.out.println (“We have a real problem”); else System.out.println(“Ok”);

15 Exam Grade Flowchart int examGrade, quizGrade; if (examGrade < 60) System.out.println(“We have a problem”); if (quizGrade < 10) System.out.println(“We have a real problem”); else System.out.println(“Ok”); examGrade < 60 “We have a problem” “We have a real problem” true quizGrade < 10 “Ok” truefalse

16 Writing Cases Print “We have a problem” if examGrade < 60 Print “We have a real problem” if examGrade < 60 and quizGrade < 10 Print “Ok” if examGrade >= 60 examGrade < 60quizGrade < 10Action Case 1truefalse“We have a problem” Case 2true “We have a problem” and “We have a real problem” Case 3falsetrue/false“Ok”

17 Putting it all together examGrade < 60quizGrade < 10Action Case 1truefalse“We have a problem” Case 2true “We have a problem” and “We have a real problem” Case 3falsetrue/false“Ok” int examGrade, quizGrade; if (examGrade < 60) System.out.println(“We have a problem”); if (quizGrade < 10) System.out.printl(“We have a real problem”); else System.out.println(“Ok”); int examGrade, quizGrade; if (examGrade < 60) { System.out.println(“We have a problem”); if (quizGrade < 10) System.out.printl(“We have a real problem”); } else System.out.println(“Ok”);

18 boolean Operators Combines multiple boolean expressions If person’s age greater than or equal to 13 and less than 17, he can go to G and PG-13 rated movies, but not R rated movies if (age >= 13 && age < 17) cout << “You can go to G and PG-13” << “ rated movies, but not R” + << “ rated movies.”) << endl; boolean operators  and - && (true if all conditions are true)  or - || (true if one condition is true)  not - ! (true if conditions is false)

19 Expression Combinations operand1operand2operand1 && operand2 true false truefalse if (age >= 13 && age < 17) cout << “You can go to G and PG-13” << “ rated movies, but not R” + << “ rated movies.”) << endl; Let age = 12Let age = 16Let age = 17 fals e true fals e The && (and) operator

20 Expression Combinations operand1operand2operand1 || operand2 true falsetrue falsetrue false The || (or) operator operand!operand truefalse true The ! (not) operator Example if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; Alternative: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl;

21 Playing Cards Exercise with playing cards  Numbers represent the rank and suit of cards // Codes for suits const int SPADES = 0; const int HEARTS = 1; const int DIAMONDS = 2; const int CLUBS = 3; // Codes for nonnumeric ranks const int ACE = 1; const int JACK = 11; const int QUEEN = 12; const int KING = 13;

22 Prints a Card Name Print “rank of suit” Consider just the rank part if (rank == JACK) cout << "Jack"; else if (rank == QUEEN) cout << "Queen"; else if (rank == KING; cout << "King"; else if (rank == ACE) cout << "Ace"; else cout << rank; Notice: comparing rank to a number of different value

switch Multiple-Selection Structure switch  Test variable for multiple values  Series of case labels and optional default case switch ( variable ) { case value1: // taken if variable == value1 statements break; // necessary to exit switch case value2: case value3: // taken if variable == value2 or == value3 statements break; default: // taken if variable matches no other cases statements break; }

switch Multiple-Selection Structure true false case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s)

25 Converting if / else to a switch if (rank == JACK) cout << "Jack"; else if (rank == QUEEN) cout << "Queen"; else if (rank == KING; cout << "King"; else if (rank == ACE) cout << "Ace"; else cout << rank; switch (rank) { case JACK: cout << "Jack"; break; case QUEEN: cout << "Queen"; break; case KING: cout << "King"; break; case ACE: cout << "Ace"; break; default: cout << rank; }