Chapter 3 SQ
Chapter 3 SQ
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.