Just finished the second week left, and the following is the third week.
Week Three: Judging
1.if and else can be followed by a statement without {}. How will the program execute if it does not have {} after it, but follows two statements followed by an else statement?
Enter the following code in Dev C + + to compile
#include"stdio.h"intMain () {intA=0; scanf ("%d",&a); if(a>0) A+=1; printf ("a=%d", a); ElsePrint ("a=%d", a); return 0;}
The following questions were found:
Tip "9 2 [Error] ' else ' without a previous ' if '"
It means that else could not find the corresponding if, the second statement after the IF, printf makes the compiler think that if the end, it is a non-else if statement, so the subsequent direct input else will cause the compiler to error.
2./* */is a comment, the compiler regardless of its contents. But have you ever wondered what the compiler would replace it with? Is it a space? Or Leave no trace? Can I write code to test the compiler you use?
Enter the following code in Dev C + + to compile
#include"stdio.h"intMain () {int/*SDFD*/b//compiled byinch/*SDFF*/t A;//compile error, prompt [ERROR] unknown type name ' in ' and [ERROR] expected ' = ', ', ', '; ',//' asm ' or ' __attribute__ ' before ' a ' return 0;}
found that the first statement was compiled, and the second statement was compiled with an error, prompting "[ERROR] Unknown type name ' in ' and [ERROR] expected ' = ', ', ', '; ', ' asm ' or ' __attribute__ ' before ' A ', judged by these two results,/**/is replaced by the compiler when it is compiled with a space
3. Can the segmented function be used with switch-case? Is it worth it?
Considering that X=0 and x>0 are the same function f (x) =2*x, then you can divide the type into 0/12 cases to use the Switch-case statement, it is not worth the cost, and if the segment is increased, it is not necessarily possible to implement this method
The code is as follows:
#include"stdio.h"intMain () {inttype; Doublex,f; scanf ("%LF",&x); Type= (x<0); Switch(type) { Case 0: F=2*x; Break; default: F=-1; Break; } printf ("%f", F); return 0;}
C language Programming (Onge)--three remaining points in the third week of courseware