Computer Programming: C++

Similar documents
Computer Programming: C++

Introduction. C provides two styles of flow control:

Computer Programming: C++

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

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

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

LECTURE 5 Control Structures Part 2

Looping statement While loop

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

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

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

ENGR 1181 MATLAB 09: For Loops 2

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

DELHI PUBLIC SCHOOL TAPI

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Selection Statement ( if )

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Why Is Repetition Needed?

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

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

Computer Science II TURBO PASCAL

Programming for Experimental Research. Flow Control

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Chapter 4: Control structures. Repetition

Dept. of CSE, IIT KGP

Chapter 4: Control structures

OER on Loops in C Programming

Module 4: Decision-making and forming loops

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Decision Making -Branching. Class Incharge: S. Sasirekha

Chapter 7: Programming in MATLAB

n Group of statements that are executed repeatedly while some condition remains true

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Computer Programming : C++

Decision Making in C

EE 121. Office suite. Lecture 3: Introduction to programming and C programming language

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Eng. Mohammed S. Abdualal

8. Control statements

Theory of control structures

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

Computer Programming I - Unit 5 Lecture page 1 of 14

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

Arrays. Eng. Mohammed Abdualal

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Flow Control. CSC215 Lecture

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

In this chapter you will learn:

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Quiz Determine the output of the following program:

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

SELECTION. (Chapter 2)

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

REPETITION CONTROL STRUCTURE LOGO

EP241 Computing Programming

3 The L oop Control Structure

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Programming in C++ PART 2

DECISION CONTROL AND LOOPING STATEMENTS

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Chapter 5: Control Structures

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Lecture 6. Statements

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

INTRODUCTION TO COMPUTER SCIENCE - LAB

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

CS201 Some Important Definitions

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 12

Computers Programming Course 7. Iulian Năstac

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

CMPT 102 Introduction to Scientific Computer Programming

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

.Net Technologies. Components of.net Framework

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

do { statements } while (condition);

Repetition Structures

6. Control Statements II

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

Structured Program Development

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Quick and simple ClassPad programs for solving problems such as those posed by Project Euler.

Prepared by: Shraddha Modi

Loops and Files. of do-while loop

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Chapter 5: Control Structures II (Repetition)

Control Structures of C++ Programming (2)

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Transcription:

The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #4 Loops Part II

Contents Loop Control Statement Nested loop Loop Control Statement Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. C++ supports the following control statements. Sr.No Control Statement & Description 1 break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. continue statement 2 Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. 3 goto statement Transfers control to the labeled statement. Though it is not advised to use goto statement in your program. 2

break Statement The break; statement terminates a loop (for, while and do..while loop) and a switch statement immediately when it appears. Syntax break; In real practice, break statement is almost always used inside the body of conditional statement (if...else) inside the loop. How break statement works? 3

EX1: C++ program to add all number entered by user until user enters 0. In the above program, the test expression is always true. The user is asked to enter a number which is stored in the variable number. If the user enters any number other than 0, the number is added to sum and stored to it. Again, the user is asked to enter another number. When user enters 0, the test expression inside if statement is false and body of else is executed which terminates the loop. Finally, the sum is displayed. 4

continue Statement It is sometimes necessary to skip a certain test condition within a loop. In such case, continue; statement is used in C++ programming. Syntax continue; In practice, continue; statement is almost always used inside a conditional statement. How continue statement works? 5

EX2: Write program to display integer from 1 to 10 except 6 and 9.. In above program, when i is 6 or 9, execution of statement cout << i << "\t"; is skipped inside the loop using continue; statement. 6

goto Statement goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program. Syntax goto label;........................ label: statement;........ in the syntax above, label is an identifier. When goto label; is encountered, the control of program jumps to label: and executes the code below it. 7

How continue statement works? EX3: Write program calculates the average of numbers entered by user If user enters negative number, it ignores the number and calculates the average of number entered before it... 8

You can write any C++ program without the use of goto statement and is generally considered a good idea not to use them (:. 9

Nested Loop A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting. Syntax The syntax for a nested for loop statement in C++ is as follows for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); // you can put more statements. The syntax for a nested while loop statement in C++ is as follows while(condition) { while(condition) { statement(s); } statement(s); // you can put more statements. } The syntax for a nested do...while loop statement in C++ is as follows do { statement(s); // you can put more statements. do { statement(s); } while( condition ); } while( condition ); 10

EX4: Write program uses a nested for loop to find the prime numbers from 2 to 100 11

Lab Work: 1- Write program to Check Armstrong Number or not ; Note: A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself. For example: 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 // 153 is an Armstrong number. 12 is not equal to 1 * 1 * 1 + 2 * 2 * 2 // 12 is not an Armstrong number. 2- Write program to print half pyramid using * Note: User will entered the number of row * * * * * * * * * * * * * * * 12

Homework 1- Write program to print pyramid using loop Note: user will entered the number of row 2- Write program to display prime numbers between two intervals Output Enter two numbers(intervals): 20 50 Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47 Good Luck 13