0% found this document useful (0 votes)
7 views

4 - Programming I

Uploaded by

shahmrunalstudy
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)
7 views

4 - Programming I

Uploaded by

shahmrunalstudy
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/ 25

Control Statements in JAVA

If Statements - Example
Output
class Main
{
public static void main (String[] args)
{
int x = 3;
if (x == 3)
{
System.out.println(“x must be 3”);
}
System.out.println(“This runs no matter what”);
}
}
If Statements - Example
Output
class Main
{
public static void main (String[] args)
{
int x = 2;
if (x == 3)
{ System.out.println(“x must be 3”); }
else { System.out.println(“x is NOT 3”); }

System.out.println(“This runs no matter what”);


}
}
Else-if Statement
if-else-if ladder – Nested if
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement; . . .

else statement;
Example
Switch Case

Output
Switch Case

Output
while, do-while Statements
class Main
{
public static void main(String[] args)
{ int i=7;
while (i>3)
{
System.out.println("Hello " +i);
i--;
} // end of while

} // end main method


} // end class
While / do-while Statements
class Main
{
public static void main(String[] args)
{ int i=7;
do
{
System.out.println("Hello " +i);
i--;
} while (i>3); // end of while

} // end main method


} // end class
While / do-while Statements - Difference
For Loop
for(initialization; condition; iteration)
{ // body }

class Main
{ public static void main(String args[])
{
int n;
for(n=10; n>0; n--)
System.out.println("tick " + n);
}
}
Eg Prime Number or Not
class Main
{ public static void main(String args[])
{ int num; boolean isPrime = true; num = 11;
for(int i=2; i < num/2; i++)
{ if((num % i) == 0)
{ isPrime = false;
break; }
}
if(isPrime)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
// Using the comma.
class Comma
{ public static void main(String args[])
{ int a, b;
for(a=1, b=4; a<b; a++,b--)
{ System.out.println("a = " + a);
System.out.println(“b = " + b);
}
}
}
Break Statement
• By using break, you can force immediate termination of a loop, bypassing
the conditional expression and any remaining code in the body of the loop

class BreakLoop
{ public static void main(String args[])
{ for(int i=0; i<100;i++)
{ if(i == 10) break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}}
Continue and Return
Actual Object-Oriented Program
class Box create a Box object called mybox
{ double width;
double height; Box mybox = new Box();
double depth;
}
Create Objects
• 2 instances
The New Operator
• Dynamically allocates memory for an object
Arrays
• Similar to C / C++

You might also like