C Language Learning Example three

Source: Internet
Author: User

#inlude <studio.h>
int main () {
int x, y;
sanf ("%d", &x);
if (x<0)
Y=-1;
Else
if (x==0) y=0;
else Y=1;
printf ("x=%d,y=%d\n", X, y);
return 0;
 
}
 
 
 
or
#include <studio.h>
int main () {
int x, y;
scanf ("%d", &x);
if (x>=0)
if (x>0) Y=1;
else y=0;
else y=-1;
printf ("x=%d,y=%d\n", X, y);
return 0;
}
 



#inlude <studio.h>
int main () {
char grade;
scanf ("%c", &grade);
printf ("Your score:");
switch (grade) {
Case ' A ': printf ("100\n");
Case ' B ': printf ("84\n");
Case ' C ': printf ("69\n");
Case ' D ': printf ("<60\n");
default:p rintf ("Enter date error!\n");
 
}
return 0;
}
 



#include <studio.h>
int main () {
void action1 (int, int), Action2 (int,int);
Char ch;
int a=15,b=22;
Ch=getchar ();
switch (h) {
Case ' a ':
Case ' A ': Action1 (A, b);
Case ' B ':
Case ' B ': Action2 (A, b);
.
.
.
Default:purchar (' \a ');
}
return 0;
 
}
 
void Action1 (int a,int b) {
printf ("x+y=%d\n", x+y);
 
}
void Action2 (int a,int b) {
 
printf ("x*y=%d\n", X*y)
}







While loops and do.....while loops

#include <studio.h>
int main () {
int i=1,sum=0;
While (i<=100) {
Sum=sum+i;
i=i+1;
 
 
 
}
printf ("sum=%d\n", sum);
return 0;
 
}
 

Or

#include <studio.h>
int main () {
int i=1,sum=0;
do{
Sum=sum+i;
i++;
printf ("%d", i++);
}while (i<=100);
printf ("sum=%d\n", sum);
return 0;
 
}
 

There is only one difference between the while loop and the Do While loop: the while loop first judges the condition, and then the loop body is formed;
Do While loop is the first to do the loop body once, and then determine whether the condition is set up, if set up, then do the loop body, otherwise jump out.


in other words, it may be better to use while the initial condition is ambiguous. This depends on your personal habits.
limit input, you must enter a number from 0 to 10, otherwise reenter.
 
Analysis: To enter first, and then determine whether the input conditions are appropriate, not appropriate to enter. This is in line with the idea of the Do and loop. I give the source code directly:
 
#include "stdio.h"
Main ()
{
int num;
do{
printf ("Please enter the number 0 to 10:");//Enter the prompt first
scanf ("%d", &num);//read a number from the keyboard
}while (num>10| | NUM<0); If this number is no longer 0~10, which is less than 0 or greater than 10, re-enter
printf ("input correct \ n");//input is correct to jump out of the loop and give a hint, \ n = line break
}
 
as with a for loop, the while and do while can omit curly braces in the case of the loop body with only one sentence.

C Language Learning Example three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.