COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
If Statements & Relational Operators Programming.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
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
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
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.
Basic Of Computer Science
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Previously Repetition Structures While, Do-While, For.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
1 CS161 Introduction to Computer Science Topic #8.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
COMP Loop Statements Yi Hong May 21, 2015.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
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):
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Midterm Review.
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.
A mechanism for deciding whether an action should be taken
Variables A piece of memory set aside to store data
Programming Fundamentals
Counting Loops.
Miscellaneous Flow Control
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Local, Global Variables, and Scope
Fundamental Programming
Decisions, decisions, decisions
Seoul National University
Presentation transcript:

COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo

Lecture Review Scope of variables  Every variable has living area, called scope  The variable will be killed once out of scope  Scope Starting:After the variable is declared Ending:At the end of the declared area

Lecture Review int y = 38; void fun( int, int); int main( ){ int z=47; while (z<400){ int a = 90; z += a++; z++; } y = 2 * z; fun(1, 2); return 0; } void fun(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } scope of i scope of r scope of s & t scope of a scope of z scope of y scope of fun

Exercise - 3 int A,B,C,D; void Two( int A, int B, int& D) { B = 21; D = 23; cout << A << " " << B << " " << C<< " " << D << endl; } void One( int A, int B, int& C) { int D; // Local variable A = 10; B = 11; C = 12; D = 13; cout << A << " " << B<< " " << C<< " " << D << endl; Two(A,B,C); } int main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C); cout << A << " " << B << " " << C<< " " << D << endl; Two(A,B,C); cout << A << " " <<B << " " << C << " " << D << endl; return 0; }

Answer Output ABCD in One = ABCD in Two = ABCD in Main = ABCD in Two = ABCD in Main =

Lecture Review Multiple Selection Statement value1 action 1 value2 action 2 value3 action 3 value4 action 4 multiway expression

Lecture Review switch Statement  Syntax:  The selector expression can only be  bool, integer, char, or enum constant switch ( ) { case : ; break; case : ; break; case : ; break; default : ; }

Lecture Review Note:  If break encountered Go to end of the switch statement  Otherwise continue the next execution int score=95; switch(int(score)/10){ case 10: case 9: cout << "Grade = A" << endl; case 8: cout << "Grade = B" << endl; case 7: cout << "Grade = C" << endl; case 6: cout << "Grade = D" << endl; default:cout << "Grade = F" << endl; } Output Grade = A Grade = B Grade = C Grade = D Grade = F

Lecture Review Common questions in exam  Change if-else-if statement to switch if (choice == 'b' || choice == 'B') balance+=bet(); else if (choice == 's' || choice == 'S') slot(balance, Jackpot); else if (choice == 'c' || choice == 'C') cash(balance); elsecout <<"Wrong choice"<<endl; switch (choice) { case 'b': case 'B': balance+=bet(); break; case 's': case 'S': slot(balance, Jackpot); break; case 'c': case 'C': cash(balance); break; default: cout <<"Wrong choice"<<endl; break; }

Summary By the end of this lab, you should be able to:  Identify the scope of different identifiers  Declared global/local variables/constants  Use switch statement

Announcement Mid-term  Next Friday (3 rd Nov) 6:30-8:30pm Review Session  Room 1403  Next Tue (31 st Oct) 6-7pm English  Next Wed (1 st Nov) 6-7pm Cantonese Q&A Session  Next Fri (3 rd Nov) 3-6pm

Lab7 Use functions & Scope of variable Download the template from website MUST NOT add any global variables If you cannot finish this lab on time, please schedule a time with me.  Tutorial & pre-test