control structures
TRANSCRIPT
![Page 1: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/1.jpg)
Control structure
s Prefer :
Jihad Enayat Mhmud NasihHemn Mstafa Tishko Nawzad
University of Human
Development
![Page 2: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/2.jpg)
Contents
Comparison operators Control structures if Statements For Statement switch Statement while & do loop
![Page 3: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/3.jpg)
Comparison Operators
== Tests whether two values are equal
!= Tests whether two values are not equal
< Tests if left side value is less than to the right side value
> Tests if left side value is greater than to the right side value
<= Tests if left side value is less than or equal to the right side value
>= Tests if left side value is greater than or equal to the right side value
Operators
![Page 4: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/4.jpg)
if Statements
Syntax :
(1)he syntax of an if statement isif (conditional clause) { Statement(s) block }
There are three basic forms for an if statement:
![Page 5: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/5.jpg)
public class IT{ public static void main(String args[]){ int x = 10; if ( x < 20 ){ System.out.print("This is if statement"); } } }
Example
![Page 6: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/6.jpg)
Result
![Page 7: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/7.jpg)
Use when you want to do one thing or another thing
Syntax: (2)
if (conditional clause) { Statement(s) block } else { Statement(s) block }
if. Else S…
![Page 8: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/8.jpg)
public class IT{ public static void main(String args[]){ int x = 30; if( x < 20 ){ System.out.print("This is if statement"); }else{ System.out.print("This is else statement"); } } }
Example
![Page 9: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/9.jpg)
Result
![Page 10: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/10.jpg)
if else if statement Syntaxif (conditional clause) (3) { Statement(s) block } else if (conditional clause) { Statement(s) block }
Use when there are three or more possibilities
if statement
![Page 11: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/11.jpg)
User input Example
![Page 12: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/12.jpg)
Result
User is…
![Page 13: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/13.jpg)
The for Loop :
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
![Page 14: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/14.jpg)
Syntax :
The syntax of a for loop is:for (initialization; Boolean_expression; update){//Statements}
The for Loop
![Page 15: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/15.jpg)
Here is the flow of control in a for loop:The initialization step is executed first, and only once. This step allows you todeclare and initialize any loop control variables. You are not required to put astatement here, as long as a semicolon appears.Next, the Boolean expression is evaluated
The for Loop
![Page 16: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/16.jpg)
If it is true, the body of the loop isexecuted. If it is false, the body of the loop does not execute and flow of controljumps to the next statement past the for loop.After the body of the for loop executes, the flow of control jumps back up to theupdate statement
The for Loop
![Page 17: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/17.jpg)
This statement allows you to update any loop controlvariables. This statement can be left blank, as long as a semicolon appears afterthe Boolean expression.The Boolean expression is now evaluated again. If it is true, the loop executesand the process repeats itself (body of loop, then update step, then Booleanexpression). After the Boolean expression is false, the for loop terminates.
The for Loop
![Page 18: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/18.jpg)
Example
public class IT{ public static void main( String args[]){For ( int x = 10; x < 20; x = x+1){System.out.print("value of x : " + x );System.out.print("\n"); } }}
![Page 19: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/19.jpg)
This would produce the following result :
Result
![Page 20: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/20.jpg)
Switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
![Page 21: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/21.jpg)
Syntax :The syntax of enhanced for loop is:Switch (expression) { case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements }
Switch Statement
![Page 22: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/22.jpg)
The following rules apply to a switch statement:
The variable used in a switch statement can only be a byte, short, int, or char.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
Switch Statement
![Page 23: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/23.jpg)
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Switch Statement
![Page 24: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/24.jpg)
Example:
![Page 25: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/25.jpg)
Result
Compile and run above program using various command line arguments. This would produce the following result:
![Page 26: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/26.jpg)
While loop :
![Page 27: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/27.jpg)
While Loop Syntax
![Page 28: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/28.jpg)
Do - While Loop
![Page 29: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/29.jpg)
While Loop Syntax
While Loop
![Page 30: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/30.jpg)
REFERENCES :http://www.homeandlearn.co.uk/java/java_if_else_statements.html
http://www.tutorialspoint.com/java/java_decision_making.htm
file:///C:/Users/DotNet/Downloads/files-2._Lectures_8-Do_While_Loop.pdf
![Page 31: Control structures](https://reader033.vdocuments.us/reader033/viewer/2022060118/55899355d8b42a0b768b4602/html5/thumbnails/31.jpg)
Thanks for your Attention
Any Question???
End