Skip to main content

C if. .else Statement.

C if. .else Statement.


C if Statement


 The syntax of the if statement in C programming is:

Syntax :

if (test expression) {
// statements to be executed if the test expression is true
}


How if statement works?

1: The if statement evaluates the test expression inside the parenthesis ().

2: If the test expression is evaluated to true, statements inside the body of if are executed.

3: If the test expression is evaluated to false, statements inside the body of if are not executed.


Example 1: if statement

#include <stdio.h> 

void main()
{
int x,y;

printf("Enter the Value of x: ");
scanf("%d",&x);

printf("Enter the Value of y: ");
scanf("%d",&y);

if (x<y) {
printf("\n%d is less than %d",x,y);
}
}

OUTPUT:-

Enter the Value of x: 10
Enter the Value of y: 20

10 is less than 20


  Program File:(click the following .c file to get the program file)    
 
                      If-statement.c




C if...else Statement

 

  The if statement may have an optional else block. The syntax of the if..else statement is:

Syntax :
 
 if (test expression) {

// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}


How if...else statement works?

1: If the test expression is evaluated to true,

statements inside the body of if are executed.

statements inside the body of else are skipped from execution.

2: If the test expression is evaluated to false,

statements inside the body of else are executed

statements inside the body of if are skipped from execution.


Example 2: if..else statement

#include <stdio.h>
void main () {
int a;
printf("Enter the Value of a: ");
scanf("%d",&a);

/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("%d is less than 20\n",a);
}
else {
/* if condition is false then print the following */
printf("%d is not less than 20\n",a);
}
}

OUTPUT:-
   
  Output 1:

    Enter the Value of a: 10
       10 is less than 20

Output 2:

    Enter the Value of a: 100
       100 is not less than 20

 
 Program File:(click the following .c file to get the program file)    

               If-else-statement.c
 


C if...else Ladder

 

 Syntax of if...else Ladder:

  if (test expression1) {

// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
} . .
else {
// statement(s)
}


Example 3: C if...else Ladder 

#include<stdio.h>

void main(){

int n;

printf("Enter the Value of n: ");
scanf("%d",&n);

if(n<18){
printf("\nYou are not able for driving");
}
else if(n>18){
printf("\nYou are able for driving");
}
else{
printf("\nInvalid choice");
}
}

OUTPUT:-
   
 Output 1:

Enter the Value of n: 25

You are able for driving


 Output 2:

Enter the Value of n: 10

You are not able for driving


Program File:(click the following .c file to get the program file)    
              If-else-ladder.c

Comments

Popular posts from this blog

Logical Reasoning.

  Quiz 2:- 2.In a code language QUEEN is written as OVCFL, then KING is written as a. IJLH b. MKOF c. PHIK d. FOKM Answer:-  a. IJLH Explanation: Alphabet Series - A B C D E F G H I J K L M N O P Q R S T U V W X Y Z The coding follows the rule -2, +1, -2, +1, etc. That means Q -2 = O U+1=V E-2=C, etc.

Logical Reasoning

Quiz 4:-   4. If DOOR = 25, LOWER=37, TOWER=18, then OVER = ? a.85 b.60 c.45 d.06 Answer:- d.06 Explanation:- Alphabets and their positions |A|B|C|D|E|F|G|H|I|J| | K|L|M|N|O|P|Q|R| |S|T |U|V|W|X|Y|Z| |1|2 |3|4 |5|6|7 |8|9|10| |11|12|13|14|15|16|17|18| |19|20|21|22|23|24|25|26 | Rule:- Add positions of alphabets and then reverse the result DOOR = 4+15+15+18 = 52 = reverse 52 to give 25 and so on...

Logical Reasoning

  Quiz 7 7. CEGI : JHFD :: KMOQ : ? a. LPNR b. RNPL c. LNPR d. RPNL Answer:  d. RPNL Explanation: Alphabet Series:- A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Write the next letters after the letters CEGI. We get DFHJ. Reverse them to get JHFD Next letters after KMOQ is LNPR. Reversing them we get RPNL