-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranching.html
More file actions
506 lines (427 loc) · 17.4 KB
/
branching.html
File metadata and controls
506 lines (427 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>CodingElite</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navm">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">CodingElite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/codingelite.org/blog.html">Blog</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link active dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
C Tutorials
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/codingelite.org/introduction.html">introduction</a></li>
<li><a class="dropdown-item" href="/codingelite.org/branching.html">Branching Statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/looping.html">looping statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/function.html">Function</a></li>
<li><a class="dropdown-item" href="/codingelite.org/array.html">Array</a></li>
<li><a class="dropdown-item" href="/codingelite.org/string.html">string</a></li>
<li><a class="dropdown-item" href="/codingelite.org/pointer.html">pointers</a></li>
<li><a class="dropdown-item" href="/codingelite.org/structure.html">structures</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/codingelite.org/blog.html">More Topics</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/book.html">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/contact.html">Contact us</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="bg-light">
<div class="container bg-c">
<h1 class="hed hedi-h1"><strong> Branching statements</strong></h1>
<h3>Branchinng statements in C</h3>
<p>A branching statements is also known as selection of control statements. <br> C languagec executes the
program statements in a sequence,branching statements are used to the alter the flow of the sequence of
statements. branching statements also known as control statements. prgrammer can jump from one part of the
program to other with the help of branching statements.
</p>
<h5><b>Branching statements are as follows:</b></h5>
<ol>
<li><b> if statement </b></li>
<li><b> if else statement</b></li>
<li><b> nested if </b></li>
<li><b> if else ladder</b></li>
<li><b> switch case </b></li>
</ol>
<h4><b>1. if statement</b></h4>
<h6>syntax of if statement:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
if (condition)
{
//body of if statement
statements;
}
</pre>
</div>
</div>
<p>in "if" statement, respective block of code is executed when condition is true.</p>
<h5>Sample program</h5>
<h6>Enter any two numbers and check which number is maximum between these two.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int a, b;
printf(“Enter the frist number\n”);
scanf(“%d ”,&a);
printf(“Enter the second number\n”);
scanf(“%d ”,&b);
if(a>b)
{
printf(“a is max and value of max is %d”, a);
}
else
{
printf(“b is max and value of max is %d”, b);
}
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the frist numbers
14
Enter the second number
27
b is max and value of max is 27
</pre>
</div>
<h4><b>2. if else statement</b></h4>
<h6>syntax of if else statement:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
if (condition)
{
//body of if statement
statements;
}
else
{
//body of else statement
}
</pre>
</div>
</div>
<p>in "if else " statement, if block of code is executed when condition is true, otherwise else block of code executed.</p>
<h5>Sample program</h5>
<h6>Check wheather the entered number is odd or even.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int n;
printf(“Enter the number\n”);
scanf(“%d ”,&n);
if(n%2==0)
{
printf(“Entered number is even”);
}
else
{
printf(“Entered number is odd”);
}
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the number
14
Entered number is even
</pre>
</div>
<h4><b>3. nested if else statement</b></h4>
<h6>syntax of nested if else :</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
if(condition1)
{
//body of else Statements;
if(condition2)
{
if(condition3)
{
//body of if Statements;
}
else
{
Statements;
}
else
{
//body of else Statements;
}
else
{
//body of else Statements;
}
</pre>
</div>
</div>
<p>In “nested if else ” statement, if condition 1 is false, then condition 2 is checked and if condition 2 is true then statements of that respective if bock are executed. <br>
If condition 2 also gets false, then else part is executed.
</p>
<h5>Sample program</h5>
<h6>Write a program to find the type of a triangle.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
float a, b, c;
clrscr();
printf(“Enter the three sides of a triangle\n”);
scanf(“ %f %f %f”, &a, &b, &c);
printf(“ The given sides are \n”);
printf(“% 8.2f %8.2f %8.2f \n”, a, b, c);
if(((a+b) > c) && ((a+c) > b) && ((b+c) > a))
{
if(((a==b) && (a==c))
{
printf(“Equilateral triangle \n”);
}
else
{
if((a==b) !! (a==c) !! (b==c))
{
printf(“Isosceles triangle \n ”);
}
else
{
printf(“ Simple triangle \n”);
}
}
}
else
{
printf(“Triangle is not possible”);
}
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the three sides of a triangle
3.0
3.0
3.0
The given sides are 3.0 3.0 3.0
Equilateral triangle
</pre>
</div>
<h4><b>4. if else if ladder</b></h4>
<h6>syntax of if else if ladder statement:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
if (condition1)
{
Statement1;
}
else if (condition2)
{
Statement2;
}
else if (condition3)
{
Statement3;
}
.
.
else
{
Default Statement;
}
</pre>
</div>
</div>
<p>In “if else if ladder” every if statement having condition, if that condition get executed and result of condition is true then body of respective if block get executed and runtime cursor get of that if else if ladder and next statement after that ladder get executed.</p>
<h5>Sample program</h5>
<h6>Implement a program to print the week day using if else if ladder.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int day ;
clrscr();
printf(“Enter the week day number between 1 to7 \n”);
scanf(“%d ”,&day);
if(day==1) {
printf(“\n Week day is Monday”);}
else if(day==2){
printf(“\n Week day is Tuesday”);}
else if(day==3) {
printf(“\n Week day is Wednesday”);}
else if(day==4){
printf(“\n Week day is Thursday”); }
else if(day==5){
printf(“\n Week day is Friday”); }
else if(day==6) {
printf(“\n Week day is Saturday”); }
else if(day==7) {
printf(“\n Week day is Sunday”); }
else {
printf(“\n Wrong choice. Please enter correct week day.”); }
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the week day number between 1 to7
4
Week day is Thursday
</pre>
</div>
<h4><b>5. switch case statement</b></h4>
<h6>syntax of switch case statement:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
switch(control expression)
{
case constant1 :
statements;
break;
case constant2 :
statements;
break;
………
………
default :
statements;
}
</pre>
</div>
</div>
<p> In switch case statement, there is no condition instead of that there is control expression. If control expression value does not match with any of case constant then last default case get executed. Here case ends with break statement. break statement breaks current switch case and cursor goes out of switch case. <br>Switch case statements are used to execute only specific case statements based on the switch expression </p>
<h5>Sample program</h5>
<h6>Implement a program to print the week day using if else if ladder.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int n;
clrscr();
printf("Enter the number of day\n");
scanf("%d",&n);
switch(a)
{
case 1:
printf("the day is monday");
break;
case 2:
printf("the day is tuesday");
break;
case 3:
printf("the day is wensday");
break;
case 4:
printf("the day is thrusday");
break;
case 5:
printf("the day is friday");
break;
case 6:
printf("the day is saturday");
break;
case 7:
printf("the day is sunday");
break;
default:
printf("enter correct number");
}
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the number of day
4
the day is thursday
</pre>
</div>
<h5>Examples For Practice</h5>
<ol>
<li>Check whether user entered character is vowel or consonant.</li>
<li>Input three numbers and find maximum between them.</li>
<li>Implement a program to perform mathematical calculation i.e +,-,*,/ (Take input & operator from user and by using else if ladder perform operation).</li>
<li> Write a C program to check whether a number is even or odd using switch case.</li>
</ol>
<a href="/codingelite.org/introduction.html"> <button type="button" class="btn btn-primary btn-m"> <<Previous</button> </a>
<a href="/codingelite.org/looping.html"> <button type="button" class="btn btn-primary btn-m btn-r">Next >></button></a>
</div>
</div>
<footer class="page-footer font-small blue navbar-dark bg-light footer-ptt">
<div class="footer-copyright text-center py-3">
© Copyright 2021. All Right Reserved. | Designed by the <a href="/codingelite.org/about.html">CodingElite team</a>. |
<a class="color-b" href="/codingelite.org/privacyPolicy.html">Privacy policy</a> | <a class="color-b" href="/codingelite.org/contact.html">Contact us</a>
</div>
</footer>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script>
-->
</body>
</html>