Anasayfa » C / C++

C / C++ Courses ? 11 (Negation Operator and For Loops)

7 Mayıs 2011 1.946 views 2 Yorum

The Negation Operator (! – NOT)

x=3;
if(! (x>10) )
printf(?%d?,x);    //it will print ?3?.
if(!0)
printf(?hey?);    //it will print ?hey?.
if(!x)
printf(?%d?,x);    //it will print nothing.

!true=false
!false=true

->  When the number of repetition is unknown

while(input != -1){    // ?-1? is called sentinal value.

//codes which will be executed

}

Ex:

Write a program that enters any number of students? grades. The program will find and print the average of the grade. (continue entering grades until -1 is entered)

#include stdio.h
int main(){

int student=0;
int grade, sum=0;
float average;
printf(?Enter grade or press -1 to end\n?);
scanf(?%d?,&grade);

while(grade!=-1){

sum+=grade;
student++;
printf(“Enter grade or press -1 to end\n”);
scanf(“%d”,&grade);

}
//According to number of students, the result will be written
if(student !=0){

average=(float)sum/student;
printf(“Average is %d \n”,average);

}else

printf(“No students”);

}

3 Steps Which We Should Follow

1– Declare the input and use scanf initialized before while
2- Use the input in condition while and compare it with sentinal value
3- Change the input using scanf before the end of the loop


FOR LOOPS
This is repetition structure.

int counter=1;
while(counter<=100){

printf(“Your name\n”);
counter++;

}

//This is equal to below one:

for(int counter=1, counter<=100, counter++)

printf(“Your name\n”);

Usage of for loop:

for(“1-initializing counter”,”2-condition”,”3-increasing or decreasing the counter”){

//Codes will be executed in every step

}

>>”initializing the counter” part is executed just the beginning of the loop.  After this initialization, it won’t be used anymore.
It will initialize the counter and then look if the condition is true or false. If condition is true, the codes will be executed and the counter will be increased or decreased. And then it will look at the condition again. It will increase or decrease the counter again until the condition is false.

-Different ways to write for headers-

int x;
for(x=1; x<=10; x++)

printf(“%d”,x);

//OR

for(int x=1; x<=10; x++)

printf(“%d”,x);

//OR

int x=1;
for(; x<=10; x++)

printf(“%d”,x);

//OR

int x=1;
for(; x<=10; x++)

printf(“%d”,x);

//OR

int x=1;
for(; x<=10;){

printf(“%d”,x);
x++;

}

//But if we write like that, this loop will be infinite. Because the condition will be true if the condition is empty

int x=1;
for( ; ; ){

printf(“%d”,x);
x++;

}

Ex:

Write a loop that prints the number 7 to 77 with increment of 7.

int a;
for(a=7; a<=77; a+=7)

printf(“%d”,a);

Ex:

Find the output of below:

int x=1;
while(++x <= 10);

printf(“%d”,x);

//It will print 11. Because while loop will just increase x.(because of the semicolon after while loop)

Ex:

Write a program that enter the side of a right triangle. The program will print a triangle as follows.
If user enters 4, it will print below without the border;) :

_____
|      * |
|    ** |
|  *** |
|**** |
_____

int main(){

int x;
printf(“Enter a number\n”);
scanf(“%d”,&x);
int row, col, a;

for(row=1; row<=x;row++){

for(a=1; a<=x-row; a++)

printf(” “);    //It prints space.

for(col=1; col<=row; col++)

printf(“*”);    //It prints stars

printf(“\n”);        //It is for new line

}

}

Ex: Write a program that inputs an integer and prints the value of y below.

if x=1, y=3+x*5
if x=2, y=x*x*x
Any other value of x, y=0

int main(){

int x,y;
printf(“Enter an integer\n”);
scanf(“%d”,&x);

if(x==1)

y=3+x*5;

else if(x==2)

y=x*x*x;

else

y=0;

}

//It is better to use “else/if”, not just “if”.
//Because x has just one value, if one of the condition is true, there is no need to execute other conditions.
//The other reason is, we must have “else” part for the other values. So we should use if/else

 
 

Take care of yourself..

The Negation Operator (! – NOT)
x=3;
if(! (x>10) )
printf(?%d?,x);    //it will print ?3?.
if(!0)
printf(?hey?);    //it will print ?hey?.
if(!x)
printf(?%d?,x);    //it will print nothing.

 

2 Yorum »

  • davut selami dedi ki:

    Selamlar. Öğrenci not ortalaması hesabındaki if(student==0) yerine if(student != 0) olması gerektiğini düşünüyorum. Saygılarımla, iyi günler.

  • Ziynet (admin) dedi ki:

    Evet bariz bir hataymış kendisi, teşekkürler.

Yoruma Açığız! :)

Be nice. İçeriği çirkin olmadığı sürece her yoruma, öneriye ve soruya "evet" :)

Etiketler: , , , , ,