2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.

Slides:



Advertisements
Similar presentations
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Chapter 6 Control Structures.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 4 Loops.
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,
CS 1430: Programming in C++.
1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2 Flow of Control Copyright © 2016 Pearson, Inc. All rights reserved.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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):
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to C++ Programming Language
While loop statement condition list
REPETITION CONTROL STRUCTURE
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 2.2 Control Structures (Iteration)
TIPS: How to Be Successful
Chapter 2.2 Control Structures (Iteration)
Summary Two basic concepts: variables and assignments Basic types:
2.6 The if/else Selection Structure
Branching Statement Condition Statement Condition list 1 list
while loop Condition Statement list
Presentation transcript:

2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F

2/19/2016IT 279, Chung-Chih Li2 The Syntax of if and if/else statements if ( condition ) { statement list; } if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if/else statement. Reserved words A Boolean expression (logical expression). In C++, 0 is false, any non-zero values will be considered as true. true false A reserved word can’t be used as an identifier

2/19/2016IT 279, Chung-Chih Li3 #include using namespace std; void main() { int a, b, c; cin >> a >> b >> c; if (a > b) { } else { } cout << “ is the biggest ” ; } if (a > c) cout << “ a: ” << a; else cout << “ c: ” << c; if (b > c) cout << “ b: ” << b; else cout << “ c: ” << c; max II

2/19/2016IT 279, Chung-Chih Li4 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !

2/19/2016IT 279, Chung-Chih Li5 Relational Operators cout << (1 < 0) << endl; 0 cout 0) << endl; 1 cout << (1 == 0) << endl; 0 cout << (1 <= 0) << endl; 0 cout = 0) << endl; 1 cout "0") << endl; 1 cout << ("Yes" == "yes") << endl; 0 cout "aaa") << endl; 1 cout << (2 < 3 < 4) << endl; cout 3 > 2) << endl; cout (3 > 2)) << endl; cout << (0 < 0.5 < 0.6) << endl; == > = !=

2/19/2016IT 279, Chung-Chih Li6 Logical Operators (1 || 0) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x = 5) (((x % 2) == 0) && ((x % 3) == 0)) || && ! Assume x = 10 true false

2/19/2016IT 279, Chung-Chih Li7 De Morgan’s law I am not a female student.  I am not female or I am not a student. I will not be in my office or in the lab.  I will not be in my office and will not be in the lab. !(A && B) is same as !A || !B !(A || B) is same as !A && !B

2/19/2016IT 279, Chung-Chih Li8 Nested if/else statement if ( condition 1 ) { statement list; } else { statement list; } Indentation indicates the level of statements. if ( condition 2 ) { statement list; } else { statement list; }; statement list;

2/19/2016IT 279, Chung-Chih Li9 Example of nested if/else statement cout << "How many items do you want to buy? "; cin >> a; if (a == 1) discount = 0.1; else { if (a == 2) discount = 0.2; else { cout << "At most two items!!"; }

2/19/2016IT 279, Chung-Chih Li10 Enumeration data type int i,j; enum days {Mon, Tue, Wed, The, Fri, Sat, Sun}; enum days d1, d2=Wed; d1=d2; if (d1 < Sat) cout << d1 << “ It is a week day ” ; else cout << d1 << “ It is a weekend ” ;

2/19/2016IT 279, Chung-Chih Li11 Enumeration data type II enum days {Mon=1, Tue=2, Wed=3, The=4, Fri=5, Sat=6, Sun=7} d1; if (d1 < Sat) cout << d1 << “ It is a week day ” ; else cout << d1 << “ It is a weekend ” ; enum days {Mon=5, Tue=4, Wed=3, The=2, Fri=1, Sat=0, Sun=0} d1; if (d1 != 0) cout << d1 << “ It is a week day ” ; else cout << d1 << “ It is a weekend ” ;

2/19/2016IT 279, Chung-Chih Li12 Ambiguity in English I saw the girl with a telescope. I saw the girl with her boy friend.

2/19/2016IT 279, Chung-Chih Li13 bool a_member, married; // You don ’ t mean this:..... if (a_member) if (married) { cout << “ Input your spouse ’ s name: ” ; cin >> sname; } else cout << “ Sorry, can ’ t get in!! ” ; // A correct way if (a_member) { if (married) {..... } else {..... } Ambiguity in C++

2/19/2016IT 279, Chung-Chih Li14 Confusing nested if/else statement bool weekend; enum days = {Mon, Tue, Wed, The, Fri, Sat, Sun}; enum days d1=Mon, d2=Sun; d1 = Sun; if (d1 < Sat) if (d1 == Mon) cout << “ Have a nice week!!\n ” ; else cout << “ have a nice weekend\n ” ; cout << “ end\n ” ; if (d1 < Sat) { if (d1 == Mon) cout << “ Have a nice week!!\n ” ; } else cout << “ have a nice weekend\n ” ; cout << “ end\n ” ;

2/19/2016IT 279, Chung-Chih Li15 Cascaded if/else statements if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6) statement_1; else statement_2; if (condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6) statement_1; else statement_2; = if (condition_1) { if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6) statement_1; else statement_2; } =

2/19/2016IT 279, Chung-Chih Li16 Other forms of Nested if/else statements (Cascaded) I if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; else if (condition_6) statement_6; if (condition_1) statement_1; if (condition_2) statement_2; if (condition_3) statement_3; if (condition_4) statement_4; if (condition_5) statement_5; if (condition_6) statement_6; V.S.

2/19/2016IT 279, Chung-Chih Li17 Other forms of Nested if/else statements (Cascaded) II if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6) statement_1; else statement_2; if (condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6) statement_1; else statement_2; =

2/19/2016IT 279, Chung-Chih Li18 Switch vs. Cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; }

2/19/2016IT 279, Chung-Chih Li19 Example of switch cout << "Input an integer as the day of the week:"; cin >> i; switch (i) { case 1 :cout << "\n Sunday"; break; case 2 : cout << "\n Monday"; break; case 3 : cout << "\n Tuesday"; break; case 4 : cout << "\n Wednesday"; break; case 5 : cout << "\n Thursday"; break; case 7 : cout << "\n Saturday"; break; case 6 : cout << "\n Friday"; break; }

2/19/2016IT 279, Chung-Chih Li20 breaks in a Switch statement if (i == 1) statement_1; else if (i == 2) { statement_2; statement_3; } else if (i == 3) statement_3; else if (i == 4) statement_4; switch (i) { case 1: statement_1; break; case 2: statement_2; case 3: statement_3; break; case 4: statement_4; }

2/19/2016IT 279, Chung-Chih Li21 LoopsBranching Condition Statement list T F Condition Statement list T F

2/19/2016IT 279, Chung-Chih Li22 while Condition Statement list T F while (Condition) { Statement list }

2/19/2016IT 279, Chung-Chih Li23 Example 1: while string ans = “ n ” ; while (ans != “ Y ” && ans != “ y ” ) { cout << “ Would you marry me? ” ; cin >> ans; } cout << “ Great!! ” ; Should I put ; here? (ans != “ Y ” || ans != “ y ” ) Can I put ; here? No!! Up to you!!

2/19/2016IT 279, Chung-Chih Li24 Example 2: while int no_times; cout << “ How many times do you want to say? ” ; cin >> no_times; while (no_times != 0) { cout << “ Hello! ” << endl; no_times--; } cout << “ End!! ” << endl; Will there be any problem? while (no_times > 0) What if one inputs –1?

2/19/2016IT 279, Chung-Chih Li25 Example 3: while int a,b,sum; cout << “ This program will return the ” ; cout << “ summation of integers from a to b.\n\n ” ; cout << “ Input two integers a and b: ” ; cin >> a >> b; while (a <= b) { sum += a; a++; } cout << “ The sum is ” << sum << endl; sum = 0; sum = sum + a; Don’t forget to set sum = 0;

2/19/2016IT 279, Chung-Chih Li26 Example 4: while int a,b,sum=0; cout << “ This program will return the sum ” ; cout << “ of odd numbers between a and b.\n\n ” ; cout << “ Input two integers a and b: ” ; cin >> a >> b; while (a <= b) { if (a % 2) sum += a; a++; } cout << “ The answer is ” << sum << endl; if (a % 2 == 0) a++; while (a <= b) { sum += a; a += 2; } 3, 4, 5, 6, 7 2, 3, 4, 5, 6, 7 a b

2/19/2016IT 279, Chung-Chih Li27 Example 5: while 3N+1 problem long n,i=0; cout << “ Input an integer: ” ; cin >> n; while (n > 1) { if (n % 2) n = 3*n+1; else n /= 2; cout << ++i << “ : ” << n << endl; } cout << “ Done!! ” << endl; W ill we always get out of a loop? That is the question!! No one knows! Input an integer:7 1:22 2:11 3:34 4:17 5:52 6:26 7:13 8:40 9:20 10:10 11:5 12:16 13:8 14:4 15:2 16:1 Done!! Press any key to continue Input an integer:11 1:34 2:17 3:52 4:26 5:13 6:40 7:20 8:10 9:5 10:16 11:8 12:4 13:2 14:1 Done!! Press any key to continue Input an integer:3759 1: :5639 3: :8459 5: : : :16 84:8 85:4 86:2 87:1 Done!! Press any key to continue

2/19/2016IT 279, Chung-Chih Li28 do-while Condition Statement list T F do { Statement list } while (Condition); string ans = “ n ” ; while (ans != “ Y ” ) { cout << “ Would you marry me? ” ; cin >> ans; } cout << “ Great!! ” ; ; is required do { cout << “ Would you marry me? ” ; cin >> ans; } while (ans != “ Y ” ); cout << “ Great!! ” ;

2/19/2016IT 279, Chung-Chih Li29 Example 1: do-while int i;.... do { cout << “ Please input a number between ” << “ 10 and 20: ” ; cin >> i; } while (i 20);......

2/19/2016IT 279, Chung-Chih Li30 Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. is 893 a prime? Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ?. Is (893 % 892 == 0) ? We can write a loop to test these.

2/19/2016IT 279, Chung-Chih Li31 Example: Silly Primality Test long int p,r,i=2; cout << “ Input an positive integer: ” ; cin >> p; cout << p << “ is “ ; do { r = p % i; i++; } while (i < p && r != 0); if (i == p) cout << “ a prime number. ” ; else { cout << “ not a prime number. ” ; cout << “ The least factor is ” << --i; }

2/19/2016IT 279, Chung-Chih Li32 Break in a loop do { r = p % i; if (r == 0) break; i++; } while (i < p); The break statement in a loop will force the program to jump out of the loop immediately. do { cout << “ Would you marry me? ” ; cin >> ans; cout << “ Really? ” cin >> ans; } while (ans != “ Y ” && ans != “ y ” ); cout << “ Great!! ” ; if (ans == “ F ” || and == “ f ” ) break;

2/19/2016IT 279, Chung-Chih Li33 Continue in a loop The continue statement in a loop will force the program to check the loop condition immediately. do { cout << “ Would you marry me? ” ; cin >> ans; if (and != “ Y ” && ans != “ y ” ) continue; cout << “ Great? ” break; } while (true);....

2/19/2016IT 279, Chung-Chih Li34 Euclid Algorithm #include using namespace std; void main() { int a,b; cout << "Input two positive integers:"; cin >> a >> b; int r = a % b; while (r) { a = b; b = r; r = a % b; } cout << "The GCD is " << b << ".\n"; }

2/19/2016IT 279, Chung-Chih Li35 Primality Test with while loop #include using namespace std; void main() { bool is_prime=true; int d=2,p; cout << "Input a positive integers:"; cin >> p; while (d <= p/2) { if (p % d == 0) { is_prime=false; break; } d++; } if (is_prime)... } Can we change to while (d < p/2) ?

2/19/2016IT 279, Chung-Chih Li36 Primality Test with do-while loop #include using namespace std; void main() { bool is_prime=true; int d=2,p; cout << "Input a positive integers:"; cin >> p; do { if (p % d == 0) { is_prime=false; break; } d++; } while (d <= p/2); if (is_prime)... } Can we change to while (d < p/2) ?

2/19/2016IT 279, Chung-Chih Li37 Primality Test with do-while loop (a bit better) void main() { bool is_prime=true; int d=3,p; cout << "Input a positive integers:"; cin >> p; do { if ((p % d == 0) || (p % 2 == 0)) { is_prime=false; break; } d += 2; } while (d < p/2); if (is_prime)... }

2/19/2016IT 279, Chung-Chih Li38 Primality Test with do-while loop (yet another improvement) void main() { bool is_prime=true; int d=3,p; cout << "Input a positive integers:"; cin >> p; if (p % 2 == 0} is_prime=false; else do { if (p % d == 0) { is_prime=false; break; } d += 2; } while (d < p/2); if (is_prime)... } What if I forget else here?

2/19/2016IT 279, Chung-Chih Li39 Definite Loop In programming a definite loop is more welcome. is I.e., number of iterations is known before the loop begins, at least the upper bound is known. I.e., repeat the loop 100 times. Precisely speaking, there is no definite loop in C++

2/19/2016IT 279, Chung-Chih Li40 The general format for a for loop for (Initialization_action; Condition; Condition_update) { statement_list; } int n,f=1; cin >> n; for (i=2; i<=n; i++) { f *= i; } cout << “ The factorial of ” << n << “ is ” << f << “. ” ; 12 3 Factorial of n is n  (n-1)  (n-2) ...2  1

2/19/2016IT 279, Chung-Chih Li41 Compare: for and while for (Initialization_action; Condition; Condition_update) { statement_list; } int n,f=1; cin >> n; for (i=2; i<=n; i++) { f *= i; } cout << “ The factorial of ” << n << “ is ” << f << “. ” ; i=2; while (i<=n) { f *= i; i++; } 123 for (Initialization_action; Condition; Condition_update) { statement_list; }

2/19/2016IT 279, Chung-Chih Li42 For Loop is not really a definite loop int n,i; n = 100; for (i=1; i <= n; i++) { statement_list; } int n,i; n = 100; i = 1; while (i <= n) { statement_list; i++; } v.s.

2/19/2016IT 279, Chung-Chih Li43 break and continue The break statement in a for/while loop will force the program to jump out of the for/while loop immediately. The continue statement in a for/while loop will force the program to update the loop condition and then check the condition immediately. for (Initialization_action; Condition; Condition_update) { statement_list; }

2/19/2016IT 279, Chung-Chih Li44 Nested loops (loop in loop) cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { cout << “*”; } cout << endl; } ************* b a

2/19/2016IT 279, Chung-Chih Li45 Nested loops (2) int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j > i) break; cout << “*”; } cout << endl; } * ** *** **** b a

2/19/2016IT 279, Chung-Chih Li46 Nested loops (3) * ** *** **** b a int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b && j < i; j++) { cout << “*”; } cout << endl; } j <= i; if (j > i) break;

2/19/2016IT 279, Chung-Chih Li47 Nested loops (4) int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j < i) cout << “ ”; else cout << “*”; } cout << endl; } ************* ************ *********** ********** b a =

2/19/2016IT 279, Chung-Chih Li48 Nested loops (5) int a,i,j; cin >> a; for (i = 0; i < a; i++) { for (j=0; j<a; j++) { if (j < a-i) cout << " "; else cout << "*"; } for (j=0; j<a; j++) { if (j > i) break; cout << "*"; } cout << endl; } * *** ***** ******* ********* ***********