0% found this document useful (0 votes)
95 views3 pages

Chapter 5 Using Conditional Construct

The document discusses conditional constructs in programming languages. It defines conditional constructs as statements that decide which code runs based on whether a condition is true or false. There are three main types: if-else statements, if-else if statements, and nested if-else statements. If-else if statements are used to test multiple conditions and have a syntax that includes if blocks checking for different conditions and optional else blocks. Examples are provided of programs using logical operators like && in if conditional statements.

Uploaded by

Sunayna Verma
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)
95 views3 pages

Chapter 5 Using Conditional Construct

The document discusses conditional constructs in programming languages. It defines conditional constructs as statements that decide which code runs based on whether a condition is true or false. There are three main types: if-else statements, if-else if statements, and nested if-else statements. If-else if statements are used to test multiple conditions and have a syntax that includes if blocks checking for different conditions and optional else blocks. Examples are provided of programs using logical operators like && in if conditional statements.

Uploaded by

Sunayna Verma
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/ 3

Using Conditional ConstrUCt

Chapter-5
Subjective Question & Answer
1. What are conditional constructs? Name the different types of conditional
constructs.
Ans A conditional construct is a statement that computer programming language
used to decide which code has to be run when the true condition is met or
which code has not to be run when the true condition is not met.
There are three different types of conditional constructs
 If-else statement
 If-else if statement
 Nested if-else statement

2. Give the syntax of a simple if-else construct.


Ans The if-else statements let a program decide what should be done when the
specified condition is true or false.
The syntax of a simple if-else construct is as follows
If (condition)
{
Statement; /* runs when the condition is true */
}
Else
{
Statement; /* runs when the condition is false */
}

3. Give examples of a Java program where the AND logical operator is used.
Ans Program Name – Basketball_TeamA
Import java.util.Scanner;
Class Basketball_TeamA
{
public static void main (String args[])
{
int height=0;
int avg=0;
Scanner Scanner = new Scanner (System.in);
System.out.println(“Enter the candidate’s height in feet: ”);
heights = Scanner.nextInt();
System.out.println(“Enter the average number of baskets per match: ”);
avg = Scanner.nextInt();
if (height>=4 && avg>=3)
{
System.out.println(“Candidate has been shortlisted: ”);
}
else if (height>=4 && avg<3)
{
System.out.println(“Candidate has not been shortlisted: ”);
}
else if (height<4 && avg>=3)
{
System.out.println(“Candidate has been shortlisted: ”);
}
else
{
System.out.println(“Candidate has not been shortlisted ! ”);
}
}
}

4. Differentiate between if-else and if-else if constructs.


Ans If-else statements: If-else statements let a program decide what should
be done when the specified condition is true or false

If-else if statements: If-else if statements are used when multiple


conditions are to be tested in a program

5. Give the syntax of a simple if-else if construct.


Ans If-else if statements: If-else if statements are used when multiple
conditions are to be tested in a program
The syntax of a simple if-else if construct is as follows:
If (condition 1) /*Executes when the condition 1 is true*/
{
/* Lines of code */
}
else if (condition 2) /* Executes when the condition 2 is true */
{
/* Lines of code */
}
else if (condition 3) /*Executes when the condition 3 is true*/
{
/* Lines of code */
}
else /*Executes when none of the above condition is true*/
{
/* Lines of code */
}

TRUE FALSE

1. In the if-else construct, statements included under the “else” code block will be run

only if the specified condition is true. FALSE

2. && is a logical operator that returns true if either of the two conditions is true.

FALSE

3. The if-else if construct is used when more than two conditions are to be tested in a

program. TRUE

4. If the condition specified in if statement evaluates to true, else block of code is

executed. FALSE

5. Not (!) logical operator returns true if the operand on right evaluates to true.

FALSE

You might also like