Saturday, October 3, 2015

Tutorial Decision or Branch in C Programming Langauge

We can make some decisions with C programming language. The decisions in C programming language used to execute some codes and ignore some codes depending on the given condition is true or false. Now, I will tell you how to make decisions or branch in C programming language. We can make decision with if, if...else, and switch...case.

If statement
Basic If syntax : 
   
          if(text expression) {
         statement to be executed if it is true;
         }
       

For example:

Try the code. If the statement is true then it will show the result.


If...else Statement
Basic If...else syntax :

          If (text expression) {
            statements to be executed if expression is true;
        }
        else {
            statements to be executed if expression is false;
        }
        

For example:


Try the code. If the result of num divided by 2 not zero than the num is odd.


If the result of num divided by 2 is zero, then the num is even.


Switch...case Statement
Basic switch...case syntax:

          switch(text expression) {
          case 1:
                code to be executed if text expression equals to case 1;
                break;
          case 2:
                code to be executed if text expression equals to case 2;
                break;
                .
                .
                .
                .
          default:
               code to be executed if text expression doesn't equals to any cases;
          }


That's the explanation about decision and branch in C programming language. Now, you can try your own code to make decision and branch. Have a great time with coding!

No comments:

Post a Comment