Loop Control Statements
1. While
2. do-while
3. for
1. while() - Loop is executed only when condition is true.
format:
while (condition)
{ series of statements; }
2. do-while - Loop is executed for first time irrespective
of the condition. After executing while loop for first
time, then condition is checked.
format:
do
{ series of statements; }
while (condition);
3. for()
format:
for (var_initialization; condition; inc_dec_statement)
{ series of statements; }
4. Continue statement
used to continue the next iteration of for loop, while loop
and do-while loops. So, the remaining statements are
skipped within the loop for that particular iteration.
Syntax :
continue;
5. Break
used to terminate the while loops, switch case loops and
for loops from the subsequent execution.
Syntax:
break;
Increment and Decrement Operators
Operator Description
++i Value of i is incremented before assigning it to variable i.
i++ Value of i is incremented after assigning it to variable i.
i Value of i is decremented before assigning it to variable i.
i Value of i is decremented after assigning it to variable i.
Sample Program #2:
A program that will print the first 10 natural numbers.
Output:
1 2 3 4 5 6 7 8 9 10
Sample Program #3:
The program will accept any integer number and will
display the numbers from 1 to the inputted number.
Output:
Enter any number: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sample Program #4:
The program should accept two values, num1 and num2,
and will print the number from num1 to 100 or less with
using the interval of num2.
Output:
Enter num1: 50
Enter num2: 5
50 55 60 65 70 75 80 85 90 95 100
Sample Program #5
Write a program that will allow the user to enter and add
any integer until a zero (0) is entered. Display the total.
Sample Run:
Enter a number: 5
Enter a number: 10
Enter a number: 8
Enter a number: 2
Enter a number: 0
( since 5+10+8+2+0=25) Sum is 25