Programming Fundamental

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
Smoking is prohibited 1 CS 101 Second Exam Review Prepared by Dr. Amer Al-Badarneh 
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Thursday, December 21, 2006 The Briggs/Chase Law of Program Development: To determine how long it will take to write and debug a program, take your best.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Fundamental Programming Fundamental Programming for Loops.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Statements
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Fundamental Programming Fundamental Programming More on Repetition.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Unary Not operator !  !true = false  !false = true.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Chapter 9 Repetition.
REPETITION CONTROL STRUCTURE
Control Structures.
Programming Fundamental
The nested repetition control structures & Continue Statements
Programming Fundamentals
Lecture 4B More Repetition Richard Gesick
Chapter 5 Repetition.
Loop Control Structure.
Iteration with While You can say that again.
For & do/while Loops.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Chapter 9 Control Structures.
Conditional Construct
Lecture Notes – Week 3 Lecture-2
Counting Loops.
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Chapter 2.2 Control Structures (Iteration)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Debugging October 4, 2006 ComS 207: Programming I (in Java)
do/while Selection Structure
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Decisions, decisions, decisions
Control Statements Paritosh Srivastava.
Iteration (Loop) part II
Programming Fundamental
Programming Fundamental
Programming Fundamental
LOOP Basics.
break & continue Statements
Programming Fundamental
Presentation transcript:

Programming Fundamental Instructor Name: Muhammad Safyan Lecture-10

Loop Order of movement of control in for loop: First time: Expression 1-> Expression 2->Loop body -> Expression 3 Second time and onward: Expression 2->Loop body -> Expression 3

Properties of Expression-1 Expression1 can initialize the more than one variable. For example:. main(){     int i,j,k;     for(i=0,j=2,k=1;i<=4;i++) {          cout<<i+j+k;     } Getch();         } Output: 3 4 5 6 7

Properties of Expression-1 Expression1 is optional. For example: main(){     int i=1;     for(;i<=4;i++){          cout<<i;     } getch();         } Output: 1 2 3 4

Properties of Expression-2 Expression2 is also optional. For example: main(){     int j;          for(j=0; ;j++){          cout<<j;          if(j>2)              break;     }     Getch();         } Output: 0 1 2

Properties of Expression-2 If expression2 is zero means condition is false and any non zero number means condition is true. For example  main(){     int i;         for(i=0;-5 ;i++){          cout<<i;          if(i==3)              break;     } Getch();         } Output: 0 1 2 3

Properties of Expression-2 If expression2 is zero means condition is false and any non zero number means condition is true. For example   main(){     int i;         for(i=5;0 ;i++){          cout<<i;     } Getch();         } Output:

Properties of Expression-3 It is called as instrumentation expression. Task of this expression is to increment the variable. Properties: We can increment more than one variable at the same time in the expression3. For example   main(){     int i,j,k;           for(i=0,j=0,k=0;k<=3;i++,++j,k+=2)) {          cout<<i+j+k;     } Getch();         } Output: 0 4

Properties of Expression-3 It is called as instrumentation expression. Task of this expression is to increment the variable. Properties: We can increment more than one variable at the same time in the expression3. For example   main(){     int i,j=0;          for(i=0;i<=3;++i,i++,++j ){          cout<<i<<”,”<<j;;     } Getch();         } Output: 0 0 2 1

Properties of Expression-3 Expression 3 is also optional. For example  main(){     int i;         for(i=0;i<=3; ){          cout<<i++;     } Getch();       } Output: 0 1 2 3

main(){ Loop body: int i,j=0; for(i=0;i<=3;++i,i++,++j ) Loop body contains the part of code which we have to execute multiple numbers of times. Properties of loop body: If loop body contain only one statement then brace is optional. For example:   main(){     int i,j=0;         for(i=0;i<=3;++i,i++,++j )    cout<<i<<j;      Getch();         } Output: 0 0 2 1

main(){ Loop body: int i; for(i=0;i<=10;i++); cout<<i; Loop without body is possible. For example   main(){     int i;         for(i=0;i<=10;i++);     cout<<i; Getch();         } Output: 11

for ( int row = 1; row <= 10; ++row ) { for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }

for (int row = 10; row >= 1; --row ) { for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }

for ( int row = 1; row <= 10; ++row ) { for ( int space = 1; space <= 10 - row; ++space ) cout << ' '; for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }

for ( int row = 10; row >= 1; --row ) { for ( int space = 1; space <= 10 - row; ++space ) cout << ' '; for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }

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();

A while loop nested in for loop

A while loop nested in for loop long sum; int i, j, count=10; for(i=1; i<=count; i++) { sum=0; j=1; while(j<=i) //++j; sum=sum+j; cout<<j<<"+"; ++j; } cout<<"="<<sum; cout<<endl;