Page 1

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

Conditional Statements • • • •

IF IF Else Else If Switch

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

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

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

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

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

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

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

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 • Java supports three types of loops: 1) For loop 2) While loop 3) Do While loop

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

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 : 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 ------------Sum = o; N=1; While(n <= 10) { Sum=Sum+n*n; n=n+1; } System.out.println(“Sum = ”+ sum);

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 • 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 â&#x20AC;˘ Data is encapsulated in a class by placing data fields inside the body of the class defition. â&#x20AC;˘ We can declare the instabce variable exctly the same way as we declare local variables Example: class rectangle { int length; int width; }

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.


â&#x20AC;˘ The general form of a method declaration is: type methodname (parameter-list) { method-body }

Continueâ&#x20AC;Ś â&#x20AC;˘ 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 class rectangle { int length; int width; Void getdata(int x, int y) // method declaration { length=x; length=y; }

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 â&#x20AC;˘ 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 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… rect2.getData=(20,12); //accessing methods area2=rect2.rectArea(); System.out.println(“Area1 = ” + area1); System.out.println(“Area2 = ” + area2); }


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 • • Website:

Android training in ahmedabad  
Android training in ahmedabad  

Learn to develop Android application from experienced professionals. Vaayaa Edu provides professional software training with the help of wo...