"while" loops

Information about "while" loops

Published on July 9, 2014

Author: nantha98

Source: authorstream.com

Content

PowerPoint Presentation: 1 Repetition Structure: while Loops CBOP3203 – Object Oriented Programming Learning Outcomes: 2 Learning Outcomes By the end of this part, you should be able to: Explain the syntax to write while loops and how it works Write Java programs using while loops Introduction: 3 Introduction From the previous session, you know that repetition structure in Java can be written in three ways –that is by using for , while and do-while statements. You have already learned about for loops. In this session, you will learn about while statement. Difference between for and while loops: Difference between for and while loops for statement is used when number of loops or repetitions are already known in advance. For example, you already know that there are exactly 10 repetitions or 60 repetitions. On the other hand, the while statement is used when number of loops or repetitions are not known in advance. It means, you know that there are loops or repetitions but not sure how many are there. Sometimes in certain problems either for or while loop can be used especially if the exact number of loops are already known in advance. PowerPoint Presentation: 5 General form of while statement: General form of while statement Example: while statement: Example: while statement Problem I: Problem I Write a Java program to find the addition from 1 to 100 using while loop. Solution: Solution class While1{ public static void main (String[ ] args){ int number=1; int sum=0; while (number <=100){ sum=sum+number; number++; } System.out.println("Sum="+sum); }//main }//class 9 Problem II: Problem II Observe the program segment given on the next slide. Notice that it has two conditions connected through the AND boolean operator. Your task is to identify the conditions and the test variables. And then, determine the output for this program. Explain also how you arrive at this output. What is the output for the following program?: What is the output for the following program? Summary: 12 Summary This session has focused on repetition structure using while loops The while statement checks a condition before any other statement in the loop. This requires that any variables in the tested condition have values assigned before the while is encountered. Within a while loop, there must be a statement that either alters the tested condition’s value or forces a break from the loop.

Related presentations


Other presentations created by nantha98

Using access modifiers
07. 07. 2014
0 views

Using access modifiers

Object and classes
09. 07. 2014
0 views

Object and classes

switch statements
09. 07. 2014
0 views

switch statements

GUI-based Applets
07. 07. 2014
0 views

GUI-based Applets

Java Operators
29. 07. 2014
0 views

Java Operators

Data Types and Variables
29. 07. 2014
0 views

Data Types and Variables