Saturday, 15 August 2020

What are conditional statements and what are the uses of conditional sentences ?

 What are conditional statements and what are the uses of conditional sentences ? 



So, here we’ll study about the conditional statements and also we’ll see the uses of these conditional statements. 


We’ll try to understand the conditional sentences by taking the examples of nested if else statements and switch-case. 


While programming in any language we often suffer with the complex problems in our program 


To overcome with the difficulty of these complex problems and to reduce the complexity of our program conditional statements are used  


Below we’ll understand the conditional statement with the help of nested if else and switch case 


  1. Nested if-else :- 


In nested if-else condition two or more than two conditions are checked the syntax of nested if-else is given below


Syntax :- 

if ( condition 1 ) {  code block

                if ( condition 2 ) { code. block }

                     else { code block }

                }


  • Nesting in ternary operators :- 


syntax :- 

variable = ( condition 1) ? ( condition 2 ) ? exp 1 ; exp 2

( condition 3 ) ? exp 3 ; exp 4 ;


e.i. :- result = a > b ? a > c ? a : c ; b > c ? b : c ;


  1. Switch case :-

 

Switch case are used to check or to implement various cases in a program 

it is particularly used to switch the various expressions in our program

let’s try to understand the use of switch case with the help of syntax below

 

syntax :- 

            switch (exp) {

              case value 1;

                 // statement 

                  break; 

                   case value2; 

                    // statements

                     break;

                      ------

                       default; 

                         //statements

                   }


  • With the help of switch case we can also print multiple statements in our program :-


So to print multiple statement within one input we have to escape the use of “break” after the completion of one condition 

In this manner we can easily print multiple statements in our program. 

e.i. :-

       switch (case)

          case1:

           case2:

            case3:

          statement

          break; 

 

With the above illustrated example one can easily understand the method to print multiple statement with single input 

also in the above illustrated example you have noticed that there is no break statement in between the case 


I hope the above mentioned information helps you to understand the conditional statements

stay connected with us for such useful information also share these informations to the needy  

Thankyou


BY :- ADARSH GUPTA







No comments:

Post a Comment

Linking Words/ Conjunctions

  Linking Words/ Conjunctions Conjunctions are words that link other words, phrases, or clauses together. Kinds of conjunctions Conjunctions...