Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

What Is Unconditional Branching?

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

What is conditional branching?

Conditional branch, A programming instruction that directs the computer to another part of the
program based on the results of a compare. High-level language statements, such as IF THEN
ELSE and CASE, are used to express the compare and conditional branch.
What is unconditional branching?
Unconditional branching is when the programmer forces the execution of a program to jump to
another part of the program. Theoretically, this can be done using a good combination of loops and
if statements. In fact, as a programmer, you should always try to avoid such unconditional
branching and use this technique only when it is very difficult to use a loop.The goto statement is
used for unconditional branching or transfer of the program execution to the labeled statement.

Write the syntax of if statement?


if (testExpression)
{
// statements
}
The if statement evaluates the test expression inside the parenthesis.
If the test expression is evaluated to true (nonzero), statements inside the body of if is executed.If
the test expression is evaluated to false (0), statements inside the body of if is skipped from
execution.

Java Program Example - Find Largest of Two Numbers


import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int a, b, big;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Two Number : ");
a = scan.nextInt();
b = scan.nextInt();
if(a>b)
{
big = a;
}
else
{
big = b;
}
System.out.print("Largest of Two Number is " +big);
}
}
What is the switch statement?
A switch statement is a type of selection control mechanism used to allow the value of a variable
or expression to change the control flow of program execution via a multiway branch.
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}

What are nested if statement?


A nested if is an if statement that is the target of another if statement. Nested if statements means
an if statement inside another if statement. Yes, i.e, we can place an if statement inside another if
statement.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}

What are various loop statements?


Looping statement are the statements execute one or more statement repeatedly several number of
times. There are three types of loops; while, for and do-while.When you need to execute a block
of code several number of times then you need to use looping concept.There are three type of
Loops available.
while loop
for loop
do..while

What is break statement?


Break used to come out of the loop instantly. When a break statement is encountered inside a loop,
the control directly comes out of loop and the loop gets terminated. It is used with if statement,
whenever used inside loop. This can also be used in switch case control structure. Whenever it is
encountered in switch-case block, the control comes out of the switch-case.

What is continue statement?


Continue statement is opposite to that of break statement, instead of terminating the loop, it forces
to execute the next iteration of the
loop.
As the name suggest the continue
statement forces the loop to continue
or execute the next iteration. When
the continue statement is executed in
the loop, the code inside the loop
following the continue statement will
be skipped and next iteration of the
loop will begin.
Syntax:
continue;

What is an array?
Arrays a kind of data structure that
can store a fixed-size sequential
collection of elements of the same type. An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99]
to represent individual variables. A specific element in an array is accessed by an index.All arrays
consist of contiguous memory locations. The lowest address corresponds to the first element and
the highest address to the last element.
To declare an array a programmer specifies the type of the elements and the number of elements
required by an array as follows −
type arrayName [ arraySize ];

Write about if and if-else statements


If statement
If statement consists a condition, followed by statement or a set of statements as shown below:
if(condition){
Statement(s);
}
The statements gets executed only when the given condition is true. If the condition is false then
the statements inside if statement body are completely ignored.
Example:
public class IfStatementExample
{
public static void main(String args[])
{
int num=70;
if( num < 100 ){
/* This println statement will only execute,
* if the above condition is true
*/
System.out.println("number is less than 100");
}
}
}
Output:
number is less than 100

If-else statement
This is how an if-else statement looks:

if(condition) {
Statement(s);
}
else {
Statement(s);
}
The statements inside “if” would execute if the condition is true, and the statements inside “else”
would execute if the condition is false.
Example:

public class IfElseExample {

public static void main(String args[]){


int num=120;
if( num < 50 ){
System.out.println("num is less than 50");
}
else {
System.out.println("num is greater than or equal 50");
}
}
}
Output:
num is greater than or equal 50
Write about switch Statement?
The switch statement is a multi-way branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression. Basically, the expression
can be byte, short, char, and int primitive data types.
Syntax of Switch-case :
// switch statement
switch(expression)
{
// case statements
// values must be of same type of expression
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional

// We can have any number of case statements


// below is default statement,used when none of the cases is true.
// No break is needed in the default case.
default :
// Statements
}

class Day {
public static void
main(String[] args) {

int week = 4;
String day;

switch (week) {
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
default:
day = "Invalid day";
break;
}
System.out.println(day);
}
}
Output:
Wednesday

Write a program to find out biggest among three numbers


import.util.Scanner;
class LargestOfThreeNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if ( x > y && x > z )
System.out.println("First number is largest.");
else if ( y > x && y > z )
System.out.println("Second number is largest.");
else if ( z > x && z > y )
System.out.println("Third number is largest.");
else
System.out.println("Entered numbers are not distinct.");
}
}

Write about while loop with example


In while loop, condition is evaluated first and if it returns true then the statements inside while
loop execute. When condition returns false, the control comes out of loop and jumps to the next
statement after while loop.
Note: The important point to note when using while loop is that we need to use increment or
decrement statement inside while loop so that the loop variable gets changed on each iteration, and
at some point condition returns false. This way we can end the execution of while loop otherwise
the loop would execute indefinitely.
Syntax of while loop
while(condition)
{
statement(s);
}

Example:
class WhileLoopExample {
public static void main(String
args[]){
int i=10;
while(i>1){
System.out.println(i);
i--;
}
}
}

Write about do-while with example


In while loop, condition is evaluated before the execution of loop’s body but in do-while loop
condition is evaluated after the execution of loop’s body.
Syntax of do-while loop:
do
{
statement(s);
} while(condition);
How do-while loop works?
First, the statements inside loop execute and then the condition gets evaluated, if the condition
returns true then the control gets transferred to the “do” else it jumps to the next statement after
do-while.

Example:
class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do{
System.out.println(i);
i--;
}while(i>1);
}
}

Write about for loop with example


Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In
Java we have three types of basic loops: for, while and do-while. In this we will learn how to use
“for loop” in Java.
Syntax of for loop:
for(initialization; condition ; increment/decrement)
{
statement(s);
}
Flow of Execution of the for Loop:
As a program executes, the interpreter always keeps track of which statement is about to be
executed. We call this the control flow, or the flow of execution of the program.

First step: In for loop, initialization happens first and only one time, which means that the

initialization part of for loop only executes once.


Second step: Condition in for loop is evaluated on each iteration, if the condition is true then the
statements inside for loop body gets executed. Once the condition returns false, the statements in
for loop does not execute and the control gets transferred to the next statement in the program
after for loop.
Third step: After every execution of for loop’s body, the increment/decrement part of for loop
executes that updates the loop counter.
Fourth step: After third step, the control jumps to second step and condition is re-evaluated.
Example:
class ForLoopExample {
public static void main(String args[]){
for(int i=10; i>1; i--){
System.out.println("The value of i is: "+i);
}
}
}
The output of this program is:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2

Write a program to find out factorial of a given number


class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:
Factorial of 5 is: 120

Write a program to Find natural sum of a given number:


public class Natural
{
public static void main(String args[])
{
int x, i = 1 ;
int sum = 0;
System.out.println("Enter Number of items :");
Scanner s = new Scanner(System.in);
x = s.nextInt();
while(i <= x)
{
sum = sum +i;
i++;
}
System.out.println("Sum of "+x+" numbers is :"+sum);
}
}
Output:
$ javac Natural.java
$ java Natural
Enter Number of items :
55
Sum of 55 numbers is :1540

Write a program to find minimum value of a given array


import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int small, size, i;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Array Size : ");


size = scan.nextInt();

System.out.print("Enter Array Elements : ");


for(i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}

System.out.print("Searching for the Smallest Element....\n\n");

small = arr[0];

for(i=0; i<size; i++)


{
if(small > arr[i])
{
small = arr[i];
}

System.out.print("Smallest Element = " + small);


}
}
Output:
Enter array size:6
Enter array elements:56
12
98
34
70
18
Searching for the smallest element........
Smallest Element = 12

Write a program Find sum of elements of a given array


import java.util.Scanner;
class SumDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
int sum = 0;
System.out.println("Enter the elements:");
for (int i=0; i<10; i++)
{
array[i] = scanner.nextInt();
}
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
Output:
Enter the elements:
1
2
3
4
5
6
7
8
9
10
Sum of array elements is:55

Write a Progam to add two matrix


import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[]) or
{
int i, j;
int mat1[][] = new int[3][3];
int mat2[][] = new int[3][3];
int mat3[][] = new int[3][3];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Matrix 1 Elements : ");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat1[i][j] = scan.nextInt();
}
}

System.out.print("Enter Matrix 2 Elements : ");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat2[i][j] = scan.nextInt();
}
}

System.out.print("Adding both Matrix to form the Third Matrix...\n");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}

System.out.print("The Two Matrix Added Successfully..!!\n");

System.out.print("The New Matrix will be :\n");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.print(mat3[i][j]+ " ");
}
System.out.println();
}
}
}

Write a program to Matrix multiplication


import java.util.Scanner;
public class MatixMultiplication
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the base of squared matrices");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the elements of 1st martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the elements of 2nd martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = input.nextInt();
}
}
System.out.println("Multiplying the matrices...");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product is:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
input.close();
}
}
Output:
$ javac MatixMultiplication.java
$ java MatixMultiplication

Enter the base of squared matrices:


3
Enter the elements of 1st martix row wise:
123
456
789
Enter the elements of 2nd martix row wise:
234
567
891
Multiplying the matrices...
The product is:
36 42 21
81 96 57
126 150 93

You might also like