LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2004, 10:36 PM   #1
salahuddin_66
Member
 
Registered: Jan 2004
Location: Dhaka, Bangladesh
Distribution: Debian, Gentoo
Posts: 283

Rep: Reputation: 30
Wink C Learners


i am just trying to learn C my self

i compile in gcc
-----------------------------
gcc -ansi 87b.c -o test
----------------------------

but i could not understand this prog the use of
-------------
right = 0;
------------
right = 1;
-------------
if(!right)
------------
for(chances=0; chances<3 && !right; chances++) "only the not right use why"




Code:
 
#include <stdio.h>

int main(void) 
{
int answer, count , chances, right;

for (count=1; count<11; count++){
    printf("What is %d + %d?", count, count);
    scanf("%d" , &answer);
    
    if (answer == count+count) printf("Right!\n");
    else{
       printf("Sorry, you're wrong\n");
       printf("Try again\n");
       
       right = 0;
       
       /* nested for */
       
           for(chances=0; chances<3 && !right; chances++){
	   printf("What is %d + %d?", count, count);
           scanf("%d" , &answer);
	   
	   if (answer == count+count){
	   printf("Right!\n");
	   right = 1;
	   }
	   
	  }
	  
	  /*if answer still wrong, tell user */
	  
	  if(!right)
	   printf("What is %d + %d?", count, count);
	  }
	  }
	  
	
return 0;

}
 
Old 08-29-2004, 10:48 PM   #2
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
hi
the program is quite clear

right=0 ,right =1 are used as flags here, these can be used when we want to mark a state

here when u go to else part then , mark it as right=0,which means we have gone to that part,else right would not have been =0

so later
for
if(!right)
if we have right =0
then !right make it '1'and the if condition bcomes

if(1)
which is true and it goes on to that section of IF condition

if it was

if(0)
then it wuld have not gone there


did u get this???


regards
 
Old 08-29-2004, 11:09 PM   #3
salahuddin_66
Member
 
Registered: Jan 2004
Location: Dhaka, Bangladesh
Distribution: Debian, Gentoo
Posts: 283

Original Poster
Rep: Reputation: 30
thanx understand

now tell me just

if(1)

will go to the if statement that contains the right =1 (the second if)

and the other if (0) will go the frist ( if or else) section ???
 
Old 08-30-2004, 02:30 AM   #4
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
hi
no. no u got this wrong
for ex.
we have x=1;
then if we have this statement
if(x==1)
will check whether x is equal to 1

so if(1) means that the if statement is true always and it will execute the block of the if statenment

1 = true
and 0 = false
in boolean

so if u use if(1) in a c program then the block below it will b executed all the time

so in ur program if right is set to 0 then
then the block of
if(!right) will be executed
i.e "printf("What is %d + %d?", count, count);"
also if(!right ) is same as (if right==0)
when u use some kinds of flags we geneally use this
i hope now u get it

regards
 
Old 08-30-2004, 03:35 AM   #5
salahuddin_66
Member
 
Registered: Jan 2004
Location: Dhaka, Bangladesh
Distribution: Debian, Gentoo
Posts: 283

Original Poster
Rep: Reputation: 30
yes



thanx
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Opinons on hardware requirements for (Learners) network jimdaworm Linux - Networking 9 12-16-2003 07:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration