Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Introduction to C Programming
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Programming In C++ Spring Semester 2013 Practice 1-3 Lectures Programming In C++, Lecture 3 By Umer Rana.
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.
Spring Semester 2013 Lecture 5
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.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
CONTROLLING PROGRAM FLOW
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Computer Programming Control Structure
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
COMP Loop Statements Yi Hong May 21, 2015.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 4 – C Program Control
- Standard C Statements
Lecture 6 Repetition Richard Gesick.
Lecture 7: Repeating a Known Number of Times
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Programming Fundamentals Lecture #6 Program Control
Introduction to Programming
Structured Program Development in C
Lecture Notes – Week 3 Lecture-2
Loops Prof.Gaikwad Varsha P. Information Technology Dept.
EPSII 59:006 Spring 2004.
Repetition Statements (Loops) - 2
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
Dale Roberts, Lecturer IUPUI
Lec 6 Loop Statements Introduction to Computer Programming
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana

What is counter? What is repeat? What is decisions? Programming In C++, Lecture 3 By Umer Rana

Assignment Operators Assignment operators abbreviate assignment expressions += -= *= /= %= Examples : If a=10 a += 1 or (a = a + 1)output is 11 a -= 1 or (a = a - 1)output is 9 a *= 1 or (a = a * 1)output is 10 a /= 1 or (a = a / 1)output is 10 a %= 1 or (a = a % 1) output is 0

Programming In C++, Lecture 3 By Umer Rana Increment / Decrement Operators Increment operator (++) Can be used instead of a+=1 or a=a+1 Decrement operator (--) Can be used instead of a-=1 or a=a-1 Example: If a equals 10, then printf( "%d", ++c ); Output 11 printf( "%d", c++ ); Output 10 In either case, c now has the value of 10

Programming In C++, Lecture 3 By Umer Rana Decision Making Equality operators == != Relational operators < > <= >=

Programming In C++, Lecture 3 By Umer Rana Data Flow Diagram (DFD) Expression Decision Process Show Flow

Loops To execute a set of instructions repeatedly until a particular condition is being satisfied. Three types of looping statements are there 1) For Loop 2) While Loop 3) Do while Loop Programming In C++, Lecture 3 By Umer Rana

Loops Requires 1.The name of a counter variable 2.The initial value of the counter variable 3.A condition that tests for the final value of the counter variable (i.e., whether looping should continue) 4.An increment or decrement by which the counter variable is modified each time through the loop run Programming In C++, Lecture 3 By Umer Rana

Loops For Loop For loop in C is the most general looping construct. In for looping statement allows a number of lines represent until the condition is satisfied. Syntax of for loop expression: For Loop expression is divided by semicolons into three separate expressions: the “Initialize expression”, the “test expression”, and the “Increment/Decrement expresssion” for(Initialize counter variable ; Condition ; Increment/Decrement the counter variable) { Body of the for loop } Programming In C++, Lecture 3 By Umer Rana

For Loop DFD Programming In C++, Lecture 3 By Umer Rana Initialization expression Test Expression /Condition Body of loop Increment / Decrement Expression Exit False True

Loops Programming In C++, Lecture 3 By Umer Rana Example: Print number 1 to 10 this way: void main() { printf (“1\n”); printf (“2\n”); printf (“3\n”); printf (“4\n”); printf (“5\n”); printf (“6\n”); printf (“7\n”); printf (“8\n”); printf (“9\n”); printf (“10\n”); }

Loops Programming In C++, Lecture 3 By Umer Rana Example: #include void main() { int counter; for(counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter ); } OutPut

Loops Programming In C++, Lecture 3 By Umer Rana Example of Multiple Statement: #include void main() { int counter, total=0; for(counter = 1; counter <= 10; counter++ ) { total=total + 1; printf( “counter=%d, total=%d\n", counter,total ); } OutPut Counter = 1 Total = 1 Counter = 2 Total = 2 Counter = 3 Total = 3 Counter = 4 Total = 4 Counter = 5 Total = 5 Counter = 6 Total = 6 Counter = 7 Total = 7 Counter = 8 Total = 8 Counter = 9 Total = 9 Counter = 10 Total = 10

Loops While Loop While loop in C is the other most general looping construct. The while loop statement executes as long as a specified condition is true. Syntax of while loop expression: while ( condition ) { Code to execute, while the condition is true. } while(condition) { statement(s); } Programming In C++, Lecture 3 By Umer Rana

Loops While Loop DFD Programming In C++, Lecture 3 By Umer Rana

Loops Example Programming In C++, Lecture 3 By Umer Rana #include void main () { // Local variable declaration: int a = 10; // while loop execution while( a < 20 ) { printf(“value of a is %d”,a); a++; }

Loops Example Programming In C++, Lecture 3 By Umer Rana #include void main () { // Local variable declaration: int a = 0; char ch=‘a’; // while loop execution while (ch != ‘\r’) { printf(“Enter a character: \n”); ch=getche(); printf(“Entered Character is %c\n”,ch); }

Loops do while Loop n do… While loop is checks its condition at the bottom of the loop. n A do...while loop is similar to a while loop, except that a do...while loop is execute at least one time Syntax of do..while loop expression: do { statement(s); } while( condition ); Programming In C++, Lecture 3 By Umer Rana

Loops do while Loop Programming In C++, Lecture 3 By Umer Rana

Loops Example Programming In C++, Lecture 3 By Umer Rana #include void main () { // Local variable declaration: int a = 10; // while loop execution do { printf(“value of a is %d”,a); a++; } while( a < 20 ) ; }

Loops Nested Loop n One loop inside another loop. n In nested loop the inner loop is executed first and then outer. Syntax of for Nested Loop: for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); } Programming In C++, Lecture 3 By Umer Rana

Loops Syntax of while Nested Loop: while(condition) { while(condition) { statement(s); } statement(s); } Programming In C++, Lecture 3 By Umer Rana

Loops Syntax of do..while Nested Loop: do { statement(s); do { statement(s); } while( condition ); }while( condition ); Programming In C++, Lecture 3 By Umer Rana

Loops Loop Control Statements: 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 Programming In C++, Lecture 3 By Umer Rana break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. goto statement Transfers control to the labelled statement.

Loops Break Statements: –When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. –If you are using nested loops ( ie. one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block Programming In C++, Lecture 3 By Umer Rana true false

Loops Break Statements: #include Void main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } Programming In C++, Lecture 3 By Umer Rana

Loops Continue Statements: n continue forces the next repetition of the loop to take place, skipping any code in between. Programming In C++, Lecture 3 By Umer Rana true false

Loops Continue Statements: #include Void main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; if( a == 15) { a++; continue; } Programming In C++, Lecture 3 By Umer Rana

Loops Goto Statements: A goto statement provides an unconditional jump from the goto to a labeled statement in the same function. Syntex goto label... label: statement; Programming In C++, Lecture 3 By Umer Rana true false

Loops Goto Statements: #include void main () { /* local variable definition */ int a = 10; /* do loop execution */ LOOP:do { if( a == 15) { /* skip the iteration */ a ++; goto LOOP; } printf("value of a: %d\n", a); a++; }while( a < 20 ); } Programming In C++, Lecture 3 By Umer Rana

Quiz How many type of loops we have? What is the expression of the for loop? What is the expression of the while loop? What is the expression of the do.. while loop? What is nested loop? What break statement does? What continue statement does? What goto statement does? What you have to do? Programming In C++, Lecture 3 By Umer Rana

Assignment # 1 Q1. Differentiate between IDE and Command-Line Development System. Explain the program execution process and environment with the help of diagram? Q2.Explain the following terms with appropriate examples: Getche() Printf() Scanf() Programming In C++, Lecture 3 By Umer Rana