Lecture 01b: C++ review Topics: if's and switch's while and for loops.

Slides:



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

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 4 Selection Structures: Making Decisions.
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
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.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CPS120: Introduction to Computer Science Decision Making in Programs.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements I
Loop Control Structure.
Looping and Repetition
Iteration with While You can say that again.
Chapter 4: Control Structures I (Selection)
Decisions, decisions, decisions
Repetition Statements (Loops) - 2
Decision making and control functions
Presentation transcript:

Lecture 01b: C++ review Topics: if's and switch's while and for loops

Part I: Conditionals A means to select (possibly) one of (possibly) many alternative code blocks. – A single statement – Or a block of code surrounded by curly braces If / else structures use a conditional (boolean expression) Switch's are more limited and can only compare the contents of a single (numeric) variable. – They offer no speed advantage – They are uglier than if/else's

if "0 of 1" version if (cond) code-block "1 of n" version if (cond1) code-block1 else if (cond2) code-block2 … else code-blockN "0-1 of n" version Like "1 of n" but without the else "n of m" version if (cond1) code-block1 if (cond2) code-block2 …

Conditions Simple x < 5 y != 3 health == 100.0f// Be careful w/ float's Compound x < 5 && y != 3 !strcmp(s, "ABC") && age < 65

(Ugly) switch statements int x; // x is filled in elsewhere... switch(x) { case 5: code// no need for braces break; case -1: code// note: no break; "fall-through" case -2: code break; default: code// Executed if none of the others break; }

Pitfalls A condition can be a boolean or… – A numeric value (0 = false, non-zero = true) int x; // x is modified if (x) // will be executed if x != 0 { /* … */ } Assignment statements – Are allowable, and are sometimes useful – But…can be an error if you meant to do comparison (==) if (x = 15) {} // Changes x and block is always executed Not enclosing a block in curly braces if (health < 15) speed = 1.2f; activateRage(); // Always executed, regard. of health

Part II: Repetition A way to repeat a code-block (possibly) multiple times 3 variants: – while: executed 0-n times – do-while: executed 1-n times – for: shorthand for common while loops

While int i = 5, x=0; while (i < n && x < 100) { // Do something // Get set up for next iteration i += 2; x += i * i; }

for // Same effect as last slide for (int i = 5, x = 0; i < n && x < 100; i+= 2, x += i * i) { // Do something }

Do-While int choice; do { cout << "Enter choice (9=quit): "; cin >> choice; // Do something else } while (choice != 9);

Break and continue Allow you to modify a loop’s behavior break; // End current iteration + leave loop continue; // End current iteration Technically, neither is necessary – It can simplify the code a bit, though. [Intentionally infinite loops] [Example: total numbers until user enters a negative]

Mini-Lab [FizzBuzz] For points or just for “fun”?