//
Main.m
C2_ Branching structure
//
Created by Dllo on 15/7/1.
Copyright (c) 2015 Zhozhicheng. All rights reserved.
//
#import <Foundation/Foundation.h>
Write a macro definition
Macro definitions are used to replace code
#define PI 2.15
A macro definition is a substitution of the previous macro definition for the name of the subsequent content
Enumeration
Enum Season {
Spring
Summer
Autumn
Winter
};
int main (int argc, const char * argv[]) {
Define a variable of type bool
BOOL isTrue = NO;
BOOL is selected
BOOL isSelected = YES;
printf ("%d\n", isSelected);
printf ("%g\n", PI);
Relational operators < <= > >= = = =
BOOL isTrue = 3 > 5;
//
logical operators
int a = 10;
//
BOOL isTrue = 3 > 5 && ++a;
printf ("%d\n", a);
Logic: The entire expression is true only if both sides of the equation are present, and if the first condition is false, then the second condition is not executed, called the logic and short-circuit problem
int a = 10;
BOOL isTrue = 3 < 5 | | ++a;
printf ("%d\n", a);
Logical OR: As long as one condition is true, the entire expression is true, and if the first condition is already true, the second condition is not executed, called a logical or short-circuit condition
If the first form of
int a = 10;
if (= = a) {
printf ("Set up, execute print statement \ n");
// }
//
char a = 0;
scanf ("%c", &a);
if (a = = ' m ') {
printf ("Male \ n");
} else{
printf ("Female \ n");
// }
int a = 0;
Input Join scanf
scanf ("%d", &a);
if (a% = = 0 | | A%4 = 0 && a% 100! = 0) {
printf ("Leap year \ n");
} else{
printf ("Not a leap year \ n");
// }
//
int year = 0;
scanf ("%d", &year);
BOOL result1= Year% 400 = = 0;
BOOL result2= year%4 ==0 && year% 100! = 0;
if (result1 | | result2 = = 1) {
printf ("Leap year \ n");
//
} else{
printf ("Not a leap year \ n");
// }
char c = 0;
scanf ("%c", &c);
if (' w ' = = c) {
printf ("Female \ n");
}else if (' m ' ==c) {
printf ("Male \ n");
//
}else {
printf ("Shemale \ n");
// }
If the condition after the parentheses cannot be added;
char c = 0;
scanf ("%c", &c);
if (c >= ' 0 ' && C <= ' 9 ') {
printf ("This is digital\n");
}else if (c >= ' A ' && C <= ' Z ') {
printf ("This was capital letter\n");
}else if (c >= ' a ' && c <= ' z ') {
printf ("This is letter\n");
}else{
printf ("other\n");
// }
//
Switch case
int a = 0;
scanf ("%d", &a);
Switch (a) {
Case 901:
printf ("Finance department \ n");
Break
Case 902:
printf ("Security Department \ n");
Break
Case 903:
printf ("Technical department \ n");
Break
Case 904:
printf ("HR \ n");
Break
Case 905:
printf ("Logistic division \ n");
Break
//
Default
printf ("Please re-enter \ n");
Break
// }
The switch stop flag is whether the Case,default has a break, or the last line of code that runs to the switch will stop
int a = 0;
scanf ("%d", &a);
Switch (a) {
Case 1:
printf ("spring\n");
Break
Case 2:
printf ("summer\n");
Break
Case 3:
printf ("autumn\n");
Break
Case 4:
printf ("winter\n");
Break
//
//
Default
Break
// }
//
//
Print the contents of the enumeration
printf ("%d\n", spring);
printf ("%d\n", summer);
printf ("%d\n", autumn);
printf ("%d\n", winter);
//
Enumeration starts from 0 by default
The function of an enumeration is to add readability to the code, and each of the enumerations represents an integer.
In the case of switch cases, replace with an enumeration
int a = 0;
scanf ("%d", &a);
Switch (a) {
Case Spring:
printf ("spring\n");
Break
Case Summer:
printf ("summer\n");
Break
Case Autumn:
printf ("autumn\n");
Break
Case Winter:
printf ("winter\n");
Break
//
//
//
Default
Break
// }
//
int a = 10,b = 20;
if (a > B) {
printf ("%d\n", a);
}else{
printf ("%d\n", b);
// }
//
Conditional operators
int a = ten, b = 20,d = 15;
int c = a > B? A:B;
c = c > d? C:d;
printf ("%d\n", c);
//
return 0;
}
c2-Branching structure