1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail.

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

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
If Statements & Relational Operators Programming.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
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.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.
Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
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.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Chapter 4 Program Control Statements
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Fundamental Programming Fundamental Programming More on Selection.
Computer Science Department Relational Operators And Decisions.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
Basic Of Computer Science
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
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,
Control Flow Statements
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 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):
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
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.
Chapter 4: Making Decisions.
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.1 Control Structures (Selection)
Condition Statements.
Chapter 4: Making Decisions.
2.6 The if/else Selection Structure
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail

2 The if Statement It’s a selection or decision statement if (test-condition) statement; –test-condition is an expression that evaluates to true or false –If the condition is true, then the statement will execute; otherwise not if (10 <= 11) cout << “hello” << endl; cout << “I am after if”; Can have a block of statements instead of just one Pitfall: if(marks = 100) {cout << “Congrats!”;} –Write if(100 == marks). Now compiler catches single =

3 The if else Statement Choice between two alternatives –Pseudocode: if( ) { } else { } If is TRUE (non-zero), then are executed If is FALSE (zero), then are executed

4 The if else Statement Example: ATM Withdrawal program #include int main() { int balance = 10000; int withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; if(withdrawal <= balance) { cout << "Here is your money." << endl; } else { cout << "Sorry, insufficient balance." <<endl; } return 0; }

5 Nested ifs An if statement that is the target of another if or an else An else refers to the nearest if statement within the same block that doesn’t have an else if(i) { if(j) statement1; if(k) statement2; // this if else statement3; // is associated with // this else } else statement4; // associated with?

6 Nested ifs The inside the braces can contain any valid C++ statements, including other if-else statements! if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance?(y/n) "; cin >> answer; if (answer == 'y') { cout << "Your balance is "<<balance<< endl; } else { cout << "Ok then. "; } else { cout << "Here is your money." << endl; } cout << "Goodbye." << endl;

7 Nested ifs Another example #include int main() { int a, b, c; cout << “Enter three numbers a, b, c:\n”; cin >> a >> b >> c; if (a==b) if(b==c) cout << “a, b, c are equal\n”; else cout << “a and b are different\n”; return 0; } Let a=2, b=c=3. What’s printed?

8 The if-else-if Construction More than 2 choices. if else is itself a statement, so can follow an else: if (ch == ‘A’) agrade++;//choice #1 else if (ch == ‘B’)//choice #2 bgrade++; else SoSo++;//choice #3 Easier to read format: if (ch == ‘A’) agrade++;//choice #1 else if (ch == ‘B’) bgrade++;//choice #2 else SoSo++;//choice #3

9 The if-else-if Ladder Conditional expressions evaluated top-down; The first true condition’s statements evaluated; rest of the ladder skipped { int num_credits; cout<<"Please enter workload: "; cin>>num_credits; if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton" << endl; }

10 The switch Statement If a large number of choices that all depend on one expression’s value, better to use switch than the if-else-if construct switch (expression) { case constant1: statement(s); break; case constant2: statement(s); break; … default: statement(s); break;// not required } expression must evaluate to an integer value (includes characters); constant must be integer (or character) constant

11 The switch Statement int main() { int x=1; switch (x) { case 0: cout<<"zero \n"; break; case 1: cout<<“one \n"; break; default: cout<<“sorry! \n"; break; } return 0; } char c = ‘M’; switch(c). What case will match now? case‘M’ will match too; so will case(‘L’ + 1) int x = 1, y = 2; switch(x<y). What case will match?

12 The switch Statement int main() { int x=1; switch (x) { case 0: cout<<"zero \n"; break; case 1: cout<<“one \n"; default: cout<<“sorry! \n“; } return 0; } break necessary to exit from switch, otherwise all subsequent executed

13 The switch Statement break and default are optional but important. No two cases can have same value Can have same statements for multiple cases int medal; cout<<"Enter your rank in class: "; cin>>medal; switch (medal) { case 1: case 2: case 3: cout<<"Wow! Medal winner \n"; break; default: cout<<"Better luck next time \n"; }

14 The switch Statement int main() { int i; for(i=0; i<5; i++) { switch(i) { case 0: cout << "less than 1\n"; case 1: cout << "less than 2\n"; case 2: cout << "less than 3\n"; case 3: cout << "less than 4\n"; case 4: cout << "less than 5\n"; } cout << '\n'; } return 0; } Output? switch more efficient than if else if when more than a few constant choices Do nested switch statements yourself