ICSE Class 10 Computer Applications Question 2 of 43

Conditional Constructs in Java — Question 2

Back to all questions
2
Question

Question 2

What is conditional flow of control? Explain with an example.

Answer

By default, the statements of a program are executed sequentially from top to bottom in order in which they are written. But most of the times our programs require to alter this top to bottom control flow based on some condition. When the flow of control of a program is changed based on some condition using control statements, it is termed conditional flow of control. For example, in the below program we achieve conditional flow of control using the if-else statement.

public class ConditionalExample {
    public static void main(String args[]) {
        int x = -10;
        if (x >= 0) {
            System.out.println("Positive Number");
        }
        else {
            System.out.println("Negative Number");
        }
    }
}