mobileNavlogo
headerlogo

Control flow statements in C

21 September, 2018

Ratings

Related Blogs

Control flow statements in C Language in Detail:-

Control flow statements always control the   execution flow of the program.

  • Control flow statements are classified in to three types.

i.e

  1. Selection statement :-2 eg :-if,else,elseif,switch
  2. Interaction statements:- 2 eg:- While,for,do-while
  3. Jumping statements:- 2 eg:- break,continue,goto

  1. Selection statement :- Selection statements are also called as decision making statements

  • When we are working with selection statements, if the condition is true, then control will pass within the body, if it is false control will pass outside the body

Syntax to if: If (condition) { Statement 1; Statement 2; Statement 3; ---------------------- ----------------------- }  

  • Constructing the body is always optional
  • For a single statement, doesn’t require to specify the body
  • If the body is not specified, then automatically,

Condition part will terminals with next semicolon(;)       Else :- It is a keyword, by using this keyword, we can create alternate block for if condition.

  • Using else is always optional, it is recommended to use, when we are having alternate block.
  • When we are working with if and else, among those two blocks, only one block will be executed.
  • Else block will be executed whenever the if condition i.e false

 

  • If(condition)
{ Statement 1; Statement 2; } Else { Statement 3; Statement 4; }         Void main() { Printf(“a”); Printf(“b”); If(5>2!=1) { Printf(“welcome”); Printf(“Hello”); } Printf(“c”); Printf(“D”); o/p: ABCD   false, so it doesn’t execute the satatements which are present inside the block
  • If(condition)
Statement1; Else Statement 2;   4) if(condition) Statements; Else { Statement2; Statement3; }
  • If (condition)
{ Statement1; Statement2; } Else Statement3;  

    Generally when we are working with constant expressions, we will get warning message from compiler i.e condition is always false or true.

  • Warnings can be ignored automatically at the time of execution.

 

Void main()

Printf(“NIT”); Printf(“welcome”); If(15!=5!=0)     // if(0!=5!=0) // if(1!=0) { Printf(“A”); Printf(“B”); } Printf(“c”); } o/p:-NIT welcome ABC        “Every non Zero is true”        

  • Void main()

{ Printf(“welcome”); Printf(“NIT”); If(!5) Printf(“A”); Printf(“B”); Printf(“c”);   O/P:  WelcomeNITBC.   When we are constructing multiple statements without using body then first statement only will consider within the “if”, because scope will be terminated with next semicolon.

  • Void main()

{ Printf(“A”); If(5>2 && 8<5) // if(1 && 0) { Printf(“B”); Printf(“c”); } Else { Printf(“welcome”); Printf(“Hello”); } Printf(“D”); } o/p: a welcome Hello    

  • Void main()

Printf(“welcome”); If(1<2!=0 && 1=5>2)   // if(1!=0 && 1==1) { Printf(“A”); Printf(“B”); } Else { Printf(“x”); Printf(“y”); } Printf(“NIT”); }   o/p:- Welcome AB NIT.    

  • Void main()

{ Printf(“A”); If(8!=8>2 or 4!=1<4) { Printf(“B”); Printf(“C”); } Else Printf(“NIT”); Printf(“Y”); Printf(“Z”); } o/p: ABCYZ.   When the body is not specified for else part then automatically scope will terminated with next statement only. Ie. Under the else part, first statement will place  

  • Void main()

Printf(“Hello”); If(15!=2>5) Printf(“A”); Printf(“B”);     on necessary because else should start at the else                 immediate ending of if statement. If is not {                       having the body. Printf(“X”); Printf(“Y”); } } o/p:  Error misplaced error   when we are using the else part, it should start immediately of if scope only i.e where the scope of the if is terminated there itself.   Else part must be started.

About Author
Authorlogo
Name

TekSlate

Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .