Decision Making and Branching in java and Advanced Java

Views:
 
Category: Education
     
 

Presentation Description

Vaayaa Edu provides IT training in ahmedabad

Comments

Presentation Transcript

Decision Making and Branching:

Decision Making and Branching Created By : Bhoomi Shah Java and Advanced Java Trainer At VaaYaa Education

Conditional Statements:

Conditional Statements IF IF Else Else If Switch IF : Syntax : if(condition) { Statements; } Example: If(no1>no2) { Printf (“Number 1 is big”); }

Continue....:

Continue.... If else Syntax If(Condition) { Statement; } Else { statement; }

Example:

Example If(no1>no2) { printf (“No1 is big”); } Else { printf (“No2 is small”); }

Else If:

Else If If(condition 1) Statement1; Elseif (condition 2) Statement2; Elseif (condition 3) Statement3; Elseif (condition 4) Statement4; Else Statement 5;

Example:

Example If(code==1) colour =“red”; Else if(code==2) Colour = “blue”; Else if(code==3) Colour =“yellow”; Else Colour =“white”;

Switch Statement:

Switch Statement Syntax : Switch(expression) { Case value: statement; break; Case value: statement;break ; Case value: statement; break; Default: statement; break; }

Example:

Example switch (grade) { case 1: printf ("Fall (F)\n");break; case 2: printf ("Bad (D)\n");break; case 3: printf ("Good (C)\n");break; case 4: printf ("Very Good (B)\n");break; case 5: printf ("Excellent (A)\n");break; default: printf ("You have inputted false grade\n"); break; // break isn’t necessary here } }

Decision Making and Looping:

Decision Making and Looping

Loops:

Loops Loops are used to repeat a block of code. A looping process ,in general, would include yhe folloinf four steps 1) Setting and initialization of a counter 2) Execution of the statement in the loop 3) Test for a specified condition for execution of the loop 4) Incrementing the counter

Types of Loops:

Types of Loops Java supports three types of loops: For loop While loop Do While loop

For loop:

For loop For Loop : Syntax : for( initialization;condition;increament ) Example : for( i =1;i<10;i++)

Example:

Example Class abc { public static void main(String args[]) { int x; for ( x = 0; x < 10; x++ ) { System.out.println(“x = “+ x); } } }

While Loop:

While Loop While Loop : Code to execute while the condition is true Syntax : While(test condition) { body of the loop } Example : While( i >10) { System.out.println(“ I = “+ i ); i ++; }

Example:

Example ------- ------- Sum = o; N=1; While(n <= 10) { Sum= Sum+n *n; n=n+1; } System.out.println(“Sum = ”+ sum);

Do while Loop:

Do while Loop Syntax : do { } while ( condition ); Example: Class abc { public static void main( args []) { int x; x = 0; do { System.out.println( "Hello, world!\n" ); } while ( x != 0 ); }

Classes,Objects&Methods:

Classes,Objects&Methods

Classes:

Classes A class os a userdefind data type with a template that serves to define its properties. Once the class has been defined,,we have create a nuer of objects The basic form of a class defition is: Class classname [extends superclassname ] { [field declaration] [method declaration] }

Fields Declaration:

Fields Declaration Data is encapsulated in a class by placing data fields inside the body of the class defition . We can declare the instabce variable exctly the same way as we declare local variables Example: class rectangle { int length; int width; }

Method Declaration:

Method Declaration A class with only data fields has no life. We must add methods that are necessary for manipulating the data contained in the class. Methods are declared inside the body of the class but immediately after the declaration of variable.

Continue…:

Continue… The general form of a method declaration is: type methodname (parameter-list) { method-body }

Continue…:

Continue… Method declaration have four basic parts: 1)The name of the method (methodname) 2)The type of the value the method return(type) 3)A list of parameters( paramter -list) 4)The body of the method

Example:

Example class rectangle { int length; int width; Void getdata ( int x, int y) // method declaration { length=x; length=y; }

Creating Object:

Creating Object Object in java are created using the new operator. The new operator creates an object of the specified class and returns a reference to that object . Example: Rectangle rect1; //declare the object rect1 = new Rectangle(); //instantiate object Second way to create a object; Rectangle rect1=new Rectangle(); Rectangle rect2=new Rectangle(); and so on…..

Accessing Class Member:

Accessing Class Member Syntax for assigning the value: objectname.variablename=value; objectname.methodname(parameter-list); Example: rect1.length=15; rect1.width=20; rect2.length=22; rect2.width=28;

Example:

Example Class Rectangle { int length,width ; //declaration of variable void getData ( int x, int y) // defition og methods { length=x; width=y; } int area=length*width; //definition of another method return(area); }

Countiue…:

Countiue … rect2.getData=(20,12); //accessing methods area2=rect2.rectArea(); System.out.println(“Area1 = ” + area1); System.out.println(“Area2 = ” + area2); } }

Thank You:

Thank You VaaYaa Edutech provides Java and Advanced Training in Ahmedabad . A/1/A, 3rd Floor, VaaYaa Lobby, Chinubhai Tower, Next to H K College, Ashram Road, Income Tax, Gujarat, Ahmedabad- 380009 Call Us Now: +919586979730 vaayaaedu@gmail.com

authorStream Live Help