0% found this document useful (0 votes)
4 views27 pages

2024 08 17 LoopingBranching

The document outlines control flow statements in Java, including decision-making, looping, and branching statements. It details various types of statements such as if-then, switch for decision-making, and while, do-while, for for looping. Additionally, it explains the use of break and continue statements within loops and provides example questions related to these concepts.

Uploaded by

prajwal sri tej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

2024 08 17 LoopingBranching

The document outlines control flow statements in Java, including decision-making, looping, and branching statements. It details various types of statements such as if-then, switch for decision-making, and while, do-while, for for looping. Additionally, it explains the use of break and continue statements within loops and provides example questions related to these concepts.

Uploaded by

prajwal sri tej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

LOOPING AND DECISION MAKING

DEFINITION

o Control flow statements, change or break the flow of execution


by implementing decision making, looping, and branching your
program to execute particular blocks of code based on the
conditions

o There are 3 types of control flow statements supported by the


Java programming language

o Decision-making statements : if-then, if-then-else, switch

DEFINITION
DEFINITION

o Looping statements : for, while, do-while

o Branching statements : break, continue, return

DEFINITION
DECISION MAKING

o Decision making structures have one or more conditions to be


evaluated or tested by the program, along with a statement or
statements that are to be executed if the condition is determined
to be true, and optionally, other statements to be executed if the
condition is determined to be false

o Decision-making statements : if-then, if-then-else, switch

DECISION MAKING
IF STATEMENT

If statement consists a condition, followed by statement


or a set of statements as shown below

if (condition)
{

Statement(s);
}

IF STATEMENT
IF STATEMENT

The statements gets executed only when the given


condition is true. If the condition is false then the statements
inside if statement body are completely ignored

IF STATEMENT
IF ELSE STATEMENT

This is how an if-else statement looks

if (condition) {
Statement(s);
} else {
Statement(s);
}
The statements inside “if” would execute if the condition
is true, and the statements inside “else” would execute if the
condition is false
NESTED IF STATEMENT

When there is an if statement inside another if statement


then it is called the nested if statement

if (condition_1) {
Statement1(s);

if (condition_2) {
Statement2(s);
}
}

NESTED IF
NESTED IF STATEMENT

Statement1 would execute if the condition1 is true.


Statement2 would only execute if both the conditions( condition1
and condition2) are true

NESTED IF
SWITCH CASE
STATEMENT

Switch case statement is used when we have number of


options (or choices) and we may need to perform a different task
for each choice

SWITCH STATEMENT
SWITCH CASE
STATEMENT

The syntax of Switch case statement looks like this


switch (variable or an integer
expression) {
case constant:
//Java code
;
case constant:
//Java code
;
default:
//Java code
;
}
A KEY NOTE

o Break statement is optional in switch case but you would use it


almost every time you deal with
switch case

o Break statements are used when you want your program-flow


to come out of the switch body

o Whenever a break statement is encountered in the switch


body, the execution flow would directly come out of the switch,
ignoring rest of the case
A KEY NOTE
LOOPING STATEMENTS

Looping in programming languages is a feature which


facilitates the execution of a set of instructions/functions
repeatedly while some condition evaluates to true

There are 3 types of looping statements

o While Loop

o Do-While Loop

o For Loop

LOOPING STATEMENTS
While LOOPING
STATEMENTS

o A while loop is a control flow statement that allows code to be


executed repeatedly based on a given Boolean condition

o The while loop can be thought of as a repeating if statement

while (Boolean
condition) {
loop statements...
}

WHILE LOOP
While LOOPING
STATEMENTS

o While loop starts with the checking of condition


o Once the condition is evaluated to true, the statements in the
loop body are executed
o Normally the statements contain an update value for the
variable being processed for the next iteration
o When the condition becomes false, the loop terminates which
marks the end of its life cycle

WHILE LOOP
DO WHILE LOOPING
STATEMENTS

o do while loop is similar to while loop with only difference that it


checks for condition after executing the statements, and
therefore is an example of Exit Control Loop.

do {
statements..
}
while (condition);

DO-WHILE LOOP
DO WHILE LOOPING
STATEMENTS
o do while loop starts with the execution of the statement(s).
There is no checking of any condition for the first time.

o After the execution of the statements, and update of the


variable value, the condition is checked for true or false value.
If it is evaluated to true, next iteration of loop starts.

o When the condition becomes false, the loop terminates which


marks the end of its life cycle.

DO-WHILE LOOP
FOR LOOPING STATEMENTS

for loop provides a concise way of writing the loop


structure. Unlike a while loop, a for statement consumes the
initialization, condition and increment/decrement in one line
thereby providing a shorter, easy to debug structure of looping.

for (initialization; condition; increment / decrement


operation)

FOR -LOOP
DEFINITION

o Initialization condition

Here, we initialize the variable in use. It marks the


start of a for loop. An already declared variable can be used or
a variable can be declared, local to loop only

o Testing Condition:

It is used for testing the exit condition for a loop. It


must return a boolean value

DEFINITION
DEFINITION

o Statement execution:

Once the condition is evaluated to true, the


statements in the loop body are executed.

DEFINITION
BREAK STATEMENT

o The break construct is used to break out of the middle of loops

o When a break statement is encountered, execution of the


current loops immediately stops and resumes at the first
statement following the current loop.

o We can have more than one break statement in a loop.

o The break command terminates only the current loop and not any
enclosing loops.
CONTINUE

o It is mostly used inside loops.

o Whenever it is encountered inside a loop, control directly


jumps to the beginning of the loop for next iteration, skipping
the execution of statements inside loop’s body for the current
iteration.

CONTINUE
QUESTION: 01

Given two integers, m and n. The task is to


check whether m!=n or m=n.
Input:
The first line of input contains T, denoting the number of
test cases. Then T test case follow. First line of each test case
contains two integer m and n separated by space.
Output:
For each test case in new line, print lesser, greater or equal for m!
=n, or m=n respectively.
Contraints:
1 <= T <= 10
-100 <= m <= 100
-100 <= n <= 100 QUESTION:01
QUESTION: 02

Program to print following pattern

Input :

Output:
1
2*2
3*3*3
3*3*3
2*2
1 QUESTION:02

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy