Вы находитесь на странице: 1из 9

Chapter 3

Conditional Branching The if Statement


The usage of the if statement is quite easy to get the hang of. After typing if, you put the condition in a pair of brackets, followed by a statement you want the program to read is the condition is true. If you want multiple statements, use a statement block.

The else Statement


If the condition is not true, sometimes you want the program do something as a last resort type of thing. So after the if statement, you use an else statement. This time, there is no condition to put in. Again, you can use a single statement or a statement block for multiple statements. Here's a proper operators#$
int main() { int a; printf("Input an integer and push return:\n"); scanf("%d" !a); if ( (a%"##$) !! (a%%##$) ) printf("%d is a multiple of " and %\n" a); else printf("%d is not a multiple of &oth " and %\n" return $; '

program! "not the most efficient but it demonstrates relation and logical

#include <stdio.h>

a);

%hen including logical operators, putting brackets around the e&pression helps the readability of the program.

Saying else if
Sometimes you want the program to test for more conditions if the first one isn't met and this is done by using else if. 'ou tag on a condition in the usual way followed by a single statement or a statement block.
opyright (ogic )ption *+t. (td

Here's an e&ample!
#include <stdio.h> int main() { int a &; printf("Input t(o integers separated (ith a space and push return:\n"); scanf("%d %d" !a !&); if (a##&) printf("%d is e)ual to %d.\n" a else if (a<&) printf("%d is less than %d.\n" a else printf("%d is greater than %d.\n" return $; ' &); &); a &);

Also, you can put nested if statements in your program like this!
#include <stdio.h> int main() { int a & c; printf("Input t(o integers separated (ith a space and push return:\n"); scanf("%d %d" !a !&); c # a * &; if (a##&) { printf("%d is e)ual to %d.\n" a &); printf("+nd their sum is e,en.\n"); ' else if (a<&) { printf("%d is less than %d.\n" a &); if (c%"##$) printf("+nd their sum is e,en.\n"); else printf("+nd their sum is odd.\n"); ' else { printf("%d is greater than %d.\n" a &); if (c%"##$) printf("+nd their sum is e,en.\n"); else printf("+nd their sum is odd.\n"); '
opyright (ogic )ption *+t. (td

'

return $;

.otice the statement blocks/#

The Conditional Operator


It's possible to say 0If this condition is true, do this.... , otherwise, do this .... 0 all in one line. Isn't that great/# Here's how 1 using the conditional operator. The quoted part abo+e can be represented in code like this!
a - & : c

%here a is a condition, & is an e&pression that is performed when the condition is true otherwise the e&pression in c is performed. As always, here's an e&ample!
.##/$ - printf(". is e)ual to /$\n") : printf(". is not e)ual to /$\n");

2e careful with your semi1colons here. 3emember that & is an e&pression, rather than a statement, since if you stick a semi1colon after it, the program wouldn't compile. The same applies for c but the only reason why there's a semi1colon at the end is because, as a whole, the line containing the conditional operator is a statement. 'ou can write the abo+e e&ample like this!
printf("%s\n" (.##/$) - ". is e)ual to /$" : ". is not e)ual to /$");

The switch Statement


Sometimes you want the program to branch off depending on what the user inputs "for e&ample$. 'ou could do this using many nested if statements but this can get comple& at times. So you could use the s(itch statement instead. Here's the general form!
s(itch (e.pression) { case e.pression/: 45 one or more statements 54 case e&pression-! 45 one or more statements 54 45 more cases if necessary 54 default!
opyright (ogic )ption *+t. (td

45 the default statement"s$ 54 7

The program will branch off depending on what is returned by the e&pression in the parentheses. The default case is e&ecuted when something else other than the case e&pression"s$ is returned. Howe+er, all is not what it seems. Take this e&ample!
#include <stdio.h> int main() { int a; printf("Input / " 0 or 1 and push return:\n"); scanf("%d" !a); s(itch (a) { case /: printf("2ou case ": printf("2ou case 0: printf("2ou case 1: printf("2ou default: printf("2ou ' return $; '

chose num&er /\n"); chose num&er "\n"); chose num&er 0\n"); chose num&er 1\n"); chose a num&er that3s not / " 0 or 1\n");

It might look okay, but it's not. 'ou see, when you choose number , you might e&pect that 0'ou chose number ,0 would be printed out onto the screen. %ell, that doesn't happen 1 the computer will run through the other cases "e&cept the default case$ until the closing brace is reached. In this case, the computer will print 0'ou chose number ,0, 0'ou chose number -0, 0'ou chose number 60 and 0'ou chose number 80 when you select ,. This is when you are introduced to .....

The break Statement


If you want to 0break0 away from the s(itch statement after a case has been selected, place the &rea4 keyword as the last statement. Here's the abo+e e&ample with the &rea4 statements added in!
#include <stdio.h> int main() { char a;
opyright (ogic )ption *+t. (td

printf("Input / " 0 or 1 and push return:\n"); scanf("%c" !a); s(itch (a) { case 3/3: printf("2ou &rea4; case 3"3: printf("2ou &rea4; case 303: printf("2ou &rea4; case 313: printf("2ou &rea4; default: printf("2ou ' return $; '

chose num&er /\n"); chose num&er "\n"); chose num&er 0\n"); chose num&er 1\n"); chose a num&er that3s not / " 0 or 1\n");

.ow it should work like you'd e&pect. .otice that added single quote marks around the e&pressions after the case keyword is surrounded. This is because that a is now of the char type, with the corresponding %c format specifier in the scanf statement. Howe+er, this method falls flat on its face when you enter a - digit number like 0,-0 as it only reads in the first number. pre+ious method## 9o and e&periment with the s(itch statement#

The goto Statement


3umour has it that the goto statement should be a+oided because it can cause compilation errors. .o harm in using it in the most simplest of programs though but don't make a habit of using it##
#include <stdio.h> int main() { int num&er attempt; num&er # 15; looping: 45 This is :ust a name for a point in the printf"09uess a number from ; to ,;;<n0$= scanf"0>d0, ?attempt$= if"number@@attempt$ printf"0'ou guessed correctly#<n<n0$= else A
opyright (ogic )ption *+t. (td

program 54

printf"0(et me ask again...<n<n0$= goto looping= 45 This is of the form, 0goto0 and then the name of the point. 54 7 return ;= 7

Looping
So, you want to do things o+er and o+er again do you/# Then without further ado, let me first introduce.....

For Loops
Here is one of the commonest forms!
for ( e.pression/ ; condition ; e.pression" ) { 45 one or more statements 54 7

%hen the loop is first encountered, e.pression/ is e&ecuted. Then the statement"s$ in the braces are e&ecuted if and only if the condition is true "i.e. if a non1Cero +alue is returned, in many cases, would be ,$ but e.pression" is e+aluated right after the statement block has been e&ecuted. D&ample!
#include <stdio.h> int main() { int a; for( a#$ ; a<#/$ ; a** ) printf("%d\n" a); ' return $;

It's pretty straight forward. 2asically the numbers ; through to ,; are printed out on separate lines. on the first iteration. a<#/$ is true "and so , is returned$, so the statement block is e&ecuted. At the end of the iteration, a is incremented by ,.
a#$
opyright (ogic )ption *+t. (td

on the ne&t iteration. a<#/$ is still true, so the statement block is e&ecuted. At the end of the iteration, a is incremented by ,. And so on.
a#/

on the ,,th iteration. a<#/$ is still true, so the statement block is e&ecuted. At the end of the iteration, a is incremented by ,.
a#/$

on the ,-th iteration. a<#/$ is no longer true "and returns Cero$ true, so the statement block is .)T e&ecuted. And the program mo+es on, terminating in this case.
a#//

While loops
%hile loops only take one e&pression in the brackets. If this e&pression returns a non1Cero +alue, the whole loop is e&ecuted. Here's an e&ample program!
#include <stdio.h> int main() { int . # /$;

45 initialiCe & 54

while"&F@;$ A 45 program will loop gi+en that & is greater or equal to Cero 54 printf"0The +alue of & is >d<n0, &$= &11= 45 decrease & by ,54 7 printf"0<n.ow we're out of the while loop.0$= printf"0 The final +alue of & is >d<n0, &$= return ;= 7 45 don't forget that main"$ returns an integer by default# 54

Gost of the time you'd want to put a condition in the brackets. 2ut there are times when the conditional operator might come in handy!
#include <stdio.h> int main() { int . # 0%; (hile( (.%/6##$) - $ : /) { printf("%d is not di,isi&le &7 /6\n" .**; ' printf("8ut %d is9\n" return $; .); .);

'

opyright (ogic )ption *+t. (td

3emember that the loop is performed if the e&pression in the brackets return a non1Cero +alue ", in many cases$.

Do-while loops
In while loops, the e&pression in the brackets is e+aluated first before and the loop is performed if the e&pression returns , "i.e. true$. So, in some cases, the loop might not e+en be performed once. This is where do1while loops come in handy 1 the loop is performed first, before the e+aluation part, so it is looped through at least once. Here's and e&ample!
#include <stdio.h> int main() { int . # $; do { printf(". # %d\n" .**; ' (hile(.</$); printf(":ut of the loop ' return $; .);

. # %d\n"

.);

nfinite loops
Sometimes we want the program to loop fore+er until the user presses ctrl1c "on I.IJ 1 don't know how to stop one on %indows##$ to stop it, or when the program reaches a &rea4 statement. To do this, you could :ust replace the e+aluated e&pression in the brackets of a for or while loop like this!
#include <stdio.h> int main() { int . # $; int 7; (hile(/) { printf("%d " if(.##/$$) &rea4; else .**; ' .);

opyright (ogic )ption *+t. (td

printf("\n\n"); for(7 # $ ; / ; 7**) { printf("%d " 7); if(7##/$$) &rea4; else 7**; ' printf("\n\n"); printf(":ut of the loops return $; ' . # %d and 7 # %d\n" . 7);

opyright (ogic )ption *+t. (td

Вам также может понравиться