2015.1.22
Single branch structure
if (conditional expression)
//{
Execute the statement;
//}
/*int Main (int argc, const char * argv[]) {
int score;
scanf ("%d", &score);
Error correction processing
if (score<0| | SCORE>100) {
return-1;
}
if (score >=90)
{
printf ("Excellent \ n");
return 0;
}
if (score>=70) {
printf ("Good \ n");
return 0;
}
if (score>=60) {
printf ("Pass \ n");
return 0;
}
printf ("Less than lattice \ n");
return 0;
}*/
Dual-branch structure
if (conditional expression)
//{
Execute statement 1;
//}
Else
//{
EXECUTE statement 2;
//}
The conditional expression establishes the Execution Statement 1, does not establish the execution statement 2;
int main (int argc,const char *argv[])
//{
int score;
scanf ("%d", &score);
if (score<0 | | score>100) {
printf ("Score input error \ n");
return-1;
// }
//
if (score>=60) {
printf ("Pass \ n");
// }
Else
// {
printf ("Less than lattice \ n");
// }
//
return 0;
//}
Determine whether the year is a leap or common year,
A leap year is one that can be divisible by 4 and not divisible by 100, or divisible by 400.
int main (int argc,const char *argv[])
//{
int year;
scanf ("%d", &year);
//
if ((!) ( year%4) && year%100) | | ! (year%400)) {
printf ("Leap year \ n");
// }
Else
// {
printf ("Common year \ n");
// }
return 0;
//}
Multi-branch structure
if (expression 1)
//{
Statement list 1;
//}
else if (expression 2)
//{
Statement List 2;
//}
else if (expression 3)
//{
Statement list 3;
//}
//...
Else
//{
Statement list;
//}
/*int Main (int argc,const char *argv[])
{
int score;
scanf ("%d", &score);
if (score>=90 && score <=100) {
printf ("Excellent \ n");
}
else if (score >=70 && score <90)
{
printf ("Good \ n");
}
else if (score >=60 && score < 70)
{
printf ("Pass \ n");
}
else if (score >=0 && score < 60)
{
printf ("Less than lattice \ n");
}
Else
{
printf ("Input error \ n");
return-1;
}
return 0;
}
*/
1) Enter a month to determine which season the month belongs to
2 3 4 Spring
5 6 7 Summer
8 9 10 Fall
11 12 1 Winter
/* #include <stdlib.h>
int main (int argc, const char *argv[])
{
int month;
scanf ("%d", &month);
if (month<1| | MONTH>12) {
printf ("Month input error \ n");
return-1;
Exit (-1);
}
else if (month>=2 && month<=4)
{
printf ("Spring \ n");
}
else if (month>=5 && month<=7)
{
printf ("Summer \ n");
}
else if (month>=8 && month<=10)
{
printf ("Autumn \ n");
}
Else
{
printf ("Winter \ n");
}
return 0;
}*/
if (an expression)
//{
if (an expression)
// {
//
// }
Else
// {
//
// }
//}
Else
//{
if ()
// {
// }
else if ()
// {
//
}else if ()
// {
//
// }
Else
// {
//
// }
//}
/*int Main (int argc,const char *argv[])
{
int choice, sex;
printf ("(1) Wine Heart Chocolate (2) Scallop chocolate (3) Nutty chocolate \ n");
printf ("Please enter your gender (1 boys, 2 girls):");
scanf ("%d", &sex);
printf ("Please enter your choice (1~~3):");
scanf ("%d", &choice);
if (sex<1 | | sex>2 | | choice <1 | | Choice >3) {
printf ("Input error \ n");
return-1;
}
if (sex==1) {//Boys
if (choice==1)
{
printf ("You are a master of love, very good for girls like \ \");
}
else if (choice ==2)
{
printf ("You are a timid person, always worried that you are not good enough \");
}
Else
{
printf ("You are all about girls, but she always does not feel");
}
}
else//Girls
{
if (choice==1) {
printf ("In love, you are often at a disadvantage \ n");
}
else if (choice==2)
{
printf ("You are a romantic person, in love can be a good adjustment atmosphere \ \");
}
Else
{
printf ("You are a strong woman, always ignore each other's feelings in love \ \");
}
}
return 0;
}
*/
If a university student's monthly consumption between 1-500 yuan for low consumption, between 501-1000 yuan for medium consumption, more than 1000 yuan for high consumption, 0 yuan for 0 consumption, 0 yuan below for negative consumption program to achieve
/* #include <math.h>
int main (int argc,const char *argv[])
{
float money;
scanf ("%f", &money);
if (Money >=0) {
if (!money) {
printf ("The student consumes \ 0");
}
else if (Money <=500)
{
printf ("The student low-spending%.2f rmb\n", money);
}
else if (Money <= 1000)
{
printf ("The student is medium-consumed%.2f rmb\n", money);
}
Else
{
printf ("The student consumes%.2f rmb\n", money);
}
}
Else
{
printf ("The student earns%.2f rmb\n", Fabs (Money)); The//fabs function evaluates the absolute value of the floating-point type data
}
return 0;
}*/
Switch statements
switch (expression)
//{
case constant Expression 1:
Statement list 1;
Break
Case constant Expression 2:
Statement List 2;
Break
Case constant Expression 3:
Statement list 3;
Break
//...
Default
Statement list;
Break
//}
int main (int argc,const char *argv[])
//{
int A, B;
Char ch;
scanf ("%d%c%d", &a, &ch, &b);//3+5 5*6 7-3
//
Switch (CH) {
Case ' + ':
printf ("a+b =%d\n", a+b);
Break;//break jump out of the switch switch statement
Case '-':
printf ("A-B =%d\n", A-a);
Break
Case ' * ':
printf ("a*b =%d\n", a*b);
Break
Case '/':
printf ("A/b =%d\n", A/b);
Break
default://Default Defaults
printf ("Input error \ n");
Break
// }
//
return 0;
//}
Enter month judgment season
int main (int argc,const char *argv[])
{
int month;
scanf ("%d", &month);
Switch (month) {
Case 2:
Case 3:
Case 4:
printf ("Spring \ n");
Break
Case 5:
Case 6:
Case 7:
printf ("Summer \ n");
Break
Case 8:
Case 9:
Case 10:
printf ("Autumn \ n");
Break
Case 11:
Case 12:
Case 1:
printf ("Winter \ n");
Break
Default
printf ("Input error \ n");
Break
}
return 0;
}
Switch statement with break removed
int main (int argc,const char *argv[])
//{
int num;
scanf ("%d", &num);
//
Switch (num) {
Case 1:
printf ("Hello one\n");
Break
Case 2:
printf ("Hello two\n");
Break
Case 3:
printf ("Hello three\n");
Break
//
Default
Break
// }
//
//
//}
IOS-C_DAY4___ Branching structure