Pages

Adhexa

Wednesday, October 7, 2015

For-each loop

For-each loop 

The for-each loop is mainly used to traverse array or collection elements. For-each loop eliminates the possibility of bugs and makes the code more readable.

Syntax of for-each loop:

for(data_type variable : array | collection){}  

Simple Example of for-each loop for traversing the array elements:

    
class ForEachExample1{  
  public static void main(String args[]){  
   int arr[]={1,2,3,4,5};  
  
   for(int i:arr){  
     System.out.println(i);  
   }  
  
 }   
}  

Tuesday, August 17, 2010

For statement

The for-loop typically looks like this:

for(initialisation; condition; incrementation)
{
body/statement
}

The for statement is a loop and the parentheses consists of three parts separated by a semicolon. The first part is the initialization. The second part is the condition. The condition is executed and if it is true, the body of the loop is executed and if the condition is false the loop terminates and the statements after the for loop are executed. The third part is the incrementation step. This part is executed after the body of the for loop is executed.

while statement

The while statement is the simplest loop statement in any programming language. Typically, a while loop look like this:

while(condition){
body/statement
}

The while loop works like this. The condition in the parentheses is first tested. If it is true then the body(or statement) of the loop is executed. The condition is tested again and if true, the body of the loop is executed again. This goes on as long as the condition is true. The loop terminates if the condition is false and the statements after the while loop are executed.

Loop Statements

Suppose in the calculation of area of the circle, we need to take 10 inputs for radius and calculate the area for each input, it is a very tiresome process to write 10 scanf/cin statements for each input and calculate the radius for each one. It is for this reason the loop statements are very useful in any programming language.

C and C++ provides the following loop statements:
  • while-loop
  • for-loop
  • do-while-loop

Getting input – cin

We have already seen what cout does, cin is a similar function except that it reads input from the user instead of writing output. The following is a simple example of how the cin works.

#include <iostream>
using namespace std;
main (){
float area;
int radius;
const float pie = 3.14;
cout<<"Enter the radius of the circle"<<endl;
cin>>radius;
area = pie * radius * radius;
cout<<"Area is "<<area<<endl;
}

Getting input – scanf

We have already seen what printf does, scanf is a similar function except that it reads input from the user instead of writing output. The following is a simple example of how the scanf works.

#include <stdio.h>
main (){
float area;
int radius;
const float pie = 3.14;
printf(“Enter the radius of the circle”);
scanf(“%d”, &radius);
area = pie * radius * radius;
printf(“Area is %f\n”, area);
}

Puzzle 3 - Vertical Limit

Write in the table numbers from 1 to 13, placing each successive number to the right in the same row, or somewhere in the next row. Two digit numbers must be written in two neighboring cells of the same row, one in each cell. Digits that correspond to different numbers can touch each other only diagonally. Numbers at bottom of the grid show the sums of digits in corresponding columns.

91511659



Answer:

123
456
78
910
11
1213
91511659