Computer Science
Nested if in C
Nested if in C is a programming construct that allows for the execution of a set of statements based on multiple conditions. It involves using an if statement within another if statement to create a hierarchy of conditions. This allows for more complex decision-making in a program.
Written by Perlego with AI-assistance
Related key terms
1 of 5
3 Key excerpts on "Nested if in C"
- eBook - ePub
ANSI C Programming
Learn ANSI C step by step
- Yashavant Kanetkar(Author)
- 2020(Publication Date)
- BPB Publications(Publisher)
else . To override this default scope, a pair of braces, as shown in the above example, must be used.Nested if-elsesIt is perfectly all right if we write an entireif-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’ of if s. This is shown in the following program.Note that the second if-else construct is nested in the first else statement. If the condition in the first if statement is false, then the condition in the second if statement is checked. If it is false as well, then the final else statement is executed.You can see in the program how each time a if-else construct is nested within another if- else construct, it is also indented to add clarity to the program. Inculcate this habit of indentation; otherwise, you would end up writing programs which nobody (you included) can understand easily at a later date. Note that whether we indent or do not indent the program, it doesn’t alter the flow of execution of instructions in the program.In the above program, an if-else occurs within the else block of the first if statement. Similarly, in some other program, an if-else may occur in the if block as well. There is no limit on how deeply the if s and the else s can be nested.Forms of ifThe if statement can take any of the following forms:Use of Logical Operators
C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively.There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and && . Don’t use the single symbol | and & . These single symbols also have a meaning. They are bitwise operators, which we would examine in Chapter 14 .The first two operators, && and|| , allow two or more conditions to be combined in an if - eBook - PDF
- Joyce Farrell(Author)
- 2018(Publication Date)
- Cengage Learning EMEA(Publisher)
Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 245 Nesting if and if…else Statements Nesting if and if…else Statements Within an if or an else clause, you can code as many dependent statements as you need, including other if and else statements. Statements in which a decision is contained inside either the if or else clause of another decision are nested if statements. Nested if statements are particularly useful when two or more conditions must be met before some action is taken. For example, suppose you want to pay a $50 bonus to a salesperson only if the salesperson sells at least three items with a total value of $1,000 or more. Figure 5-12 shows the logic and the code to solve the problem. Notice there are no semicolons in the if statement code shown in Figure 5-12 until after the bonus = SALES_BONUS; statement. The expression itemsSold >= MIN_ITEMS is evaluated first. Only if this expression is true does the program evaluate the second Boolean expression, totalValue >= MIN_VALUE. If that expression is also true, the bonus assignment statement executes, and the nested if statement ends. When you use nested if statements, you must pay careful attention to placement of any else clauses. For example, suppose you want to distribute bonuses on a revised schedule as follows: • $10 bonus for selling fewer than three items • $25 bonus for selling three or more items whose combined value is less than $1,000 • $50 bonus for selling at least three items whose combined value is at least $1,000. Figure 5-13 shows the logic. Figure 5-12 Determining whether to assign a bonus using nested if statements The Boolean expression in each if statement must be true for the bonus assignment to be made. - eBook - ePub
- K. S. Oza, S. R. Patil, R. K. Kamat(Authors)
- 2022(Publication Date)
- River Publishers(Publisher)
C language has three branching statements: if statement, if-else statement, and switch statement. These statements control the behavior of a program. Each branch consists of set of instructions to be executed depending upon some condition. If that condition evaluates to true, then the corresponding block of statements get executed. The “if statement” is the simplest form. It takes an expression in parenthesis, and if the expression evaluates to true, then the statement/statements get executed; otherwise, they are skipped.2.2 The if Statement
The if statement is used to make one time decision. Here either the statement is true or false, like binary decision. General form:if (condition is true)execute statement;where if is a keyword: condition—expression using C’s relational operator statement—block of code to be executed. The block of code is executed if and only if the condition is true; otherwise, compiler skips it.2.3 The if-else Statement
The if statement facilitates execution of block of code if condition is true; otherwise, it does nothing. The if-else statement provides a way to execute a set of instructions if condition is true or false.General form:if (condition is true)execute statement;elseexecute statement; // if condition is falseHere, else part will execute if condition will evaluate to false.Program 2.1 Use of if-else statement to predict leap year Leap year comes once in four years. It has 29 days in February. User is asked to enter the year, which is then divided by 4; and if the remainder is zero, then the year is leap year, else not.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.


