Statements and flow control

Slides:



Advertisements
Similar presentations
Revision.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
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.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Chapter 6 Control Structures.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
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.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
High-Level Programming Languages: C++
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
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.
Previously Repetition Structures While, Do-While, For.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
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.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Chapter 4 Repetition Structures
The Ohio State University
Introduction to Computer Programming
Review 1.
REPETITION CONTROL STRUCTURE
C++ Programming: CS150 For.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Engineering Problem Solving with C++, Etter/Ingber
CS1010 Programming Methodology
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.
Chapter 4 Loops Case Studies
לולאות קרן כליף.
Function Basics.
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.
Compound Assignment Operators in C++
Introduction to Programming
Conditional Construct
Starting Out with C++: From Control Structures through Objects
Code::Block vs Visual C++
Summary Two basic concepts: variables and assignments Basic types:
Looping III (do … while statement)
Let’s all Repeat Together
CS1201: Programming Language 2
Arrays of Two-Dimensions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
do/while Selection Structure
Presentation transcript:

Statements and flow control Iteration statements (loops) We have already seen most of these: While loops: void ouch(int n) { int x = 1; while (x <= n) { printf("n = %d x = %d\n",n,x); printf("Ouch! Stop it!!\n"); x++; } } We could have also written this command as: void ouch(int n) { int x = 1; do { printf("n = %d x = %d\n",n,x); printf("Ouch! Stop it!!\n"); x++; } while (x <= n); } What’s the difference???

Statements and flow control The while command is an entry-condition loop; the do-while command is an exit-condition loop. For loops: int expo(int b, int p) { int result = 1, passno; for (passno = 1; passno <= p; passno++) result = result * b; return (result); } Nested For loops: #include "stdafx.h" void main() { int i, j, pp2array[4][2] = {{2,4},{3,9},{5,25},{7,49}}; for (i=0; i<4; i++) for (j=0; j<2; j++) printf("%d ", pp2array[i][j]); printf("\n"); }

Statements and flow control The output of the nested loop should appear as:

Statements and flow control If - Else Statements Consider a simple if statement: if(1 == 1) { //Stuff to do if the condition is true (which in this case, it always is! 1 is always 1!) } If else statement (enter, compile and run this code): int main() { int input = 1; char instring[5]; while (input != 0) { cout << "\nEnter an integer (enter 0 to quit): "; cin >> instring; input = atoi(instring); if (input == 0) return(1); else if (input % 2 == 0) cout << endl <<"The integer is even"<< endl; else if (input % 2 == 1) cout << endl <<"The integer is odd"<< endl; }

Statements and flow control The output should appear as:

Statements and flow control Rewrite (compile and Execute) your program so it appears as: #include "stdafx.h" #include <iostream> using namespace std; int main() { int input = 1, modinput; char instring[5]; while (input != 0) { cout << "\nEnter an integer (enter 0 to quit): "; cin >> instring; input = atoi(instring); if (input == 0) return 0; modinput = input % 2; switch(modinput) { case 0: cout << "The Number is even" << endl; break; case 1: cout << "The Number is odd" << endl; break; }