Informatics Notes (Class 11)

CBSE Class 11 Informatics Practices Chapter 2 Introduction to Programming Languages Part 3

Written by cbselearners
Rate this post

Introduction to Programming Languages

Introduction to Programming Languages

Introduction to Programming Languages

 

Programming Construct

  • Sequnce
  • Selection
  • Iteration


Decision Making Statements ( Selection Construct)

There are two types of decision making statements in Java. They are:

  • if statements
  • switch statements

 

 The if Statement:An if statement consists of a Boolean expression followed by one or more statements.
Syntax:
The syntax of an if statement is:

 if (Bolean_expression)
{
//Statement will execute if the Boolean expression is true

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:

intx=10;
if(x<20){
System.out.print(“This is if statement”)
}

This would produce the following result;
This is if statement

The if…else Statement:  

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:  

if(Boolean expression){
//Executes when the Boolean expression is true
}else{ 
Executes when the Boolean expression is false
}

Example:

intx=30;

if(x<20){
System.out.print(“This is if statement”);
}else{
System.out.print(“This is else statement”);
}

This would produce the following result:
This is else statement 

The if…else if…else Statement: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:   

if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean _expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean _expression 3){
//Executes when the Boolean expression 3 is true

}else{
//Executes when the none of the above condition is true.
}
Example:
intx=30;
if(x==10){
System.out.print(“Value of X is 10”):

}else if(x==20{

System.out.print(“Value of X is 20”):

}else if(x==30{

System.out.print(“Value of X is 30”):

}else{

System.out.print(‘This is else statement”);
This would produce the following results:
Value of X is 30  

Nested if…else Statement:

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:

if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
if(Boolean expression 2){
//Executes when the Boolean expression 1 is true
    }
}
You can nest else if…else in the similar way as we have nested if statement. Example:
int x= 30;
int y= 10;
 
if( x==30){
if(y== 10){
System.out.print(“X=30 and Y=10”);
}
 
This would produce the following result:
X=30 and Y=10

The Switch Statement :

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:

switch(expression){
case value :
//Statements
Break: 
case value :
//Statements
Break:

//You can have any number of case statements.
default : //Optional
//Statements

}
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 :





Iteration Construct :
1. Java while Loop

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;
} w
hile(i<10);



3. Java for Loop :

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( )

Output:

 1 2 3 4 5 6 7 8 9 10
 1 3 5 7 9
10 9 8 7 6 5 4 3 2 1

Statement for Jump:
 
1. break statement in java:

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:

Output:
0
1
2
3
4
breaking the for loop..

2. continue statement in java:
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:  

 

About the author

cbselearners

Leave a Comment

error: Content is protected !!