Posts

Showing posts with the label Control Flow in Java

Java for Beginners – Control Flow in Java Decisions & Loops

  1. What is Control Flow? Control flow decides which part of your code runs and how many times it runs. It’s like giving your program the ability to make choices and repeat tasks. There are two main types : Decision-making statements (if-else, switch) Looping statements (for, while, do-while) 2. Decision-Making Statements A) if Statement Runs code only if the condition is true. int age = 20 ; if (age >= 18 ) { System.out.println( "You are an adult." ); } B) if-else Statement Chooses between two options. if (age >= 18 ) { System.out.println( "Adult" ); } else { System.out.println( "Minor" ); } C) if-else-if Ladder Chooses between multiple options. int marks = 85 ; if (marks >= 90 ) { System.out.println( "Grade A" ); } else if (marks >= 75 ) { System.out.println( "Grade B" ); } else if (marks >= 50 ) { System.out.println( "Grade C" ); } else { ...