top of page
PREVIOUS STEP
NEXT STEP
NEXT STEP
PREVIOUS STEP
MY BUTTON

C LANGUAGE

The if, if...else and nested if...else statement are used to make one-time decisions in C Programming, that is, to execute some code/s and ignore some code/s depending upon the test expression.C if Statementif (test expression) { statement/s to be executed if test expression is true; }The if statement checks whether the text expression inside parenthesis ( ) is true or not. If the test expression is true, statement/s inside the body of if statement is executed but if test is false, statement/s inside body of if is ignored.Flowchart of if statement Example 1: C if statementWrite a C program to print the number entered by user only if the number entered is negative. #include <stdio.h> int main(){ int num; printf("Enter a number to check.\n"); scanf("%d",&num); if(num<0) { /* checking whether number is less than 0 or not. */ printf("Number = %d\n",num); } /*If test condition is true, statement above will be executed, otherwise it will not be executed */ printf("The if statement in C programming is easy."); return 0; }Output 1Enter a number to check. -2 Number = -2 The if statement in C programming is easy.When user enters -2 then, the test expression (num<0) becomes true. Hence, Number = -2 is displayed in the screen.Output 2Enter a number to check. 5 The if statement in C programming is easy.When the user enters 5 then, the test expression (num<0) becomes false. So, the statement/s inside body of if is skipped and only the statement below it is executed.C if...else statementThe if...else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false.Syntax of if...elseif (test expression) { statements to be executed if test expression is true; } else { statements to be executed if test expression is false; }Flowchart of if...else statementExample 2: C if...else statementWrite a C program to check whether a number entered by user is even or odd#include <stdio.h> int main(){ int num; printf("Enter a number you want to check.\n"); scanf("%d",&num); if((num%2)==0) //checking whether remainder is 0 or not. printf("%d is even.",num); else printf("%d is odd.",num); return 0; }Output 1Enter a number you want to check. 25 25 is odd.Output 2Enter a number you want to check. 2 2 is even.Nested if...else statement (if...elseif....else Statement)The nested if...else statement is used when program requires more than one test expression.Syntax of nested if...else statement.if (test expression1){ statement/s to be executed if test expression1 is true; } else if(test expression2) { statement/s to be executed if test expression1 is false and 2 is true; } else if (test expression 3) { statement/s to be executed if text expression1 and 2 are false and 3 is true; } . . . else { statements to be executed if all test expressions are false; } How nested if...else works?The nested if...else statement has more than one test expression. If the first test expression is true, it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true, it executes the statement/s inside the braces{ } just below it. This process continues. If all the test expression are false, code/s inside else is executed and the control of program jumps below the nested if...elseThe ANSI standard specifies that 15 levels of nesting may be continued.Example 3: C nested if else statementWrite a C program to relate two integers entered by user using = or > or < sign.#include <stdio.h> int main(){ int numb1, numb2; printf("Enter two integers to check".\n); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) //checking whether two integers are equal. printf("Result: %d = %d",numb1,numb2); else if(numb1>numb2) //checking whether numb1 is greater than numb2. printf("Result: %d > %d",numb1,numb2); else printf("Result: %d > %d",numb2,numb1); return 0; } Output 1Enter two integers to check. 5 3 Result: 5 > 3 Output 2Enter two integers to check. -4 -4 Result: -4 = -4

 

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. If a programmar has to choose one among many alternatives if...else can be used but, this makes programming logic complex. This type of problem can be handled in C programming using switch...case statement.Syntax of switch...caseswitch (expression) { case constant1: codes to be executed if expression equals to constant1; break; case constant2: codes to be executed if expression equals to constant3; break; . . . default: codes to be executed if expression doesn't match to any cases; } In switch...case, expression is either an integer or a character. If the value of switch expression matches any of the constant in case, the relevant codes are executed and control moves out of the switch...case statement. If the expression doesn't matches any of the constant in case, then the default statement is executed.Example of switch...case statementWrite a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and perform the corresponding calculation on the operands. /* C program to demonstrate the working of switch...case statement */ /* Program to create a simple calculator for addition, subtraction, multiplication and division */ # include <stdio.h> int main() { char o; float num1,num2; printf("Enter operator either + or - or * or divide : "); scanf("%c",&o); printf("Enter two operands: "); scanf("%f%f",&num1,&num2); switch(o) { case '+': printf("%.1f + %.1f = %.1f",num1, num2, num1+num2); break; case '-': printf("%.1f - %.1f = %.1f",num1, num2, num1-num2); break; case '*': printf("%.1f * %.1f = %.1f",num1, num2, num1*num2); break; case '/': printf("%.1f / %.1f = %.1f",num1, num2, num1/num2); break; default: /* If operator is other than +, -, * or /, error message is shown */ printf("Error! operator is not correct"); break; } return 0; }OutputEnter operator either + or - or * or divide : * Enter two operands: 2.3 4.5 2.3 * 4.5 = 10.3Notice break statement at the end of each case, which cause switch...case statement to exit. If break statement are not used, all statements below that case statement are also executed.

MY BUTTON
DOWNLOADS
EXAMPLES
SEE ALSO
REPORT AN ERROR
UNION
PREPROCESSOR
STRUCTURE
FUNCTION
ARRAY
STORAGE CLASS
TYPE QUALIFIER
CASE CONTROL
LOOP
DECISION
OPERATORS
VARIABLE
CONSTANT

© 2014 by KAILASH LALE.

FAVORITES

FOLLOW US ON

CONTACT ME

Tel: +91 8446823181
Skype: deathreveals
Mail: shareandlearnhub@gmail.com

bottom of page