11/1 c언어 시험 대비 복습
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i=4;
printf("if문\n");
if(i>5){
printf("참: i는 5보다 큽니다.\n ");
//조건을 만족할 때 수행하는 수행문
}else{
printf("거짓: i는 5보다 작습니다.\n ");
//조건을 만족하지 않을 때 수행하는 실행문
}
printf("\nelse-if문\n");
if(i==1){
printf("i가 1입니다.\n");
}else if(i==2){
printf("i가 2입니다.\n");
}else{
//아무것도 적지 않아도 괜찮다.
}
//and와 or연산자
//and연산자 &&
//or 연산자 ||
printf("\nand or 연산자.\n");
if(i>3 && i<5){
printf("i는 4가 맞습니다.\n");
}
printf("\nbreak문\n");
//break문
int k;
for(k=0;k<10;k++){
if(k==6){
printf("6입니다.\n");
break; //반복문을 탈출하고 싶을 때 사용
}
printf("%d입니다.\n", k);
}
printf("\ncontinue문\n");
//continue문
for(k=0;k<10;k++){
if(k==6){
printf("6입니다.\n");
continue; //반복문을 탈출하지 않고 다음 문장만 실행하지 않는다.
}
printf("%d입니다.\n", k);
printf("\n난수생성\n");
//난수 생성하기
//헤더파일
srand(time(NULL)); //난수 초기화
rand() % 3; //0~2까지
rand() % 3+1; //1~3까지
printf("\nswitch\n");
//switch문
int i;
switch(p){
case 1:
printf("1입니다.\n");
break;
case 2:
printf("2입니다.\n");
break;
defauit: //모든 case조건에 들어맞지 않을 떄
break;
}
}
}
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.