C Tutorial/Statement/If statement — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:32, 25 мая 2010
Содержание
- 1 Checking for a Range of Values
- 2 if Comparisons and Their Opposites
- 3 if statement
- 4 if statement: compare the value from user input
- 5 if statement with two else statements
- 6 If with else statement
- 7 Operators Used in if Comparisons
- 8 The if-else-if statement
- 9 The if statement: a compound statement
- 10 To execute only one statement, opening and closing braces are not required
- 11 Use compound conditions to check for upper and lowercase letters
- 12 Use isdigit Function in if statement
- 13 Using nested ifs
Checking for a Range of Values
<source lang="cpp">#include <stdio.h> main() {
int iResponse = 0; printf("Enter a number from 1 to 10: "); scanf("%d", &iResponse); if ( iResponse < 1 || iResponse > 10 ) printf("\nNumber not in range\n"); else printf("\nThank you\n");
}</source>
Enter a number from 1 to 10: 2 Thank you
if Comparisons and Their Opposites
if Comparison else Statement Executed By This Condition < >= == != > <= <= > >= < != ==
if statement
To conditionally execute statements, you can use the if or the if...else statement.
The general form of the if statement is
<source lang="cpp">if(expr) {
s1 ; s2 ; .... }</source>
if statement: compare the value from user input
<source lang="cpp">#include <stdio.h> int main(void) {
int number = 0; printf("\nEnter an integer between 1 and 10: "); scanf("%d",&number); if (number > 5) printf("You entered %d which is greater than 5\n", number); if (number < 6) printf("You entered %d which is less than 6\n", number); return 0;
}</source>
Enter an integer between 1 and 10: 5 You entered 5 which is less than 6
if statement with two else statements
<source lang="cpp">#include <stdio.h>
- include <stdlib.h>
int main() {
char num[2]; int number; printf("Enter a number from 0 to 9:"); gets(num); number=atoi(num); if(number<5) { printf("That number is less than 5!\n"); } else if(number==5) { printf("You typed in 5!\n"); } else { printf("That number is more than 5!\n"); } return(0);
}</source>
Enter a number from 0 to 9:2 That number is less than 5!
If with else statement
The general format for an if-else statement is
<source lang="cpp">if (condition)
simple or compound statement
else
simple or compound statement.</source>
Operators Used in if Comparisons
Comparison Meaning or Pronunciation "True" Example < Less than 1 < 5, 8 < 9 == Equal to 7 == 7, 1 == 1 > Greater than 7 > 5, 10 > 0 <= Less than or equal to 4 <= 5, 8 <= 8 >= Greater than or equal to 6 >= 5, 2 >= 2 != Not equal to 1 != 0, 1 != 3.99
The if-else-if statement
The general format for the if-else if statement is:
<source lang="cpp">if (condition 1)
simple or compound statement
else if (condition 2)
simple or compound statement
else if ( condition 3)
simple or compound statement .....
else if ( conditon n )
simple or compound statement</source>
The if statement: a compound statement
If a compound statement is used, it must be enclosed in opening and closing braces.
<source lang="cpp">#include<stdio.h>
main()
{
int i=5, j =1; if (i > j){ i = i + 1; printf("%d",i); } else j = j +1;
}</source>
6
To execute only one statement, opening and closing braces are not required
<source lang="cpp">#include <stdio.h> main(){
int i = 5; if(i > 0) printf(" i > 0. \n");
}</source>
i > 0.
Use compound conditions to check for upper and lowercase letters
<source lang="cpp">#include <stdio.h> main(){
char cResponse = "\0"; printf("Enter the letter A: "); scanf("%c", &cResponse); if ( cResponse== "A" || cResponse == "a" ) printf("\nCorrect response\n"); else printf("\nIncorrect response\n");
}</source>
Enter the letter A: 1 Incorrect response
Use isdigit Function in if statement
<source lang="cpp">#include <stdio.h>
- include <ctype.h>
main() {
char cResponse = "\0"; printf("\nPlease enter a letter: "); scanf("%c", &cResponse); if ( isdigit(cResponse)== 0 ) printf("\nThank you\n"); else printf("\nYou did not enter a letter\n");
}</source>
Please enter a letter: 1 You did not enter a letter
Using nested ifs
<source lang="cpp">#include <stdio.h>
- include <limits.h>
int main(void) {
long test = 0L;
if( test % 2L == 0L) { printf("The number %ld is even", test); if ( (test/2L) % 2L == 0L) { printf("\nHalf of %ld is also even", test); printf("\nThat"s interesting isn"t it?\n"); } } else printf("The number %ld is odd\n", test); return 0;
}</source>
The number 0 is even Half of 0 is also even That"s interesting isn"t it?