Review 1.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer Science 1620 Loops.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Chapter 5: Loops and Files.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Chapter 5: Control Structures II (Repetition)
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Chapter 4 Program Control Statements
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Chapter 9 Additional Control Structures Dale/Weems.
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.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 2 Flow of Control Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University
Bill Tucker Austin Community College COSC 1315
Chapter 4 Repetition Structures
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The switch Statement, and Introduction to Looping
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.
C++ Programming: CS150 For.
Branching Constructs Review
Control Structures II (Repetition)
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.
Lecture 2-3 C++ Basic Constructs
User-Defined Functions
Lecture 4B More Repetition Richard Gesick
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.
Introduction to Functions
Conditinoal Constructs Review
Additional Control Structures
Arrays Kingdom of Saudi Arabia
Conditinoal Constructs Review
Chapter 3 Even More Flow of Control 1
Control Statements Loops.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Looping III (do … while statement)
2.6 The if/else Selection Structure
Statements and flow control
Arrays Arrays A few types Structures of related data items
Control Statements Paritosh Srivastava.
Control Statements Loops.
Chapter 4 Repetition Structures
Presentation transcript:

Review 1

Structure of a C++ program include necessary header files every C++ program should have a main function as the entrance! the body of the function enclosed by “{ }” How many stuff can we put under the main function??

Is there any issue of the following C++ code? void main() { cout << “No, I can be compiled and run! ; }

Is there any issue of the following C++ code? #include <iostream> using namespace std; missing a header file void main() { cout << “No, I can be compiled and run! ; } Is a header file always needed ? Is the header file always be #include <iostream> ?

Does the following code need a header file? void main() { } Is a header file always needed ? Is the header file always be #include <iostream> ?

Does the following code need a header file? void main() { } Is a header file always needed ? Yes and no! An empty main() does not need a header. But all meaningful programs do not have empty body! Is the header file always be #include <iostream> ? No. depending on what you need from the C++ library

Is there any issue of the following C++ code? #include <iostream> using namespace std; void main() { cout << “No, I can be compiled and run! ; }

Is there any issue of the following C++ code? #include <iostream> using namespace std; void main() { cout << “No, I can be compiled and run!” ; } the string should be put in “ ”!! How to accept the user input from keyboard? Can the user input other information than just numbers?

Is there any issue of the following C++ code? #include <iostream> using namespace std; void main() { cout << “No, I can be compiled and run!” ; } the string should be put in “ ”!! How to accept the user input from keyboard? cin >> [variable]; Can the user input other information than just numbers? Yes!

Is there any issue of the following C++ code? #include <iostream> #include <string> using namespace std; int main() { string name; cout << “Please input your name! \n” ; cin >> name; cout << “Hello, “ << name << endl; return 0; } the string should be put in “ ”!! How to accept the user input from keyboard? cin >> [variable]; Can the user input other information than just numbers? Yes!

if-else Statement Syntax Formal syntax: if (<boolean_expression>) <yes_statement> else <no_statement> Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement

Multiway if-else Not new, just different indenting Avoids "excessive" indenting Syntax: Copyright © 2016 Pearson Inc. All rights reserved.

What will be the output of this program? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; else { cout << "value of x unknown";

How about now? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; if (x == 1) { cout << "x is 1"; }

How about now? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; cin >> x; if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; else { cout << "value of x unknown";

Other examples…

switch Statement Syntax The controlling expression must be integral! This includes char. Copyright © 2016 Pearson Inc. All rights reserved.

The switch Statement in Action Copyright © 2016 Pearson Inc. All rights reserved.

Can you rewrite the following code using the switch statement? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; cin >> x; if (x == -1) { cout << "x cannot be negative!"; } else if (x == 0) cout << "Draw red."; else if (x == 1) cout << "Draw blue."; else if (x == 2) { cout << "Draw green"; else { cout << "value of x unknown";

#include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; cin >> x; switch(x) case -1: cout << "x cannot be negative!"; break; case 0: cout << "Draw red."; case 1: cout << "Draw blue."; case 2: cout << "Draw green."; default: cout << "value of x unknown"; }

What will be the output of the following code? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; switch(x) case 0: cout << "Draw red."; case -1: cout << "x cannot be negative!"; break; case 1: cout << "Draw blue."; case 2: cout << "Draw green."; default: cout << "value of x unknown"; }

What will be the output of the following code? #include <iostream> using namespace std; int main() { int x = 2; cout << "Please enter x: \n"; switch(x) case 0: case 1: cout << "Draw blue."; break; case 2: cout << "Draw green."; default: cout << "value of x unknown"; }

switch Menu Example Switch stmt "perfect" for menus: switch (response) { case 1: // Execute menu option 1 break; case 2: // Execute menu option 2 break; case 3: // Execute menu option 3 break; default: cout << "Please enter valid response."; }

Loops Why do we need loops?

Loops 3 Types of loops in C++ while do-while for Most flexible No "restrictions" do-while Least flexible Always executes loop body at least once for Natural "counting" loop

for Loop Syntax for (Init_Action; Bool_Exp; Update_Action) Body_Statement Like if-else, Body_Statement can be a block statement Much more typical Good for the situations that the exact or maximum number of iterations is known!!! Example: compute the sum of numbers 1,2, 3,…, 100

#include <iostream> using namespace std; int main() { int x = 0; int i; for (i=1; i<=100; i++) x += i; } cout << "the sum of 1 to 100 is " << x << endl;

while Loops Syntax

while Loop Example Consider: count = 0; // Initialization while (count < 3) // Loop Condition { cout << "Hi "; // Loop Body count++; // Update expression } Loop body executes how many times?

do-while Loop Syntax

do-while Loop Example count = 0; // Initialization do { cout << "Hi "; // Loop Body count++; // Update expression } while (count < 3); // Loop Condition Loop body executes how many times? do-while loops always execute body at least once!

#include <iostream> using namespace std; int main() { int x = 0; int i; for (i=1; i<=100; i++) x += i; } cout << "the sum of 1 to 100 is " << x << endl; Can you implement the above for loop example using while or do-while mechanism?

The break and continue Statements Flow of Control Recall how loops provide "graceful" and clear flow of control in and out In RARE instances, can alter natural flow break; Forces loop to exit immediately. continue; Skips rest of loop body These statements violate natural flow Only used when absolutely necessary!

Nested Loops Recall: ANY valid C++ statements can be inside body of loop This includes additional loop statements! Called "nested loops" Requires careful indenting: for (outer=0; outer<5; outer++) for (inner=7; inner>2; inner--) cout << outer << inner; Notice no { } since each body is one statement Good style dictates we use { } anyway

Arrays Why do we need arrays?

Declaring Arrays Declare the array  allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to size - 1

Accessing Arrays Access using index/subscript cout << score[3]; Note two uses of brackets: In declaration, specifies SIZE of array Anywhere else, specifies a subscript Size, subscript need not be literal int score[MAX_SCORES]; score[n+1] = 99; If n is 2, identical to: score[3]

Array Usage Powerful storage mechanism Can issue command like: "Do this to ith indexed variable" where i is computed by program "Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score"