flow of control in c, c++, and java, class 11nnotes pdf download, part 1 flow of control in c, c++, java, and code wiht programming class 11 nnotes pdf download 11 notes, class 11 informatics practices notes, part 1 flow of control in c, c++, java, and code wiht programming class 11 nnotes pdf download class 11, part 1 flow of control in c, c++, java, and code wiht programming class 11 nnotes pdf download class 11 notes, class 11 part 1 flow of control in c, c++, java, and code wiht programming class 11 nnotes pdf download, note informatics practices, informatics practices notes, part 1 flow of control in c, c++, java, and code wiht programming class 11 nnotes pdf download, class 11 cinformatics practices chapter 2 notes, 11th standard informatics practices notes, 11th std
PROGRAMMING CONSTRUCT
1. SEQUENCE
2. SELECTION
3. ITERATION
There are two types of decision making statements in Java. They are:
· if statements
· switch statements
An if statement consists of a Boolean expression followed by one or more statements.
Syntax:
The syntax of an if statement is:
If the Boolean expression evaluates to true then the block of code inside the if statement will be
executed. If not the first set of code after the end of the if statement (after the closing curly brace)
will be executed.
Example:
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax: The syntax of an if...else is:
An if statement can be followed by an optional else if...else statement, which is very useful to
test various conditions using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind.
· An if can have zero or one else’s and it must come after any else if’s.
· An if can have zero to many else if’s and they must come before the else.
· Once an else if succeeds, none of the remaining else if’s or else’s will be tested.
Syntax:
The syntax of an if...else is:
Example:
It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. Syntax:- The syntax for a nested if...else is as follows:
You can nest else if...else in the similar way as we have nested if statement. Example:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax: The syntax of enhanced for loop is:
The following rules apply to a switch statement:
· The variable used in a switch statement can only be a byte, short, int, or char.
· You can have any number of case statements within a switch. Each case is followed by
the value to be compared to and a colon.
· The value for a case must be the same data type as the variable in the switch and it must
be a constant or a literal.
· When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
· When a break statement is reached, the switch terminates, and the flow of control jumps
to the next line following the switch statement.
· Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
· A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true. No
break is needed in the default case.
Example
The while loop or while statement continually executes a block of statements while a particular condition is true. The while syntax can be written as:
while (expression)
{
statement(s)
}
The while loop evaluates expression, which must return a boolean value. If the while loop expression returns true, then the statements with in the while block will be executed. The while loop continuously executes the statements within the block, until the expression returns false. Here is a simple while loop example, which executes until i value became 10:
int i=0;
while(i < 10){
//this block will executed until
//i value became 10
System.out.print(i+" ");
i=i+1;
}
Output:
0 1 2 3 4 5 6 7 8 9
2. Java do-while Loop :-
A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least
one time. The difference between do-while and while loop is that do-while evaluates its condition at the
bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at
least once.
Here is the syntax to write do-while loop:
do
{
statement(s)
}
while (expression);
}
While (expression);
The do-while statement evaluates expression, which must return a boolean value. If the expression is true, the flow of control goes back to the do, and the statements within the loop executes again. This process repeats until the expression returns false. Here is a simple do-while example
int i = 0;
do {
System.out.print(i+" ");
i=i+1;
} while(i<10);
The for statement or for loop provides a way to iterate over a range or list of values. Using for loop you
can repeatedly loops until a particular condition is satisfied. The general form of the for statement can be
expressed as follows:
for (initialization; condition
for terminating; loop increment)
{
statement(s)
}
In the above statement, initialization expression initializes the loop; it is executed only once, as the loop begins. When the termination condition returns false, then the loop terminates. The increment expression is invoked after each iteration through the loop. Here you can either increment or decrement a value. Here is a simple for loop example to display numbers from 1 to 10.
for(int i=1;i<=10;i++){
System.out.print(i+" ");
}
System.out.println( );
/**
* another example to increment by 2 steps
*/
for(int i=1;i<=10;i=i+2){
System.out.print(i+" ");
}
System.out.println( );
/**
* Below loop prints the numbers in reverse order
*/
for(int i=10;i>0;i--){
System.out.print(i+" ");
}
System.out.println( )
The break statement is one of the control statement in java. The break statement is generally used to break the loop before the completion of the loop. The break statement is used when we want to jump out of a single loop or single case in switch statement. Here is a simple example for break statement:
The continue statement skips the current iteration of a for, while , or do-while loop. The moment it encounters continue statement, it skips the rest of the statements in the loop and evaluates the expression for next iterations. The difference between break and continue statements are, break statement comes out of the loop, continue statement skips the current iteration and jumps to the next iteration in a single loop. Here is a simple example for continue statement:
Copyright @ ncerthelp.com A free educational website for CBSE, ICSE and UP board.