Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

05 C Programming

C Programming lec -5 notes

Uploaded by

pnkulkarni05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

05 C Programming

C Programming lec -5 notes

Uploaded by

pnkulkarni05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

C PROGRAMMING

DECISION CONTROL STATEMENTS


(CONDITIONAL STATEMENT)
INDEX
QUIZ

1 If Statement

2 If-else Statement

3 If else-if

4 Switch Statement

5 Ternary Operator
C if else Statement
 The if statement in C language is used to perform
operation on the basis of condition.

 The single if statement in C language is used to


execute the code if condition is true.

Syntax
#include<stdio.h>
#include<conio.h>

void main(){
int number=0;

printf("enter a number:");
scanf("%d",&number);

if(number%2==0){
printf("%d is even number",number);

getch();

}
OUTPUT
IF-ELSE STATEMENT

 The if-else statement in C language is used to execute


the code if condition is true or false.
#include<stdio.h>
#include<conio.h>

void main(){
int number=0;

printf("enter a number:");
scanf("%d",&number);

if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
getch();
}
OUTPUT
IF ELSE-IF STATEMENT

 The if else-if statement is used to execute one


code from multiple conditions.
#include<stdio.h>
#include<conio.h>

void main(){
int number=0;

printf("enter a number:");
scanf("%d",&number);

if(number==10){
printf("number is equals to 10");
}

else if(number==50){
printf("number is equal to 50");
}

else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}

getch();
}
OUTPUT
C SWITCH STATEMENT

 The switch statement in C language is used to execute


the code from multiple conditions.

 It is like if else-if statement.


syntax
#include<stdio.h>
#include<conio.h>
void main(){
int number=0;

printf("enter a number:");
scanf("%d",&number);

switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
getch();
}
Output
Ternary Operator

 The ternary operator is an operator that takes


three arguments.

 It can be represented with ? : .

 It is also called as conditional operator.

 It is shortened way of writing an if-else statement.


syntax

expression-1 ? expression-2 : expression-3


condition execute if true execute if false

In the above symbol expression-1 is condition and expression-2 and


expression-3 will be either value or variable or statement or any
mathematical expression.

If condition will be true expression-2 will be execute otherwise


expression-3 will be executed.
#include<stdio.h>
#include<conio.h>

void main()
{

int a;

printf("Enter any number: ");


scanf("%d",&a);

(a % 2 ==0) ? printf("Even Number : %d",a) : printf("Odd Number : %d",a);

getch();

}
OUTPUT
*****************************************************

You might also like