0% found this document useful (0 votes)
30 views4 pages

Chapter 3 SQ

Uploaded by

faiq3239
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)
30 views4 pages

Chapter 3 SQ

Uploaded by

faiq3239
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/ 4

CHAPTER:03

CONDITIONAL LOGIC
1. What is conditional logic?
Definition:
“The logic in which we set rules to change our process based on some input is called
conditional logic.”
Example:
We perform a specified task when the condition is True and some other tasks when it is
False like I will go to walk if it’s not raining otherwise, I will watch a movie.
2. What are control statements?
Definition:
“The statements that are useful to control the flow of execution in a program are called
control statements or control structures.”
Flow of execution means the order in which instruction will execute.
3. Why do we need control statements?
In programming, sometimes we need to execute a certain set of statements if the condition is True
and certain statements if it is False. There are also situations where we need to repeat a set of
statements many times. The control structures deal with these kinds of issues.
4. List the types of control statements.
In C language, there are three basic types of control structures:
• Sequence control statement
• Selection control statement
• Repetition/Loop statement
5. What is sequence control structure?
In sequence control structure, the statements are executed in the same order in which they are
written in the program. Sequential control is the default control statement in C language.
6. What is selection control structure?
Definition:
“The structure that decides which set of statements will execute next based on certain
conditions is called the selection control structure.”
The selection control structure is widely used in programming because it allows the
program to decide an action based upon the user’s input or other processes.
7. What is repetition control structure?
Definition:
“The control structure that is used to repeat a set of statements many times is called
repetition control structure.”
8. Define selection control statements.
Definition:
“The statements which help us to decide which should be executed next, on the basis of
conditions, are called selection control statements.”
9. What are the applications of selection control statements?
The application of selection structure can be seen in systems that have password checking
mechanisms such as ATMs, smartphones, etc. The system allows user access only if the user input
password is the same as the password stored in the system.
10. List the types of selection control statements.
The following are two types of selection statements:
• if statement
• if-else statement
11. What is if statement?
The “if statement” is the simplest decision-making statement. It is used to check a condition.
Based on the result it is decided whether a certain statement or block of statements will be
executed or not. If the condition is true then a block of statements is executed otherwise not.
12. What is the general structure of if statement? Briefly describe it.
The following is the basic structure of if statement:
if(condition)
Associated code
The if statement consists of the following three components i.e., if keyword, condition, and
associated code.
• The “if” is a keyword that is followed by a condition enclosed in parenthesis().
• The result of condition (which is enclosed in parenthesis) will decide whether to execute desired
statements or not. The condition can be a constant, variable, or any valid expression. Any
expression that has a non-zero value is considered True and zero value is considered as False.
• The associated code contains valid C statements. If it contains multiple statements then these must
be enclosed in braces. In case there is a single statement then braces are optional.
13. How if statement work?
The if statement evaluates the test expression inside the parenthesis(). If the test expression is
evaluated to be True, the statements inside the body of if are executed otherwise the statements
inside the body of if are not executed.
14. What is condition in if statement?
The statements which evaluate actions in the program are called conditions.
They always evaluate to True or False. These are created by the programmer. The condition can
be constant, variable, or any valid expression including arithmetic expressions, relational
expressions, logical expressions, or a combination of these. Any expression that has a non-zero
value is considered as True and zero value is considered as False.
15. Write a simple program to explain working of if statement.
Program Output
#include<stdio.h> Variable x is less than y.
int main()
{
int x=20, y=22;
if(x<y)
printf(“Variable x is less than y”);
}

Explanation:
The condition(x<y) specified in the “if” will result in True as 20<22. So, the statement inside
the body of if is executed.
16. Draw the flowchart of if statement.

17. What is the importance of using the braces in if statement?


In case there are more than one statement in an if block, then the use of braces is mandatory.
When the braces are skipped the compiler will generate the syntax error. But these are optional
if there is a single statement in the body of if statement.
18. What is associated code?
The associated code is any valid C language set of statements. It may contain one or more
statements.
19. What is if-else statement?
It is a two-way decision-making statement. In this, the set of statements under “if” are executed
when the condition is True. But if the condition is False then the statements under “else” are
executed. An “if” may not have an associated “else” statement but an “else” is always
associated with an “if” statement. In case the code of “if” or “else” contains multiple statements
then these should be enclosed in braces(). Otherwise, the compiler will issue an error.
20. What will happen if the condition in the if-else block is True?
If the condition in the if-else evaluate to True, then the code associated to if block will be
executed.
21. What will happen if the condition in the if-else block is False?
If the condition in the if-else evaluate to False, then the code associated to else block will be
executed.
22. Draw the flowchart of if-else statement.

23. Write the general structure of if-else statement.


The following is the general structure of if-else statement:
if(condition)
Associated code
else
Associated code
24. How if-else statement works?
If the condition inside the “if” is evaluated to True, then the code associated with it is executed
otherwise code associated to else is executed. But only one of them will execute in both cases.
25. Write a simple program to explain working of if-else statement.
Program Output
#include<stdio.h> Enter an integer: 16
int main()
{
int number; 16 is an even integer
printf(“Enter an integer.”);
scanf(“%d”,&number);
if(number%2==0) //True if the remainder is 0
printf(“%d is an even integer”, number);
else
printf(“%d is an odd integer”, number);
return 0;
}
26. What is compound statement?
Definition:
“A set of multiple instructions enclosed in braces is called a block (enclosed in curly braces)
or a compound statement.”
The braces play an important role while executing a program.
Example:
An “if” may not have an associated else statement but an “else” is always associated with an
“if” statement. In case you have used an “else” without “if” statement then a syntax error will
occur.
27. What is if-else if statement?
The if-else if statement is a more advanced version of the if-else statement. It is useful in
situations when many cases must be executed for different conditions. In if-else if statement,
if a condition in the “outer if” is true then the statements defined in its block will be executed,
otherwise if some other condition from the “inner if” if true then the statements defined in the
“else-if” block will be executed. However, if none of the conditions is true then the statements
defined in the outer “else” block will be executed.
28. Write the general structure of if-else if statement.
The following is the basic structure of if-else if statement:
if(condition1)
{
// These statements would execute if the condition1 is true
}
else if(condition2)
{
// These statements would execute if the condition2 is true
}
else if(condition3)
{
// These statements would execute if the condition3 is true
}
else
{
// These statements would execute if all the conditions return false.
}
29. Draw the flowchart of if-else if statement.

30. Describe some nested selection structures.


The following structures are some valid nested selection structures:
if (conditional 1 is true) if (conditional 1 is true)
if (conditional 2 is true) if (conditional 2 is true)
associated code associated code
else else
associated code if (conditional 3 is true)
associated code

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