CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

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.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
CS31: Introduction to Computer Science I Discussion 1A 4/23/2010 Sungwon Yang
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 05 (Part III) Control Statements: Part II.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Overview Go over parts of quiz? Another iteration structure for loop.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
1 CS161 Introduction to Computer Science Topic #8.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
Bill Tucker Austin Community College COSC 1315
Introduction to Computer Programming
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Introduction to C++ October 2, 2017.
JavaScript: Control Statements I
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
3 Control Statements:.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
CS150 Introduction to Computer Science 1
switch Selection Structure
C Programming Getting started Variables Basic C operators Conditionals
2.6 The if/else Selection Structure
Chapter 2 Programming Basics.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Fundamental Programming
COMS 261 Computer Science I
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang

Quick Review What did we learn last week? – Arithmetic Expressions Addition, Subtraction, Multiplication, Division, Modulo Evaluation precedence: left -> right, parenthesis Abbreviation: +=, -=, *=, /=, %= Increment & decrement: ++, -- – Boolean Expressions bool type: true or false >, =, <=, ==, !=, &&, || – IF statement Conditional statements – IF - ELSE statement Can choose one between two alternatives – IF - ELSE IF – ELSE statement Can choose one among multiple alternatives – Nested IF statement Embed IF statements in IF statement – String variables variable for “text” getline (cin, variable); cin.ignore(100000, '\n'); – Variables vs. Constants

switch statement Choose one among multiple choices switch (Controlling Expression) { case Constant_1: statement_1; … break; case Constant_2: statement_2; … break; default: statement_default; } cin >> grade; switch (grade) { case 1: cout << “Freshmen” <<endl; break; case 2: cout << “Sophomore” << endl; break; case 3: cout << “Junior” << endl; break; case 4: cout << “Senior” << endl; break; default: cout << “Wrong Input” << endl; }

WHILE Loops Repeat statements while condition holds while (Condition) { statements; … } int yourInput = 0; cin >> yourInput; while (yourInput > 0) { cout << yourInput << endl; yourInput--; } cout << “The End” << endl; The End

DO-WHILE Loops Repeat statement while condition holds – Execute the statements once then test condition do { statements; … } while (Condition); int yourInput = 0; cin >> yourInput; do { cout << yourInput << endl; yourInput--; } while (yourInput > 0); cout << “The End” << endl; The End

while vs. do-while int main() { int yourInput = 0, sum = 0; cin >> yourInput; while (yourInput > 0) { yourInput--; sum += yourInput; } cout << sum << endl; } int main() { int yourInput = 0, sum = 0; cin >> yourInput; do { yourInput--; sum += yourInput; } while (yourInput > 0); cout << sum << endl; }

Infinite loops Your loop will never end – Make sure that it ends at some point int main () { int x = 0, y = 0, sum = 0; while ( sum != 100 ) { sum = x + y; x += 1; y += 2; cout << sum << endl; } int main () { int x = 0, y = 0, sum = 0; while ( sum < 100 ) { sum = x + y; x += 1; y += 2; cout << sum << endl; }

FOR Loops for ( initialization ; condition ; update ) { statements; … } The order of execution 1.Initialization 2.Check the condition If true 3. Run the body (statements) 4. Do the update Go back to 2 If false Exit the for loop

Initialization in for loop Can declare initialization variable inside for loop – It is different from variables declared elsewhere int i = 0; for ( i = 0 ; i < 5 ; i++ ) { cout << i << endl; } cout << i << endl; int i = 0; for (int i = 0 ; i < 5 ; i++ ) { cout << i << end; } cout << i << endl;

Nested loop Embed loops into loops int main() { for (int i=0 ; i < 10 ; i++) { cout << i << ':'; for (int j=0 ; j < 10 ; j++) { cout << j << ','; } cout << endl; } 0:0,1,2,3,4,5,6,7,8,9, 1:0,1,2,3,4,5,6,7,8,9, 2:0,1,2,3,4,5,6,7,8,9, 3:0,1,2,3,4,5,6,7,8,9, 4:0,1,2,3,4,5,6,7,8,9, 5:0,1,2,3,4,5,6,7,8,9, 6:0,1,2,3,4,5,6,7,8,9, 7:0,1,2,3,4,5,6,7,8,9, 8:0,1,2,3,4,5,6,7,8,9, 9:0,1,2,3,4,5,6,7,8,9,

FOR to WHILE Conversion while loop for loop for ( initialization ; condition ; update ) { statements; … } initialization While ( condition ) { statements; … update }

Quick question int main() { int result = 0; for ( int i = 10; i >= 0; i--) { result += i; } cout << result << endl; } 55 int main() { int result = 0; for ( int i = 32; i > 1; i /= 2) { result += i; } cout << result << endl; } 62

Functions Sub-programs in your program Simplify multiple operations which are basically same Calculator Addition Subtraction Multiplication Division Your Program Functions

3 operations, but basically same job int main() { int result = 0; for ( int i = 1; i <= 10; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 50; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 100; i++) { result += i; } cout << result << endl; } int main() { int result = 0; result = sum ( 1, 10 ); cout << result << endl; result = 0; result = sum ( 1, 50 ); cout << result << endl; result = 0; result = sum ( 1, 100 ); cout << result << endl; }

Functions Define a function return_type function_name (argument_type_1 argument_name_1, argument_type_2 argumrnt_name_2, … ) { function_statements; … return return_value; } int sum (int begin, int end ) { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result;; }

Function definition int main() { int result = 0; for ( int i = 1; i <= 10; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 50; i++) { result += i; } cout << result << endl; result = 0; for ( int i = 1; i <= 100; i++) { result += i; } cout << result << endl; } int sum (int begin, int end ) { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result; } int main() { int result = 0; cout << sum(1,10) << endl; result = sum(1,50); cout << result << endl; result = 0; cout << sum(1,100) << endl; }

Function declaration int sum (int begin, int end ) { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result;; } int main() { int result = 0; cout << sum(1,10) << endl; result = sum(1,50); cout << result << endl; result = 0; cout << sum(1,100) << endl; } int sum (int begin, int end); //proto type int main() { int result = 0; cout << sum(1,10) << endl; result = sum(1,50); cout << result << endl; result = 0; cout << sum(1,100) << endl; } int sum (int begin, int end ) //definition { int result = 0; for ( int i=begin ; i <= end ; i++ ) { result += i; } return result; }

Function without arguments Functions can have no arguments string askName () { string name; cout << "What's your name?" << endl; getline (cin, name); return name; } int main() { string name; name = askName(); cout << "Hello!" << endl; cout << name << endl; cout << "Nice to meet you!" << endl; } What’s your name? Sungwon Hello! Sungwon Nice to meet you!

Void functions Functions can have no return values void greeting (string name ) { cout << “Hello!” << endl; cout << name << endl; cout << “Nice to meet you!” << endl; } int main() { string name; cout << “What’s your name?” << endl; getline (cin, name); greeting (name); } What’s your name? Sungwon Hello! Sungwon Nice to meet you!

End point of function return ends function In the case of void function, the last statement int larger (int arg1, int arg2 ) { if ( arg1 >= arg2 ) return arg1; else return arg2; } int main() { int first = 0, second = 0; cout << “Enter first number” << endl; cin >> first; cout << “Enter second number” << endl; cin >> second; cout << larger(first,second) << “is larger” << endl; }

Functions of string (Chap 9 in textbook) OperationWhat it doesExamples string s = “Hello”; string s2 = “!!!”; Declares strings s and s2 s.length() or s.size() Returns the length of scout << s.size();  5 s.at(i)Returns i-th character i must be an interger between 0 and size-1(inclusive) cout << s.at(0);  H cout << s.at(4);  o s.empty()Returns true if s is emptyIf (s.empty()) cout << “It is empty”; s + s2Concaternates two stringscout << s + s2;  Hello!!! s.substr(i,n) s.substr(i) Takes a substring of length n, starting from the i-th character cout << s.substr(2,2);  ll cout << s.substr(2);  llo s.replace( i, n, s2 )Replace a substring of length n starting from at i with another string s2, and set s with a new string s.replace (2,2,s2); cout << s;  He!!!o

Functions of character(Chap 9 in textbook) You need to include library: #include OperationsWhat it does char c;declare a character variable c isspace(c)true if c is a white space character (space, tab, newline) isalpha(c)true if c is a letter isalnum(c)true if c is a letter or digit islower(c)true if c is a lowercase letter isupper(c)true if c is a uppercase letter tolower(c)returns the lowercase version of c, if c is a letter toupper(c)returns the uppercase version of c, if c is a letter

Quick question #include using namespace std; int main() { string s = "hello"; char c; for (int i=0 ; i< s.length() ; i++) { c = s.at(i); c = toupper(c); cout << c; } cout << endl; } HELLO

ASCII and character Everything is represented in 0s and 1s in computers – computers do not understand ‘a’, ‘B’, ‘3’,… – characters are stored as binary numbers ASCII defines the mapping between characters and integers ‘0’  48, ‘A’  65 int main() { int a = ‘A’; cout << a << endl; } 65 int main() { char a = 65; cout << a << endl; } A