Programming in C++ PART 2

Similar documents
Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Programming in OOP/C++

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Computer Programming: C++

LECTURE 5 Control Structures Part 2

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Lecture Transcript While and Do While Statements in C++

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

COMPUTER SKILLS LECTURE 07

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

REPETITION CONTROL STRUCTURE LOGO

CpSc 1111 Lab 4 Formatting and Flow Control

APCS Semester #1 Final Exam Practice Problems

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Unit 7. 'while' Loops

CpSc 1111 Lab 5 Formatting and Flow Control

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

More About WHILE Loops

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

C++ Programming: From Problem Analysis to Program Design, Third Edition

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Computer Programming

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

What is PHP? [1] Figure 1 [1]

Variables and Functions. ROBOTC Software

Computer Programming

EP241 Computing Programming

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Computer Programming: C++

Python: common syntax

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Computer Programming : C++

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

PIC 10A. Review for Midterm I

C++ DATA TYPES BASIC CONTROL FLOW

CSE 20. Lecture 4: Number System and Boolean Function. CSE 20: Lecture2

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

CSE 115. Introduction to Computer Science I

More Complex Versions of the if Statement. Class 13

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CS11 Java. Fall Lecture 1

Reviewing all Topics this term

CS1 Lecture 3 Jan. 18, 2019

Lab # 02. Basic Elements of C++ _ Part1

CS 251 Intermediate Programming Java Basics

Fundamentals of Programming CS-110. Lecture 2

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

CS1 Lecture 3 Jan. 22, 2018

Python for loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 7

Intro. Scheme Basics. scm> 5 5. scm>

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Introduction. C provides two styles of flow control:

Sixth lecture; classes, objects, reference operator.

C++ Reference NYU Digital Electronics Lab Fall 2016

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

CS 231 Data Structures and Algorithms, Fall 2016

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Information Science 1

Computational Expression

(Refer Slide Time: 01.26)

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS 177 Week 15 Recitation Slides. Review

Flow Control 10. Flow Control. Content. if Statement. Conditional Statements. if Statement. Example. if, if/else, switch for, while, do/while

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

C++ Support Classes (Data and Variables)

Chapter 2. The Algorithmic Foundations of. Computer Science INVITATION TO. Computer Science. Tuesday, September 10, 13

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Problem Solving for Intro to Computer Science

EP578 Computing for Physicists

EEE130 Digital Electronics I Lecture #4_1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Lecture 6: While Loops and the Math Class

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

All answers will be posted on web site, and most will be reviewed in class.

CS 106 Introduction to Computer Science I

Why Is Repetition Needed?

Add Subtract Multiply Divide

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Intro to Python & Programming. C-START Python PD Workshop

introjs.notebook March 02, 2014

Transcription:

Lecture 07-2 Programming in C++ PART 2 By Assistant Professor Dr. Ali Kattan 1

The while Loop and do..while loop In the previous lecture we studied the for Loop in C++. In this lecture we will cover iteration using while and do..while loops. Remember, iteration means repeating some code until some condition terminate the loop. 2

The while statement The while statement in C++ has the following simple form: It means in English: while the condition is true, do the statement. The loop will stop only when the condition is false. Notice that if the condition is not true, the loop will never run. If you have more than one statement, then use curly braces: { } 3

The while loop: Simple example The following is a simple while loop example to print numbers from 1 to 5. The while statement checks the condition BEFORE going inside the body of the loop. 4

The while loop: Simple example Notice the flowchart for while. The condition check is at the beginning before loop statements. 5

The while loop: More useful example The following program is used to sum positive numbers. It will keep asking the user to enter a new number until the user enters a negative number to stop the program. 6

The while loop: More useful example 2 The following program is used print powers of 10 (10, 100, 1000..) Notice the line power *=10; You can replace this line with power = power + 10; However, it is shorter to type the one in the program. 7

The while loop: More useful example 3 In the following program, the while condition is actually a cin input statement! You should know that if cin successfully enters an integer then this is regarded as true, otherwise false. 8

The while loop: More useful example 3 Notice that if you keep entering integer numbers the while condition will be true. If you enter a character (like Q), program stops. 9

The while loop: More useful example 4 In the following program, the while condition is a check for a Boolean value ok. As long as ok is true, the loop will continue. The program counts and ask the user each time. 10

The while loop: More useful example 5 The following program is similar to the previous but it checks for the (not) condition. 11

The do..while statement The do..while statement in C++ has the following simple form: It means in English: do the statement, then check the condition and if true do the statement again. The loop will stop only when the condition is false. Notice that the loop will run at least once even if the condition is false. 12

The do..while statement If you have more than one statement, then use curly braces: { } Basically, do..while is similar to while but the condition is checked at the end of the loop, not at the beginning. Because of this, the loop body in do..while runs at least once and this is unlike while loop since the loop body may never run. 13

The while and do..while statement See the difference between the flowchart for while and that for the do..while statement. In while, the condition check is at the beginning. In do..while, the condition check is at the end. 14

The do..while loop: Simple example The do..while statement checks the condition AFTER going inside the body of the loop. Try this: compare this program with the code in slide 4. Change line 5 so that k = 6 and see the difference in output. 15

Compare do..while & while loop How many times the word hello will be printed by each program? If we change p = 0 in line 3, how many times the word hello will be printed by each program? 16

Homework (for LAB) Problem 1: Write a program using while loop to print the numbers that can be divided by 3 or 7 starting from 40 down to 5. Problem 2: Same program in problem 1 but using do..while loop. Problem 3: Same program in problem 1 but using for loop. 17

Remember: If you don t practice C++ programming on the computer, YOU WILL NOT LEARN anything. To become a professional programmer you must try all the examples by yourself. 18

Thank You My Office, 3 rd floor, 303 Email: ali.kattan@nobleinst.com Website: www.alikattan.org 19