0% found this document useful (0 votes)
87 views45 pages

LESSON3 - Logical Control Structures

Here are programs to solve the two activities: //Activity 1 import java.util.Scanner; public class Activity1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); if(age < 18) { System.out.println("You can't get a driver's license under 18 years of age."); } else { System.out.println("You are eligible to apply for a driver's license."); } } } //Activity 2 import java.util.Scanner; public class Activity2 {

Uploaded by

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

LESSON3 - Logical Control Structures

Here are programs to solve the two activities: //Activity 1 import java.util.Scanner; public class Activity1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); if(age < 18) { System.out.println("You can't get a driver's license under 18 years of age."); } else { System.out.println("You are eligible to apply for a driver's license."); } } } //Activity 2 import java.util.Scanner; public class Activity2 {

Uploaded by

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

LOGICAL CONTROL STRUCTURES

SEQUENTIAL
DECISION/SELECTION
SWITCH
LOGICAL CONTROL STRUCTURES

SEQUENTIAL
DECISION/SELECTION
SWITCH
C++ if statement
An if statement executes
when the Boolean expression
is TRUE.
Syntax:

if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}
If the boolean expression
evaluates to true, then the
if block of code will be
executed
if( a > 20 )
{
cout << "a is greater than 20" << endl;
}
C++ if...else statement
An if statement can be
followed by an optional else
statement, which executes
when the Boolean expression
is false.
Syntax:

if(boolean_expression)
{
// statement(s) will execute if the boolean expression
is true
}
else
{
// statement(s) will execute if the boolean expression
is false
}
If the boolean expression
evaluates to true, then the
if block of code will be
executed, otherwise else
block of code will be
executed.
if( a < 20 )
{
cout << "a is less than 20;" << endl;
}
else
{
cout << "a is not less than 20;" << endl;
}

cout << "value of a is : " << a << endl;


RESULT:

a is not less than 20;


value of a is : 100
C++ if...else if…else statement
An if statement can be followed by an
optional else if...else statement, which is
very useful to test various conditions using
single if...else if statement.

When using if , else if , else statements


there are few points to keep in mind.
An if can have zero or one else's and it must
come after any else if's.

An if can have zero to many else if's and


they must come before the else.

Once an else if succeeds, none of the


remaining else if's or else's will be tested.
Syntax:
if(boolean_expression 1)
{
// Executes when the boolean expression 1 is true
}
else if( boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
else if( boolean_expression 3)
{
// Executes when the boolean expression 3 is true
}
else
{
// executes when the none of the above condition is true.
}
if( a == 10 )
{
cout << "Value of a is 10" << endl;
}
else if( a == 20 )
{
cout << "Value of a is 20" << endl;
}
else if( a == 30 )
{
cout << "Value of a is 30" << endl;
}
else
{
cout << "Value of a is not matching" << endl;
}
cout << "Exact value of a is : " << a << endl;
RESULT:

Value of a is not matching


Exact value of a is : 100
C++ nested if statement
You can include multiple selection paths in
a program by using an
if. . .else structure if the action statement
itself is an if or if. . .else statement.
When one control statement is located
within another, it is said to be nested.
if (gender ==‘M’)
if (age < 21)
cout<<“Policy rate is 0.05”;
else
cout<<“Policy rate is 0.035”;
else if (gender == ‘F’)
if (age < 18)
cout<<“Policy rate is 0.04”;
else
cout<<“Policy rate is 0.03”;
C++ switch statement
A switch statement allows a
variable to be tested for equality
against a list of values. Each value is
called a case, and the variable being
switched on is checked for each
case.
Syntax
switch(expression)
{
case constant-expression : statement(s);
break; //optional
case constant-expression : statement(s);
break; //optional
default : //Optional
statement(s);
}
The following rules
apply to a switch
statement:
• The expression used in
a switch statement must have an integral
or enumerated type, or be of a class type in
which the class has a single conversion
function to an integral or enumerated type.

• You can have any number of case


statements within a switch. Each case is
followed by the value to be compared to
and a colon.
• The constant-expression for a
case must be the same data type as
the variable in the switch, and it must
be a constant or a literal.

• When the variable being switched on


is equal to a case, the statements
following that case will execute until
a break statement is reached.
When a break statement is reached, the
switch terminates, and the flow of control
jumps to the next line following the switch
statement.

Not every case needs to contain a break. If


no break appears, the flow of control
will fall through to subsequent cases until a
break is reached.
• A switch statement can have an
optional default case, which
must appear at the end of the
switch. The default case can be
used for performing a task when
none of the cases is true. No break
is needed in the default case.
switch(grade)
{
case 'A' : cout << "Excellent!" << endl;
break;
case 'B' : cout<<“Magnificent!” <<endl;
break;
case 'C' : cout << "Well done" << endl;
break;
case 'D' : cout << "You passed" << endl;
break;
case 'F' : cout << "Better try again" << endl;
break;
default : cout << "Invalid grade" << endl;
}

cout << "Your grade is " << grade << endl;


Exercise

if (april == 99)
cout<<“ F is for fire that burns down the whole
town”;
else
cout<<“ F is for friends who do stuff together!”;
Exercise

if ((c+d)>= m))
cout<<“ U is for uranium, bombs”;
else
cout<<“ U is for you and me”;
Exercise

if ((score<= 80)&&(score==85))
cout<<“ N is for no survivors when you're-”;
else
cout<<“ N is for anywhere and anytime at all”;
Exercise

if (grade > 90 )
cout<< “ Excellent ”;
else if (grade > 85)
cout<< “ Very Good ”;
else if (grade > 80)
cout<< “ Good ”;
else
cout<< “ Poor ”;
Exercise

d = d + c / a – b;
if (d == – 94)
cout<<"Peace";
else
cout<<”Love";
Exercise

z = x * y % z;
if (y >= 10)
cout<<"Good”;
else
cout<<"Bad";
Activity1.cpp
Write a Java program for applying a
driver’s license. User will enter their
age and it should give a feedback that
the user can’t get a driver’s license
under the age of 18.
Make a simple switch program that
will determine if you are 1st year,
2nd year, 3rd year, 4th year or not a
student.

You might also like