Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
5. C Programming - Decision Making - Branching

5. C Programming - Decision Making - Branching

Ratings: (0)|Views: 539|Likes:
Published by neo_411

More info:

Published by: neo_411 on Jun 18, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

06/17/2009

pdf

text

original

 
C Programming - Decision Making -Branching
C Programming - Decision Making - Branching
In this tutorialyou will learn about C Programming - Decision Making, Branching, if  Statement, The If else construct, Compound Relational tests, Nested if Statement, TheELSE If Ladder, The Switch Statement and The GOTO statement.
Branching
TheC languageprograms presented until now follows a sequential form of execution of statements. Many times it is required to alter the flow of the sequence of instructions. Clanguage provides statements that can alter the flow of a sequence of instructions. Thesestatements are called control statements. These statements help to jump from one part of the program to another. The control transfer may be conditional or unconditional.
if Statement: 
The simplest form of the control statement is the If statement. It is very frequently used indecision making and allowing the flow of program execution.The If structure has the following syntax
 
if (condition)statement;
The statement is any valid C’ language statement and the condition is any valid C’language expression, frequently logical operators are used in the condition statement. Thecondition part should not end with a semicolon, since the condition and statement should be put together as a single statement. The command says if the condition is true then perform the following statement or If the condition is fake the computer skips thestatement and moves on to the next instruction in the program.
 
Example program
Sample Code
1.# include <stdio.h> //Include the stdio.h file2.void main () // start of the program3.{4. int numbers // declare the variables5. printf ("Type a number:") // message to the user6. scanf ("%d", &number) // read the number fromstandard input7. if (number < 0) // check whether the number is anegative number8. number = -number // if it is negative thenconvert it into positive9. printf ("The absolute value is %d \n", number)// print the value10.}
Copyright exforsys.com
The above program checks the value of the input number to see if it is less than zero. If itis then the following program statement which negates the value of the number isexecuted. If the value of the number is not less than zero, we do not want to negate it thenthis statement is automatically skipped. The absolute number is then displayed by the program, and program execution ends.
The If else construct:
The syntax of the If else construct is as follows:-The if else is actually just on extension of the general format of if statement. If the resultof the condition is true, then program statement 1 is executed, otherwise programstatement 2 will be executed. If any case either program statement 1 is executed or 
 
 program statement 2 is executed but not both when writing programs this else statementis so frequently required that almost all programming languages provide a specialconstruct to handle this situation.
Sample Code
1.#include <stdio.h> //include the stdio.h header filein your program2. void main () // start of the main3. {4. int num // declare variable num as integer5. printf ("Enter the number") // message to theuser6. scanf ("%d", &num) // read the input number fromkeyboard7. if (num < 0) // check whether number is lessthan zero8. printf ("The number is negative") // if it isless than zero then it is negative9. else // else statement10. printf ("The number is positive") // if it ismore than zero then the given number is positive11.}
Copyright exforsys.com
In the above program the If statement checks whether the given number is less than 0. If it is less than zero then it is negative therefore the condition becomes true then thestatement The number is negative is executed. If the number is not less than zero the If else construct skips the first statement and prints the second statement declaring that thenumber is positive.
Compound Relational tests:
C language provides the mechanisms necessary to perform compound relational tests. Acompound relational test is simple one or more simple relational tests joined together by

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->