Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
128 views
in Control Statements by (29.8k points)
closed by

Explain conditional statements in detail?

1 Answer

+1 vote
by (29.6k points)
selected by
 
Best answer

1. Simple if: 

The syntax is given below if(expression) 

statement;

or 

if(expression) 

Block of statements

First expression evaluates if it is true then only statement will be executed. 

eg: if (n>0) 

cout<<n<<"is positive";

2. if else: 

The syntax is given below, if (expression)

statement 1; 

else 

statement 2; 

or 

if (expression) 

statement block 1; 

else 

statement block 2; 

First expression evaluates if it is true statement block 1 will be executed otherwise statement block 2 will be executed. Only one block will be executed at a time so it is called branching statement. 

eg: 

if (n>0) 

cout<<n<<"is positive";

else

cout<<n<<"is negative";

3. if else if ladder: 

The syntax will be given below 

if (expression!) 

statement block 1; 

else if (expression 2) 

statement block 2; 

else if (expression 3) 

statement block 3;

else 

statement block n; 

}

Here first expression 1 will be evaluated if it is true only the statement blockl will be executed otherwise expression 2 will be executed if it is true only the statement block 2 will be executed and so on. If all the expression evaluated is false then only statement block n will be evaluated .

eg: 

If (mark>=90) 

cout<<"Your grade is A+";

else if(mark>=80)

cout<<"Your grade is A";

else if(mark>=70)

cout<<"Your grade is B+";

else if(mark>=60)

cout<<"Your grade is B";

else if(mark>=50)

cout<<"Your grade is C+";

else if(mark>=40)

cout<<"Your grade is C";

else if(mark>=30)

cout<<"Your grade is D+";

else

cout<<"Your grade is D";

4. conditional operator: 

It is a ternary operator and it is an alternative for if else construct. 

The syntax is given below. 

expression 1? expression 2: expression 3;

or 

expression 1? Value if true : value if false;

Here 

expression 1 will be evaluated if it true

expression 2 will be executed otherwise 

expression 3 will be executed. 

eg:

n>0?cout<<n<<"is positive":cout<<n<<"is negative";

5. Switch: 

It is a multiple branch statement. Its syntax is given below. 

switch(expression) 

case value: statements;break; 

case value: statements;break; 

case value: statements;break; 

case value: statements;break; 

case value: statements;break; 

…….. 

default : statements; 

}

First expression evaluated and selects the statements with matched case value. 

eg: 

switch (n)

case 1: cout<< “Sunday”;break; 

case 2: cout<< “Monday”;break;

case 3: cout<< “Tuesday”;break; 

case 4: cout<< “Wedesday”;break; 

case 5: cout<< “Thursday”;break; 

case 6: cout<< “Friday”;break; 

case 7: cout<< “Saturday”;break; 

default : cout<< “lnvalid”

}

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

...