CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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.
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.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
1 Chapter Four Boolean Expressions and Control Statements.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Overview Go over parts of quiz? Another iteration structure for loop.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Control Statements Eric Roberts CS 106A January 15, 2016.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 3: Decisions and Loops
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 8 Repetition Statements
For & do/while Loops.
3 Control Statements:.
Control Structures Part 2
Chapter 4: Control Structures I (Selection)
ICT Programming Lesson 3:
2.6 The if/else Selection Structure
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

CSCI 161 Lecture 7 Martin van Bommel

Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May want to repeat statements - iteration May or may not want particular statement executed at all - conditional

Control Statement Structure Control line - first line (e.g. while) Body - statements enclosed in { } that are repeated Body typically indented to show range of control statement

Conditional Execution May want to execute a statement only if some condition applies May wish to choose between two alternative courses of actions depending on some test Simplest form - if statement

if Statement if ( conditional-test ) { … statements executed if test true... } } else { … statements executed if test false... }

Relational & Logical Operators ==Equal !=Not equal >Greater <Less >=Greater or equal <=Less or equal Logical operate on results of comparisons !Not (true if operand false – one operand) && And ( true if both true ) || Or ( true if either true )

Example 1 “ x is not equal to either 2 or 3” if (x != 2 || x != 3) No, it should be if (!(x == 2 || x == 3)) or if (x != 2 && x != 3)

Example 2 “x is in the range from 0 to 10 exclusive” if (0 < x < 10) No, it should be if (0 < x && x < 10)

Example Leap year every fourth year, except centuries, then just every fourth century –year is divisible by 4 but not by 100, or –year is divisible by 400 Try ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)

Bad Example if (x < 5) cout << ”A”; if (x > 5) cout << ”B”; else cout << ”C”;

If Blocking Rule For any if statement that requires (1) more than a single line, or (2) an else clause, always use braces to enclose in a separate block the statements under the control of the if statement.

Single-line if statement if ( condition ) statement where: condition is the Boolean value to test statement is a single statement to be executed if condition is true

Multiline if statement if ( condition ) { statements; } where condition is the Boolean value to test statements is a block of statements to be executed if condition is true

if-else statements if ( condition ) { statements T ; } else { statements F ; }

Cascading if statement if ( condition 1 ) { statements 1 ; } else if ( condition 2 ) { statements 2 ;... } else { statements none }

Iteration and Loops Iteration - process of repeating an operation Essential for large amounts of data - repeating statements shortens programs Loop - any portion of program repeated via control statement e.g. while loop

The while statement while ( condition ) { statements } where condition tested before first and every iteration statements executed each time condition true

while operation Condition test performed before every cycle, including first. If condition is initially false, body of loop not executed at all. Condition test performed only at beginning of each loop cycle. Even if condition becomes false during cycle, cycle is completed.

while example Sum up digits of a number sum = 0; while (n > 0) { sum = sum + n % 10; n = n / 10; }

Infinite loops A loop cycle that never stops – infinite loop Condition of while statement never false e.g. while (n >= 0) { sum += n % 10; n /= 10; }

Repeat-N-Times Idiom for (i = 0; i < N ; i++) { statements to be repeated } May want to add up 10 input numbers for (i = 0; i > value; sum = sum + value; }

Index Variable in Counting Loop for (i = 0; i < N ; i++) Variable i is called the index variable Any variable may be used - traditionally i Must be declared at beginning of function Initial value of i in loop is 0 - first cycle Next cycles value is 1, 2, etc. On last cycle, value is N – 1 After loop, value of i is N

Non-Zero Counting Loop It is possible to modify for loop to begin at another number for (i = first ; i <= last ; i++) Note the <= operator instead of < Value of i ranges from first to last Used if value of i required in body of loop

for statement for ( init ; test ; step ) { statements } where init is an expression evaluated at beginning test is a condition for continuing the loop step is an expression to prepare next cycle

for example Countdown from 10 to 0 for (t=10; t >= 0; t--) { cout << t; } cout << ”Liftoff!\n”;

for more Note that expressions init, test, and step are optional, but the semicolons must appear for (;;) - infinite loop for (;t<10;) same as while (t<10)

Nested for loops Create a 10 x 10 multiplication table for (row=1; row<=10; row++) { for (col=1; col<=10; col++) { cout << (row * col); } cout << endl; }