Excercise/Assignment
======================
if-else
=========
Create a if else taking two float variables and compare them and print any output
switch
=========
Create a switch statement by creating a double variable print output
Syntax
===========
if-else
======================
if(condition)
//statement
if(conditions)
{
//statement1
//statement2
}
else
{
//statement1
//statement2
}
Nested if
==================
if
{
statements
if(condition)
statements
}
Switch
==============
switch(expression)
{
case value1:
//Statement;
break;
case value2:
//Statement;
break;
case value3
//statement;
break;
default:
//statement;
}
Examples:
===============
if-else Example
============================
package controlstatements;
public class ifelse
{
public static void main(String args[])
{
int a=7;
int b=6;
if(aGTb)
{
System.out.println("Yes a is greater than b");
System.out.println("test");
if(a==5)
{
System.out.println("a value is 5");
}
else
{
System.out.println("a value NOT iss 5");
}
}
/*
else { System.out.println("a is not greater than b"); }
*/
}
}
Switch Example:
===================
package controlstatements;
public class switch_Example
{
public static void main(String args[])
{
System.out.println("Test");
int a=5;
switch(a)
{
case 10:
System.out.println("a value is 10");
break;
case 11:
System.out.println("a value is 11");
break;
case 12:
System.out.println("a value is 12");
break;
default:
System.out.println("a value is something");
break;
}
}
}
コメント