You are on page 1of 16

DECISION MAKING

• If-else Statement
• Switch Statement.
IF STATEMENT
• The if statement is the most simple decision making statement. It is used to decide whether a
certain statement or block of statements will be executed or not based on a certain type of
condition.
Syntax:
if (condition){
// statement will be executed if condition is true.
}
Flowchart:
Example:
If-else:

if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Flowchart:
Nested if-else:
Nested if statements mean an if statement inside another if statement. 

Syntax: 

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else{
// Executes when condition 2 is false
}
}
If else-if ladder:

Here, a user can decide among multiple options. As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the else-if ladder is bypassed. If none of the conditions are true, then
the final else statement will be executed. 

Syntax: 

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
PROGRAM TO FIND LARGEST OF THREE NUMBERS
PROBLEMS
• Program to find a number is even or odd.
• Program to find largest of three numbers.
• In an assembly a person aged between 18 to 40 years are allowed to vote. Write a program to
check if a person is eligible to vote or not.
• Write a program to evaluate the following:
user enters a number, if number is even the number get divided by 2, if number is odd , the
number gets multiplied by 2.
PROGRAM TO FIND A NUMBER IS EVEN OR ODD
PROGRAM FOR ELIGIBILITY OF VOTE
Ternary Operator:

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It
can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements:

Syntax:

variable = (condition) ? expressionTrue : expressionFalse;


JUMP STATEMENTS

• Break: A break statement is a jump statement which terminates the execution of loop and
the control is transferred to resume normal execution after the body of the loop.
• Continue: The continue statement works quite similar to the break statement.
Instead of terminating the loop (break statement), continue statement forces the
loop to continue or execute the next iteration.
• Goto :This statement is used to jump directly to that part of the program to which
it is being called. Every goto statement is associated with the label which takes
them to part of the program for which they are called. (break statement), continue
statement forces the loop to continue or execute the next iteration.

You might also like