INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CS201 - Repetition loops.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Program Control Dilshad M. Shahid New York
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Sentinel Loops. while Syntax while(expression) statement.
Follow up from lab See Magic8Ball.java Issues that you ran into.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Lab 4: Loops and Iteration Graham Northup
INC 161 , CPE 100 Computer Programming
REPETITION CONTROL STRUCTURE
INC 161 , CPE 100 Computer Programming
Lecture 7: Repeating a Known Number of Times
Chapter 2 Assignment and Interactive Input
Week 4 – Repetition Structures / Loops
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Repetition Structures
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Topics discussed in this section:
Logical Operators and While Loops
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Debugging October 3, 2007 ComS 207: Programming I (in Java)
- Additional C Statements
Outline Altering flow of control Boolean expressions
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Topics discussed in this section:
Loops in C.
INC 161 , CPE 100 Computer Programming
INC 161 , CPE 100 Computer Programming
Week 6 CPS125.
Computer Science Core Concepts
Loops Prof.Gaikwad Varsha P. Information Technology Dept.
Computer programming Lecture 3.
Debugging October 4, 2006 ComS 207: Programming I (in Java)
INC 161 , CPE 100 Computer Programming
Logical Operators and While Loops
Seating “chart” Front - Screen rows Back DOOR.
Dale Roberts, Lecturer IUPUI
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Lec 6 Loop Statements Introduction to Computer Programming
Looping and Repetition
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lab 4

While-Loop Note: while has the same format as if While Loop The syntax of a while statement is as follows: The evaluation of the controlling expression takes place before each execution of the loop body. The loop body is executed repeatedly until the return value of the controlling expression is equal to 0. while(expression) statement Note: while has the same format as if

Flowchart of a while Loop The syntax of a while loop is as follows: while(expression) statement Do you remember “if” flowchart?

Example1 Try changing this to 0 What happen? #include <stdio.h> main () { int i = 1; if(i==1) printf(“Hello\n”); } Try changing this to 0 What happen? Run normal and with debugger

Example1 Change to while Try changing this to 0 What happen? #include <stdio.h> main () { int i = 1; while(i==1) printf(“Hello\n”); } Try changing this to 0 What happen? Run normal and with debugger

Counter-controlled Loop i = counter

Counter-Controlled Loop #include <stdio.h> main () { int i = 1; while(i <= 5) printf(“%d\n”,i); i++; } 3 points Initial counter Counter condition Count up/down Run normal and with debugger

Task 1 Figure out how to Print numbers from 0 to 4 Print numbers down from 20 to 1 Using while loop

Do-While Loop Do-While Loop The syntax of a do-while statement is as follows: The evaluation of the controlling expression takes place after each execution of the loop body. The loop body is executed repeatedly until the return value of the controlling expression is equal to 0. do statement while(expression);

Flowchart of a do-while loop The syntax of a do-while statement is as follows: do statement while(expression);

While vs. Do-While Run normal and with debugger #include <stdio.h> main () { int i = 1; while(i <= 5) printf(“%d\n”,i); i++; } #include <stdio.h> main () { int i = 1; do printf(“%d\n”,i); i++; } while(i <= 5); Run normal and with debugger

Task 2 Figure out how to Print numbers from 0 to 4 Print numbers down from 20 to 1 Using do-while loop

For-Loop Designed for counter–controlled loop Put everything in places for(expression1; expression2; expression3) statement Expression 1 Do once before entering the loop Expression 2 Condition of the loop Expression 3 Do every time after statement

While-For Comparison #include <stdio.h> main () { int i = 1; while(i <= 5) printf(“%d\n”,i); i++; } #include <stdio.h> main () { int i; for(i=1;i <= 5;i++) printf(“%d\n”,i); }

Task 3 Figure out how to Print numbers from 0 to 4 Print numbers down from 20 to 1 Using for loop

Task 4 Write a program that calculate the sum of numbers from 1-100 and print the result on the screen. Hint: You program should compute in steps, Round 1 0 + 1 = 1 Round 2 1 + 2 = 3 Round 3 3 + 3 = 6 Round 4 6 + 4 = 10 :