Page 333 - CITS - Computer Software Application -TT
P. 333

COMPUTER SOFTWARE APPLICATION - CITS



           Syntax of if statement is given below.

           f(condition) {
           statement 1; //executes when condition is true

           }
           Consider the following example in which we have used the if statement in the java code.

           Student.java
           Student.java
           public class Student {

           public static void main(String[] args) {
           int x = 10;
           int y = 12;

           if(x+y > 20) {
           System.out.println(“x + y is greater than 20”);
           }

           }
           }

           Output:
                                                  x + y is greater than 20

           2  if-else statement
           The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The
           else block is executed if the condition of the if-block is evaluated as false.
           Syntax:

           if(condition) {
           statement 1; //executes when condition is true

           }
           else{

           statement 2; //executes when condition is false
           }

           Consider the following example.

           Student.java
           public class Student {

           public static void main(String[] args) {
           int x = 10;

           int y = 12;
           if(x+y < 10) {

           System.out.println(“x + y is less than      10”);


                                                           320

                              CITS : IT&ITES - Computer Software Application - Lesson 85 - 93
   328   329   330   331   332   333   334   335   336   337   338