Advertisement
Mukezh

Session 6 If Statement

Dec 11th, 2020 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. IF => Keyword
  2.  
  3.  
  4. Normal Code ----> Above If Always Executed
  5. if(condition (Statement) )
  6. {
  7. body of if; --> This part is Execute when Condition (Statement) is True 1 (Non-Zero)
  8. --> This part is Skipped when Condition (Statement) is False 0
  9. }
  10. Normal Code ----> Below If Always Executed
  11.  
  12. Notes:
  13. If Condition Construct :
  14. Based on Some condition (Statement) Some part of Code is Executed.
  15. Based on Some condition (Statement) Some part of Code is Skipped to achieve
  16. this provision we need to go for if condition construct.
  17.  
  18. void main()
  19. {
  20. printf("A");
  21. printf("B");
  22. if(3>2) Condition True
  23. {
  24. printf("C");
  25. printf("D");
  26. }
  27. printf("E");
  28. printf("F");
  29. } Output : ABCDEF
  30.  
  31. void main()
  32. {
  33. printf("A");
  34. printf("B");
  35. if(3>20) Condition False
  36. {
  37. printf("C");
  38. printf("D");
  39. }
  40. printf("E");
  41. printf("F");
  42. } O/P : ABEF
  43.  
  44. void main()
  45. {
  46. printf("A");
  47. printf("B");
  48. if( )
  49. {
  50. printf("C");
  51. printf("D");
  52. }
  53. printf("E");
  54. printf("F");
  55. } o/p : Error
  56.  
  57. void main()
  58. {
  59. printf("A");
  60. printf("B");
  61. if(3)
  62. {
  63. printf("C");
  64. printf("D");
  65. }
  66. printf("E");
  67. printf("F");
  68. } O/P : ABCDEF
  69.  
  70. void main()
  71. {
  72. int a=89;
  73. printf("A");
  74. if(a)
  75. {
  76. printf("C");
  77. }
  78. printf("E");
  79. } O/P: ACE
  80.  
  81. void main()
  82. {
  83. int a=89;
  84. printf("A");
  85. if(a)
  86. {
  87. printf("C");
  88. }
  89. printf("E");
  90. } O/P ACE
  91. Example
  92. void main()
  93. {
  94. printf("A");
  95. if(!2+7)
  96. {
  97. printf("B");
  98. }
  99. printf("C");
  100. } O/P : ABC
  101. --------------
  102. void main()
  103. {
  104. printf("A");
  105. if(Condition Follow below)
  106. {
  107. printf("B");
  108.  
  109. }
  110. printf("C");
  111. }
  112.  
  113. Condition() O/P
  114.  
  115. 33 ABC
  116. 0 AC
  117. 2+8 ABC
  118. 2-2.0 AC
  119. !6-!4 AC
  120. 5.9 ABC
  121. 7.8<=7.1 AC
  122. 9>9.9 AC
  123.  
  124.  
  125. void main()
  126. {
  127. printf("A");
  128. if(3>20)
  129. printf("B");
  130. printf("C");
  131. } O/P : AC
  132. ----> If Programmer does not provide {} body, then compiler by default
  133. consider the First Semi-colon (;) as end of IF BODY
  134.  
  135. void main()
  136. {
  137. } This is a Valid Program
  138.  
  139. void main()
  140. {
  141. ;
  142. } This is also a valid program
  143.  
  144. void main()
  145. {
  146. printf("A");
  147. printf("B");
  148. if(5>64); ----> End of IF Body
  149. printf("C");
  150. printf("D");
  151. printf("E");
  152. } O/P : ABCDE
  153.  
  154. If we provide Semicolon after If We will not get Error.
  155.  
  156. WAP to find Max of 2 int using (IF condition construct)
  157.  
  158. void main()
  159. {
  160. int a,b,max;
  161. if(a>b)
  162. { max=a;
  163. }
  164. if(b>a)
  165. { max=b;
  166. }
  167. printf(" %d",max);
  168. }
  169.  
  170. WAP to find Max of 2 int using Only one (IF condition construct)
  171.  
  172. void main()
  173. {
  174. int a=1,b=3,max;
  175. max=b; 3
  176. if(a>b)
  177. {
  178. max=a;
  179. }
  180. printf("%d",max); 3
  181. }
  182. WAP to find Max of 2 int using Only one (IF condition construct) and 2 Variable
  183. void main()
  184. {
  185. int a, b;
  186. scanf("%d %d", &a , &b);
  187.  
  188. if(b>a)
  189. {
  190. a=b;
  191. }
  192. printf("%d", a);
  193. }
  194. WAP to find Max of 2 int using Only one (IF condition construct) and 2 Variable without losing values.
  195.  
  196. void main()
  197. {
  198. int a, b;
  199. scanf("%d %d", &a , &b);
  200.  
  201. if(b>a)
  202. {
  203. printf("%d", b);
  204. exit(0); Stops the program return;
  205. }
  206. printf("%d", a);
  207. }
  208. ------------------------------------------------------------------
  209. void main()
  210. {
  211. printf("A");
  212. printf("B");
  213. if(0)
  214. {
  215. ;
  216. }
  217. printf("C");
  218. printf("D");
  219. printf("E");
  220. } O/P = ABCDE
  221.  
  222. 1. void main()
  223. {
  224. printf("A");
  225. if(5>64)
  226. printf("B");
  227. printf("C");
  228. }
  229. 2. void main()
  230. {
  231. printf("A");
  232. if(5>64)
  233. {
  234. printf("B");
  235. }
  236. printf("C");
  237. }
  238. 3. void main(){printf("A"); if(5>64) printf("B");printf("C");}
  239.  
  240.  
  241. ---------------IF ELSE------------------
  242. IF(statement)
  243. { IF BODY
  244. }
  245. else
  246. { ELSE BODY
  247. }
  248.  
  249. Write A Program to find greatest of 2 int using IF ELSE
  250. void main()
  251. {
  252. int a,b;
  253. scanf("%d %d",&a,&b);
  254.  
  255. if(a>b)
  256. {
  257. printf("%d",a);
  258. }
  259. else
  260. {
  261. printf("%d",b);
  262. }
  263. }
  264. Write A Program to find greatest of 3 int using IF ELSE
  265.  
  266. void main()
  267. {
  268. int a,b,c;
  269.  
  270. if(a>b)
  271. {
  272. if(a>c)
  273. {
  274. printf("%d",a);
  275. }
  276. }
  277. else
  278. {
  279. if(b>c)
  280. {
  281. printf("%d",b);
  282. }
  283. else
  284. {
  285. printf("%d",c);
  286. }
  287.  
  288. }
  289. }
  290.  
  291. if(a>b)
  292. if(a>c)
  293. printf("%d",a);//// if(a>c)
  294. else //// if(a>c)
  295. if(b>c)
  296. printf("%d",b);////// if(b>c)
  297. else ////if(b>c)
  298. printf("%d",c); else
  299.  
  300. ==============Actual Code
  301. if(x)
  302. if(y)
  303. printf("A"); -----if(y)
  304. else -----if(y)
  305. printf("B"); -----else
  306. ---------------------------11111111111111111111111111111--------
  307. if(x)
  308. {
  309. if(y)
  310. {
  311. printf("A");
  312. }
  313. }
  314. else
  315. {
  316. printf("B");
  317. }
  318. ---------------------------22222222222222222222222222222-----------------------------
  319. if(x)
  320. {
  321. if(y)
  322. {
  323. printf("A");
  324. }
  325. else
  326. {
  327. printf("B");
  328. }
  329. } Correct
  330. ***************************************************************************
  331.  
  332. void main()
  333. { ------>Not Valid
  334. if(3>2) ======> If Needs a statement. Conditional Construct needs a statement
  335. }
  336. ************************************************************************
  337. void main()
  338. {
  339. } ----->Valid
  340. -------------------------------------------------------
  341. void main()
  342. {
  343. if(2+2) ----->Valid
  344. printf("A"); =====>expression in some value Zero - False Non-Zero- True
  345. }
  346. ---------------------------------
  347. void main()
  348. {
  349. if(!2+3) ----->Valid
  350. printf("A"); -----> O/P: A
  351. }
  352. --------------------------------
  353.  
  354. void main()
  355. {
  356. if(1) No of Statement : 2
  357. printf("A"); No of Executable Statement : 1
  358. else
  359. printf("B");
  360. }
  361. --------------------------
  362. void main()
  363. {
  364. if(12)
  365. {
  366. if(2==2)
  367. {
  368. printf("A");
  369. }
  370. else /////Belongs to if(2==2)
  371. {
  372. printf("B");
  373. }
  374. }
  375. else /////if(12)
  376. {
  377. }
  378. }
  379. Note: Else always belongs to the nearby if body.
  380.  
  381. if(x)
  382. if(t)
  383. printf("A"); printf("A"); =>belongs to if(t)
  384. whole if(t) =>if(x)
  385. ------------------------------------------------------
  386. void main()
  387. {
  388. 4-9; ----> -5;
  389. 2+3; ====> 5
  390. }
  391. Operator does
  392. 1. Does its operation
  393. 2. Replace that expression with its result.
  394. ------------------------------------------------------
  395. int x=10;
  396. if(_____)
  397. printf("Hello %d",x);
  398. else
  399. printf("Hai %d",x);
  400.  
  401. if O/P
  402. x==10 Hello 10
  403. x=0 Hai 0
  404. x=1 Hello 1
  405. x==5 Hai 10
  406. x=5 Hello 5
  407. x Hello 10
  408. -------------------------------------
  409. void main()
  410. {
  411. int x; ==> GV stored in x.
  412. if(x)
  413. printf("Hello %d ",x);
  414. else
  415. printf("Hai %d ",x);
  416. }
  417. O/P
  418. x==GV ==> Zero Hai 0
  419. x==GV ==> Non Zero Hello GV
  420. ************************************************
  421. void main()
  422. {
  423. int x;
  424. if(x = 0.5) ////// x=0
  425. printf("Hello %d",x);
  426. else
  427. printf("Hai %d",x); Variable Convert the data type FS (%d %c ) Does not convert data type
  428. } O/P : Hai 0
  429. ----------------------------------------------------
  430.  
  431. int a,b;
  432. float c,d;
  433. a b c d
  434. a=b=c=d=7.89; 7 7 7.89 7.89
  435. d=c=b=a=7.89; 7 7 7.0 7.0
  436. --------------------------------------------------------
  437.  
  438. void main()
  439. {
  440. printf("A");
  441. if(23>4)
  442. printf("B");
  443. printf("C"); ///// Else body Must be very after of IF body ends
  444. else error: โ€˜elseโ€™ without a previous โ€˜ifโ€™
  445. printf("D");
  446. printf("E");
  447. }
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement