Control statements in Java

3,014 views

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,014
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
89
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Control statements in Java

  1. 1. Control Statements Prepared by: Jean Michael Castor
  2. 2. Introduction • Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program.
  3. 3. Introduction • In Java, control statements can be divided under the following three categories: • Selection statements • Iteration statements • Jump statements
  4. 4. SELECTION STATEMENTS
  5. 5. • Selection statements are used in a program to choose different paths of execution based upon the outcome of an expression or the state of a variable.
  6. 6. Using if and if...else • Syntax: if ( Expression ) Statement • Semantics: The expression must be of type boolean. If it evaluates to true, the given statement is executed, otherwise not.
  7. 7. Using if and if...else • Note that there is only one statement. To execute more than one statement conditionally, a block statement is to be used.
  8. 8. Using if and if...else • In addition, our coding standard expects following layout: if ( Expression ) { Statement1 } elseif ( Expression ){ Statement 2 } else{ Statement 3(default value) }
  9. 9. Example Program • Write a program that display grade as PASSED if value is greater than or equal to 75, otherwise, FAILED.
  10. 10. Solution public class Grades{ public static void main (String [] args){ int grade; grade = 10; if(grade >=75) { System.out.println(“PASSED”); } else if(grade < 75){ System.out.println(“FAILED”); } else{ System.out.println(“ ”); } } }

×