0% found this document useful (0 votes)
204 views

Selection Control Structures: 1. Simple If/else 2. Nested If's 3. Ladderized If/else If/else 4. Switch-Case

The document discusses different selection control structures in programming: 1. Simple if/else statements evaluate a single condition and provide different code blocks based on if the condition is true or false. 2. Nested if statements allow evaluating multiple conditions by placing if statements inside other if statements. 3. Ladderized if/else if/else statements evaluate multiple conditions in order and execute the code for the first matching condition. 4. Switch-case statements evaluate an expression and execute the code block for the matching case value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views

Selection Control Structures: 1. Simple If/else 2. Nested If's 3. Ladderized If/else If/else 4. Switch-Case

The document discusses different selection control structures in programming: 1. Simple if/else statements evaluate a single condition and provide different code blocks based on if the condition is true or false. 2. Nested if statements allow evaluating multiple conditions by placing if statements inside other if statements. 3. Ladderized if/else if/else statements evaluate multiple conditions in order and execute the code for the first matching condition. 4. Switch-case statements evaluate an expression and execute the code block for the matching case value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Selection Control Structures

1. Simple if/else
2. Nested if’s
3. Ladderized if/else if/else
4. Switch-case

Lecture B Slide 1 of 69.


What is selection control structure?

Selection control structure is the capability of


a program to make decisions based on a given
set of conditions and choices.

The four selection statements:


1. Simple if/else
2. Nested if’s
3. Ladderized if/else if/else
4. Switch-case

Lecture
CECSD B Slide 2 of 69.
Simple if/else: syntax

if(condition)
{
statement(s) if condition is true;
}
else
{
statement(s) if condition is false;
}

Lecture
CECSD B Slide 3 of 69.
Example:

int age=17;

if(age>=18)
{
System.out.println(“Qualified to vote”);
}
else
{
System.out.println(“Not qualified to vote”);
}

Lecture
CECSD B Slide 4 of 69.
Nested if’s: syntax
if(condition1)
{
if(condition2)
{
statement(s) if both condition1 and
condition2 are true;
}
else{
statement(s) if condition2 is false;
}
}
else{
statement(s) if condition1 is false;
}

Lecture
CECSD B Slide 5 of 69.
Example:
int age=17;

if(age>=1&&age<=100)
{
if(age>=18)
{
System.out.println(“Qualified to vote”);
}
else
System.out.println(“Not qualified to vote”);
}
else
{
System.out.println(“Out-of-Range”);
}
Lecture
CECSD B Slide 6 of 69.
Ladderized if/else if/else: syntax

if(condition1)
{
statement(s) if condition1 is true;
}
else if(condition2)
{
statement(s) if condition2 is true;
}
else
{
statement(s) if condition1 and condition2 are false;
}

Lecture
CECSD B Slide 7 of 69.
Example:
int age=17;

if(age>=18&&age<=100)
{
System.out.println(“Qualified to vote”);
}
else if(age>=1&&age<=17)
{
System.out.println(“Not qualified to vote”);
}
else
{
System.out.println(“Not qualified to vote”);
}

Lecture
CECSD B Slide 8 of 69.
Switch-case: syntax
switch(expression)
{
case value1:
statement(s) if case value1 is true;
break;
case value2:
statement(s) if case value2 is true;
break;
case value3:
statement(s) if case value3 is true;
break;
default:
statement(s) if no cases value are true;
}

Lecture
CECSD B Slide 9 of 69.
Example:
int number=2;
switch(number)
{
case 1:
System.out.println(“You entered 1”);
break;
case 2:
System.out.println(“You entered 2”);
break;
case 3:
System.out.println(“You entered 3”);
break;
default:
System.out.println(“Invalid input”);
}
Lecture
CECSD B Slide 10 of 69.

You might also like