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.

Slides:



Advertisements
Similar presentations
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
Computer Science 1620 Loops.
Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous.
Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
The If/Else Statement, Boolean Flags, and Menus Page 180
Thursday, December 21, 2006 The Briggs/Chase Law of Program Development: To determine how long it will take to write and debug a program, take your best.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
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.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Loops and Iteration for Statements, while Statements and do-while Statements.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
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,
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
1 CS161 Introduction to Computer Science Topic #8.
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.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 9 Repetition.
Data Types and Expressions
Bill Tucker Austin Community College COSC 1315
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 5: Loops and Files.
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Bools & Ifs.
Chapter 5 Repetition.
Chapter 9 Control Structures.
Counting Loops.
Repetition Control Structure
do/while 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.
Repetition Statements (Loops) - 2
Presentation transcript:

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 Engine exists, it will necessarily guide the future course of science.” - Charles Babbage ( )

§Office hours today

Another way of writing this? 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 for punishment" << endl; }

Nested if statements  The inside the braces can contain any valid C++ statements, including if statements! // … some other code here char answer; if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance? "; cin >> answer; if (answer == 'y') cout<< "Your balance is "<<balance<< endl; } else { cout << "Here is your money." << endl; } cout << "Good bye." << endl;

Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; //bad style- no indentation (code with proper indentation on next slide)

Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; /*else statement always refers to nearest if statement that is within same block as else and not already associated with another else*/

Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else //Error cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

Nested if statements // what is output here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z){ cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; } else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0; }

§Output is 4

SELF TEST // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

SELF TEST // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; cout y) block. Try this code for x=10, y=2, z=3, p=13 and also for x=10, y=2, z=13, p=3 */ } else cout<<5<<endl;//statement5; return 0;}

char alarm; int fuel_quantity, temperature, pressure; cout<<"Enter alarm fuel_quantity temperature pressure\n"; cin>> alarm >> fuel_quantity >> temperature>> pressure; if (alarm=='y') { if(fuel_quantity <10){ cout<<"Add more fuel\n"; } else{ if (temperature >90){ if (pressure >100) cout<<"RED ALERT! Shut down motor\n"; else cout<<"Turn on pressure valve\n"; } else { cout<<"Check temperature again after 10 minutes\n"; } else { cout<<"No problem with motor\n"; }

§Loops of various sorts are used to repeat a set of statements some number of times.

§Print numbers from 0 to 1000

int count_down=3; while (count_down > 0) { cout << "Hello "; count_down -= 1; }

int count_down=3; while (count_down > 0) { cout << "Hello "; //count_down -= 1; } What happens now?

int x = 10; while ( x > 0) { cout << x << endl; x = x – 3; }

int x = 10; while (x > 0) { cout << x << endl; x = x – 3; } Output using the comparison x 0?

What happens here? int x = 1; while (x != 12) { cout << x << endl; x = x + 2; }

Print the odd numbers less than 12 int x = 1; while (x != 12) { cout << x << endl; x = x + 2; } How to fix it?

While loops What's wrong with this? int x = 10; while ( x > 0 ); { cout << x << endl; x--; }

While-loop and for-loop

int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; }

The for loop for (initialization; expression; increment) { //statements here } Example: flowchart

int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; }

int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; } for (x=1; x<12; x=x+2) { cout<<x<<endl; }

int i ; for( i=0; i<10; ++i ) { cout << i << " "; }

int i; for( i=0; i<10; ++i ) { cout << i << " " ; } int i; for (i=0; i<10; i++) { cout <<i << " " ; }

int x, n=100; for (x=0; x<n; x++){ x=x+1; } cout<<"x after end of loop is "<<x<<endl;

int x, n=100; for (x=0; x<n; x++){ x=x+1; } cout<<"x after end of loop is "<<x<<endl; x is the smallest even number >= n

SELF TEST int x, n=100; for (x=0; x<n; x++){ cout<<x<<endl; x=x+1; } cout<<"x after end of loop is "<<x<<endl;

SELF TEST int x, n=100; for (x=0; x<n; x++){ x=x+1; cout<<x<<endl; } cout<<"x after end of loop is "<<x<<endl;

The For Loop int i ; for( i=23; i>=30; ++i ) { cout << i << " "; } for (i=13; i>=10; --i) { cout <<i <<" "; }

The For Loop What is wrong here? int i, j, k ; for( i=0; i<10; i-- ) { cout<<i; } for (i=0; i<10; ) { j=i+30; k=j+30; }

Fibonacci numbers F1=1 F2=2 Fn=Fn-1 + Fn …