C Programming Conditional Statements PDF
Document Details
Uploaded by AffableClover
Tags
Summary
This document discusses various conditional statements in C programming, including if, if-else, nested if-else, and if-else-if ladder statements. It also provides example programs for each type.
Full Transcript
Decision making statement:- The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true. There are the following variants of if statement in C language. o If statement...
Decision making statement:- The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true. There are the following variants of if statement in C language. o If statement o If-else statement o If else-if ladder o Nested if If Statement The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below. 1. if(expression){ 2. //code to be executed 3. } Flowchart of if statement in C Let's see a simple example of C language if statement. #include #include void main(){ int number=0; printf("Enter a number:"); scanf("%d",&number); if(number%2==0){ printf("%d is even number",number); } getch(); } Output Enter a number:4 4 is even number enter a number:5 if-else in C The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else when the condition is false? Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false. The if-else statement consists of two blocks, one for false expression and one for true expression. Syntax of if else in C if (condition) { // Executes this block if condition is true } else { // Executes this block if condition is false } Flowchart of the if-else statement in C Write a program in c for if else. #include #include vsoid main() { int number; clrscr(); printf("enter an integer:"); scanf("%d",&number); if(number%2==0) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); getch (); } Output: Nested if-else in C A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, C allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement. Syntax of Nested if-else if (condition1) { // Executes when condition1 is true if (condition_2) { // statement 1 } else { // Statement 2 } } else { if (condition_3) { // statement 3 } else { // Statement 4 } } Flowchart of Nested if-else Write a program for nested if statement. #include #include void main() { int var1,var2; clrscr(); printf("input value of var1 "); scanf("%d",&var1); printf("input value of var2"); scanf("%d",&var2); if(var1!=var2) { printf("var1 is not equal to var2"); if(var1>var2) { printf("var1 is greater than var2"); } else { printf("var2 is greater than var1"); } } else { printf("var1 is equal to var2"); } getch(); } If else-if ladder Statement The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched. if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true }... else{ //code to be executed if all the conditions are false } Flowchart of else-if ladder statement in C Program to calculate the grade of the student according to the specified marks. #include #include void main() { int marks; printf("Enter your marks?"); scanf("%d",&marks); if(marks > 85 && marks 60 && marks 40 && marks 30 && marks