0% found this document useful (0 votes)
4 views20 pages

Lec 05 Java Fundamental of Computing

The document provides an overview of the 'if' and 'if-else' statements in programming, focusing on their syntax and execution flow. It includes examples demonstrating how to find the minimum of two numbers and handle division by zero errors. The document emphasizes the evaluation of conditions and the execution of corresponding statements based on the truth value of those conditions.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Lec 05 Java Fundamental of Computing

The document provides an overview of the 'if' and 'if-else' statements in programming, focusing on their syntax and execution flow. It includes examples demonstrating how to find the minimum of two numbers and handle division by zero errors. The document emphasizes the evaluation of conditions and the execution of corresponding statements based on the truth value of those conditions.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

ESc101 : Fundamental of Computing

I Semester 2008-09

Lecture 5

• If statement

1
Execution of a Program

class anyname
{
public static void main(String args[])
{
⇒ statement1;
⇒ statement2;
⇒ statement3;
· · ·;
· · ·;
⇒ statementk;
}
}

2
Execution of a Program

class anyname
{
public static void main(String args[])
{
⇒ statement1;
⇒ statement2;
⇒ statement3;
· · ·;
· · ·;
⇒ statementk;
}
}

3
Execution of a Program

class anyname
{
public static void main(String args[])
{
⇒ statement1;
⇒ statement2;
⇒ statement3;
· · ·;
· · ·;
⇒ statementk;
}
}
⇒ : Instruction pointer (IP) - the next instruction to be executed

4
I : If statement
Find the minimum of two or more numbers.

class if_example
{
public static void main(String args[])
{
int i,j,min;
.
.
//---write code here so that min
//---stores the minimum of i and j
}
}

5
If statement

Syntax :

if (condition) statement;

where condition is an expression of type boolean

6
If statement

Execution of IF statement :condition is evaluated

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒ statement c;

7
If statement

Execution of IF statement : if condition is true

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒ statement c;

statement b is executed, and ...

8
If statement

Execution of IF statement : if condition is true

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒statement c;

statement b is executed, and then IP goes to statement c.

9
If statement

Execution of IF statement : if condition is false

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒statement c;

statement b is NOT executed, and IP goes to statement c directly.

10
Examples : If statement
class if_example
{
public static void main(String args[])
{
int i,j,max;
i=100; j=79;
min = i;
if(j<i) min=j;
System.out.println(‘‘minimum of ‘‘+i+’’ and ‘‘+j+’’ is ‘‘+min);
}
}

11
If-else statement : Example
class IF_Else_example1
{
public static void main(String args[])
{
int i,j;
i = 100; j = 0;
// write code so that
// if j == 0 then we print an error message
// else we print the quotient i/j
i =i+j;
}
}

12
If-else statement

Syntax :

if (condition) statement b;
else statement c;

where condition is an expression of type boolean

13
If-else statement

Execution of IF-ELSE statement : first condition is evaluated

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒else ⇒statement c;
⇒ statement d;

14
If-else statement

Execution of IF-ELSE statement : if condition is true

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒else ⇒statement c;
⇒ statement d;

statement b is executed, and ...

15
If-else statement

Execution of IF-ELSE statement : when condition is true

⇒ statement a;
⇒ if (condition) ⇒ statement b;
⇒else ⇒statement c;
⇒statement d;

statement b is executed, and then IP goes to statement d

16
If-else statement

Execution of IF-ELSE statement : when condition is false

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒else ⇒statement c;
⇒ statement d;

statement c is executed, and ...

17
If-else statement

Execution of IF-ELSE statement : when condition is false

⇒ statement a;
⇒ if(condition) ⇒ statement b;
⇒else ⇒statement c;
⇒statement d;

statement c is executed, and then IP goes to statement d

18
If-else statement : Example
class IF_Else_example1
{
public static void main(String args[])
{
int i,j;
i = 100; j = 0;
if(j==0) System.out.println("Division by error");
else System.out.println(i+" divided by "+j+" is "+(i/j));
i = i+j;
}
}

19
If-else statement for blocks

if(condition)
{
.
statements
.
}
else
{
.
statements
.
}

20

You might also like