Programming Fundamental

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
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 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
do - while  while: Execute the body of the loop at least once
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
ECE Application Programming
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.2 Control Structures (Iteration)
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.
Lecture 4B More Repetition Richard Gesick
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.
Programming Fundamentals Lecture #6 Program Control
TOPIC 4: REPETITION CONTROL STRUCTURE
- Additional C Statements
Outline Altering flow of control Boolean expressions
Structured Program Development in C
Chapter 4 - Program Control
Chapter 2.2 Control Structures (Iteration)
Repetition and Loop Statements
Computing Fundamentals
Alternate Version of STARTING OUT WITH C++ 4th Edition
Dale Roberts, Lecturer IUPUI
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Programming Fundamental
Programming Fundamental
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Programming Fundamental Instructor Name: Muhammad Safyan Lecture-9

Lecture outline For Loop Increment/ Decrement Operator Compound assignment statement Break statement Continue statement Nested Loop

FOR LOOP

FOR LOOP In C++, for is a reserved word. The for loop executes as follows: The initial statement executes. The loop condition is evaluated. If the loop condition evaluates to true i. Execute the for loop statement. ii. Execute the update statement (the third expression in the parentheses). Repeat Step 2 until the loop condition evaluates to false. The initial statement usually initializes a variable (called the for loop control, or for indexed, variable).

You can count backward using a for loop if the for loop control expressions are set correctly. For example, consider the following for loop:

You can increment (or decrement) the loop control variable by any fixed number. In the following for loop, the variable is initialized to 1; at the end of the for loop, i is incremented by 2. This for loop outputs the first 10 positive odd integers.

2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 : 2 x 10 = 20 Sample Program Write a program to print the table of 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 : 2 x 10 = 20

Increment Operator ++ counter ++ ; same as counter = counter + 1;

Decrement operator -- counter -- ; same as counter = counter - 1

Increment/Decrement Operator j++ ++j

Increment Operator ++ counter ++ ; same as counter = counter + 1;

Decrement operator -- counter -- ; same as counter = counter - 1

Increment/Decrement Operator Increment Operator can be written with following two ways: Pre increment ++j Post increment j++ Decrement Operator can be written with following two ways: Pre decrement - -j Post decrement j- -

Increment/Decrement Operator If using one variable with Increment/decrement Operator , does not matter. It does matter when the variable used in an expression where it evaluated to assign the value to other variable. If used pre increment (++j), the value of j is increased by 1 , new value is used in an expression e.g. If j==5 X=++j;

Increment/Decrement Operator If used post increment(j++) the vlaue of j is used first in an expression than is increased by by 1. If j = 5, and we write the expression x = j++ ;

Increment/Decrement Operator x=5; x++; ++x Cout<<“x=“<<x j=7; Cout<<“j=“<<j++; Cout<<“j=“<<j; Cout<<“j=“<<++j; Increment and decrement operator can be used in repetitive structure e.g. FOR LOOP

Compound Assignment Operators

Compound Assignment Opreator += counter += 3 ; same as counter = counter + 3 ;

-= counter -= 5 ; same as counter = counter – 5 ;

*= x*=2 x = x * 2

%= x %= 2 ; same as x = x % 2 ;

Sample Program (Assignment Operator) find the sum of the squares of the integers from 1 to n. Where n is a positive value entered by the user (i.e. Sum = 1 2 + 2 2 + 3 2 + n2

break; continue;

Break statement Break statement interrupt the flow of control. In some case, we want only true case to be executed and remain to be skipped For this purpose we write the break statement right after the true case. Break statement interrupt the flow of control and control jumps out of the repitative structure

Break statement #include<iostream.h> #include<conio.h> main() { int num, j; cin>>num; j=2; while(j<num) if(num%j==0) cout<<"number is not prime"; break; } j++; if(num==j) cout<< "number is prime"; getch();

Continue statement There is another statement relating to loops that is continue statement. We may have lot of code in the body of loop Early part of code is executed every time Remaining portion is executed in certain cases and may not executed in other cases. But loop should be continue. Continue forces the immediate next iteration of loop. Statement of the loop body will not executed after continue statement

Continue statement-example int j; J=2; For (int i=0; i<=3; i++) { if If(i==j) Continue; Else Cout<<i; }

Nested LOOP A loop within a loop is called Nested loop for (initial value , terminating value, increment expression) { statement or compound statement }

Nested LOOP A loop with a loop is called Nested loop Inner loop will be executed completely for each iteration of outer loop Inner loop will executed multiple times of outer loop

Nested LOOP Exercise Program For(int i=1; i<=3; i++) For (int j=1; j<=3; j++) cout<< j; Exercise Program

Nested LOOP main() { int i,j; i=1; j=1; while(i++<=100) while(j++<=200) if(j==150) break; else cout<<i<<"----"<<j<<"\n"; } getch();

Nested LOOP Write a program that print different combination of digits between 1----3 (excluding equal combination)

Nested LOOP #include<iostream.h> #include<conio.h> main() { for(int i=1; i<=3; i++) for (int j=1; j<=3; j++) if (i==j) continue; cout<<i<<"------"<<j<<"\n"; } getch();