ICSE java Iterative statements, Summaries of Computer science

This fille shows you iterative constructs in java. Very easy to understand iteration

Typology: Summaries

2023/2024

Available from 06/25/2024

scoremaster-icse-java
scoremaster-icse-java 🇮🇳

4 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Iterative constructs in JAVA
Loops
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download ICSE java Iterative statements and more Summaries Computer science in PDF only on Docsity!

Iterative constructs in JAVA

Loops

Definition

  • Computers are best suited for applications where a sequence of steps has to repeat several times.
  • The program construct that is used to repeat such a sequence is called a loop.
  • Repeating a set of action again and again until a particular condition is satisfied
  • Ex: Filling Water , Eating food

Entry controlled Loop While loop and “for” loop are called as entry- controlled loop, since they check the condition in the beginning of the loop itself.

Exit controlled Loop Do… while loop is called as exit-controlled loop, since it checks the condition at the end of the loop. .

For loop

  • Loop is used in programming to repeat a specific block of code. In this article, you will learn to create a for loop in Java programming.
  • Loop is used in programming to repeat a specific block of code until certain condition is met (test expression is false).
  • Loops are what makes computers interesting machines. Imagine you need to print a sentence 50 times on your screen. Well, you can do it by using print statement 50 times (without using loops). How about you need to print a sentence one million times? You need to use loops.

How for loop works?

  • The initialization expression is executed only once.
  • Then, the test expression is evaluated. Here, test expression is a Boolean expression.
  • If the test expression is evaluated to true,
  • Codes inside the body of for loop is executed.
  • Then the update expression is executed.
  • Again, the test expression is evaluated.
  • If the test expression is true, codes inside the body of for loop is executed and update expression is executed.
  • This process goes on until the test expression is evaluated to false.
  • If the test expression is evaluated to false, for loop terminates.

We can initialize the variable, check condition and increment/decrement value. It consists of four parts: Initialization Condition^ Statement Incr/decr It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition. It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition. The statement of the loop is executed each time until the second condition is false It increments or decrements the variable value. It is an optional

condition.

Examples of for loop To print first 10 natural numbers 1 2 3 4 5 6 7 8 9 10 initialization test expression incr/decr for(int I = 1;i<=10;i++) { System.out.println(i); }

While Loop The syntax of while loop is while (testExpression) { // codes inside body of while loop }

Flowchart

Example int x=1; while (x<=10) { System.out.println (x); x++; } Note: If there is more than one statement in the loop body, then it is compulsory to group them within a pair of curly block { } initialization test expression incr/decr

do...while Loop

The do...while loop is similar to while loop with one key

difference. The body of do...while loop is executed for

once before the test expression is checked.