Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IF => Keyword
- Normal Code ----> Above If Always Executed
- if(condition (Statement) )
- {
- body of if; --> This part is Execute when Condition (Statement) is True 1 (Non-Zero)
- --> This part is Skipped when Condition (Statement) is False 0
- }
- Normal Code ----> Below If Always Executed
- Notes:
- If Condition Construct :
- Based on Some condition (Statement) Some part of Code is Executed.
- Based on Some condition (Statement) Some part of Code is Skipped to achieve
- this provision we need to go for if condition construct.
- void main()
- {
- printf("A");
- printf("B");
- if(3>2) Condition True
- {
- printf("C");
- printf("D");
- }
- printf("E");
- printf("F");
- } Output : ABCDEF
- void main()
- {
- printf("A");
- printf("B");
- if(3>20) Condition False
- {
- printf("C");
- printf("D");
- }
- printf("E");
- printf("F");
- } O/P : ABEF
- void main()
- {
- printf("A");
- printf("B");
- if( )
- {
- printf("C");
- printf("D");
- }
- printf("E");
- printf("F");
- } o/p : Error
- void main()
- {
- printf("A");
- printf("B");
- if(3)
- {
- printf("C");
- printf("D");
- }
- printf("E");
- printf("F");
- } O/P : ABCDEF
- void main()
- {
- int a=89;
- printf("A");
- if(a)
- {
- printf("C");
- }
- printf("E");
- } O/P: ACE
- void main()
- {
- int a=89;
- printf("A");
- if(a)
- {
- printf("C");
- }
- printf("E");
- } O/P ACE
- Example
- void main()
- {
- printf("A");
- if(!2+7)
- {
- printf("B");
- }
- printf("C");
- } O/P : ABC
- --------------
- void main()
- {
- printf("A");
- if(Condition Follow below)
- {
- printf("B");
- }
- printf("C");
- }
- Condition() O/P
- 33 ABC
- 0 AC
- 2+8 ABC
- 2-2.0 AC
- !6-!4 AC
- 5.9 ABC
- 7.8<=7.1 AC
- 9>9.9 AC
- void main()
- {
- printf("A");
- if(3>20)
- printf("B");
- printf("C");
- } O/P : AC
- ----> If Programmer does not provide {} body, then compiler by default
- consider the First Semi-colon (;) as end of IF BODY
- void main()
- {
- } This is a Valid Program
- void main()
- {
- ;
- } This is also a valid program
- void main()
- {
- printf("A");
- printf("B");
- if(5>64); ----> End of IF Body
- printf("C");
- printf("D");
- printf("E");
- } O/P : ABCDE
- If we provide Semicolon after If We will not get Error.
- WAP to find Max of 2 int using (IF condition construct)
- void main()
- {
- int a,b,max;
- if(a>b)
- { max=a;
- }
- if(b>a)
- { max=b;
- }
- printf(" %d",max);
- }
- WAP to find Max of 2 int using Only one (IF condition construct)
- void main()
- {
- int a=1,b=3,max;
- max=b; 3
- if(a>b)
- {
- max=a;
- }
- printf("%d",max); 3
- }
- WAP to find Max of 2 int using Only one (IF condition construct) and 2 Variable
- void main()
- {
- int a, b;
- scanf("%d %d", &a , &b);
- if(b>a)
- {
- a=b;
- }
- printf("%d", a);
- }
- WAP to find Max of 2 int using Only one (IF condition construct) and 2 Variable without losing values.
- void main()
- {
- int a, b;
- scanf("%d %d", &a , &b);
- if(b>a)
- {
- printf("%d", b);
- exit(0); Stops the program return;
- }
- printf("%d", a);
- }
- ------------------------------------------------------------------
- void main()
- {
- printf("A");
- printf("B");
- if(0)
- {
- ;
- }
- printf("C");
- printf("D");
- printf("E");
- } O/P = ABCDE
- 1. void main()
- {
- printf("A");
- if(5>64)
- printf("B");
- printf("C");
- }
- 2. void main()
- {
- printf("A");
- if(5>64)
- {
- printf("B");
- }
- printf("C");
- }
- 3. void main(){printf("A"); if(5>64) printf("B");printf("C");}
- ---------------IF ELSE------------------
- IF(statement)
- { IF BODY
- }
- else
- { ELSE BODY
- }
- Write A Program to find greatest of 2 int using IF ELSE
- void main()
- {
- int a,b;
- scanf("%d %d",&a,&b);
- if(a>b)
- {
- printf("%d",a);
- }
- else
- {
- printf("%d",b);
- }
- }
- Write A Program to find greatest of 3 int using IF ELSE
- void main()
- {
- int a,b,c;
- if(a>b)
- {
- if(a>c)
- {
- printf("%d",a);
- }
- }
- else
- {
- if(b>c)
- {
- printf("%d",b);
- }
- else
- {
- printf("%d",c);
- }
- }
- }
- if(a>b)
- if(a>c)
- printf("%d",a);//// if(a>c)
- else //// if(a>c)
- if(b>c)
- printf("%d",b);////// if(b>c)
- else ////if(b>c)
- printf("%d",c); else
- ==============Actual Code
- if(x)
- if(y)
- printf("A"); -----if(y)
- else -----if(y)
- printf("B"); -----else
- ---------------------------11111111111111111111111111111--------
- if(x)
- {
- if(y)
- {
- printf("A");
- }
- }
- else
- {
- printf("B");
- }
- ---------------------------22222222222222222222222222222-----------------------------
- if(x)
- {
- if(y)
- {
- printf("A");
- }
- else
- {
- printf("B");
- }
- } Correct
- ***************************************************************************
- void main()
- { ------>Not Valid
- if(3>2) ======> If Needs a statement. Conditional Construct needs a statement
- }
- ************************************************************************
- void main()
- {
- } ----->Valid
- -------------------------------------------------------
- void main()
- {
- if(2+2) ----->Valid
- printf("A"); =====>expression in some value Zero - False Non-Zero- True
- }
- ---------------------------------
- void main()
- {
- if(!2+3) ----->Valid
- printf("A"); -----> O/P: A
- }
- --------------------------------
- void main()
- {
- if(1) No of Statement : 2
- printf("A"); No of Executable Statement : 1
- else
- printf("B");
- }
- --------------------------
- void main()
- {
- if(12)
- {
- if(2==2)
- {
- printf("A");
- }
- else /////Belongs to if(2==2)
- {
- printf("B");
- }
- }
- else /////if(12)
- {
- }
- }
- Note: Else always belongs to the nearby if body.
- if(x)
- if(t)
- printf("A"); printf("A"); =>belongs to if(t)
- whole if(t) =>if(x)
- ------------------------------------------------------
- void main()
- {
- 4-9; ----> -5;
- 2+3; ====> 5
- }
- Operator does
- 1. Does its operation
- 2. Replace that expression with its result.
- ------------------------------------------------------
- int x=10;
- if(_____)
- printf("Hello %d",x);
- else
- printf("Hai %d",x);
- if O/P
- x==10 Hello 10
- x=0 Hai 0
- x=1 Hello 1
- x==5 Hai 10
- x=5 Hello 5
- x Hello 10
- -------------------------------------
- void main()
- {
- int x; ==> GV stored in x.
- if(x)
- printf("Hello %d ",x);
- else
- printf("Hai %d ",x);
- }
- O/P
- x==GV ==> Zero Hai 0
- x==GV ==> Non Zero Hello GV
- ************************************************
- void main()
- {
- int x;
- if(x = 0.5) ////// x=0
- printf("Hello %d",x);
- else
- printf("Hai %d",x); Variable Convert the data type FS (%d %c ) Does not convert data type
- } O/P : Hai 0
- ----------------------------------------------------
- int a,b;
- float c,d;
- a b c d
- a=b=c=d=7.89; 7 7 7.89 7.89
- d=c=b=a=7.89; 7 7 7.0 7.0
- --------------------------------------------------------
- void main()
- {
- printf("A");
- if(23>4)
- printf("B");
- printf("C"); ///// Else body Must be very after of IF body ends
- else error: โelseโ without a previous โifโ
- printf("D");
- printf("E");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement