Page 271 - Computer_Science_F5
P. 271

Computer Science  IF…..ELSE statement
           If you have two options to test in C++ programming, IF…THEN statement is not a
           good choice to use. Instead, you can use IF…ELSE statement. IF…ELSE statement
           specifies two decision-making statements where one is true and the other is false.
           It carries a logical test among two options and selects one option that correlates
          FOR ONLINE READING ONLY
           to the condition. The application example of IF…..ELSE statement as shown in
           Figure 4.40 is to set condition when you need to make choice among the people by
           considering their gender, either male or female. The syntax of if..else statement is
           given as:

                    if(condition)
                    {
                    statement(s); // this block will execute when condition is true.
                    }

                    else
                    {
                    statement(s); // this block will execute when condition is false
                    }


           Figure 4.40 shows the execution flowchart of IF….ELSE statement.








                                     True       Condition       False




                          True statement(s)                      False statement(s)












                                       Figure 4.40: If…else flowchart

           You can learn from the following Program Examples 4.21 on how to use the ‘If else’
           to write the program.


                                                 262
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   262                                                     23/07/2024   12:34
   266   267   268   269   270   271   272   273   274   275   276